目次

概要

Webページ上にホスト名、時刻、プロセスリストを出力するPython CGIのサンプル

サンプル

転送用HTML

転送元ディレクトリに格納する。(/var/www/htmlなど)

  • index.html
    <!DOCTYPE html>
    <html lang="ja">
    <head>
    <meta charset="uft-8">
    <script>
    setTimeout("location.href='/cgi-bin/index.py'",0);
    </script>
    <title>redirect</title>
    </head>
    <body>
    </body>
    </html>

スクリプト

CGIの実行権限が付与されたディレクトリに格納する。(/var/www/cgi-binなど)

  • index.py [#t623780e]
    #!usr/bin/python3
    
    #
    # index page create
    #
    
    ## data import os, datetime, subprocess
    sysname = os.uname()[1]
    date = datetime.datetime.now()
    now = '{0:%Y/%m/%d %H:%M:%S}'.format(date)
    cmd = "ps ax | grep -v '\[' | sed 's/$/<br>/g' | grep -v 'sed'"
    procs = subprocess.check_output(cmd, shell=True).decode('utf-8')
    
    ## HTML
    html = """
    
    <HTML>
    <HEAD>
    <title>index page</title>
    </HEAD>
    <BODY>
    <div align="center">
    <hr>
    %s
    <hr>
    <table>
    <tr><td>Date : </td><td>%s</td></tr>
    <tr><td>Procs : </td><td>%s</td></tr>
    </table>
    </div>
    </BODY>
    </HTML>
    """
    
    ## output
    print ( 'Content-type: text/html' )
    print ( '' )
    print ( html % (sysname,now,procs) )
    
    # EOF

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-01-03 (木) 23:35:49 (2031d)