Python CGI
CGIで環境変数を出力する
サンプルプログラム:
#!/usr/bin/python
#coding: utf-8
import cgi
import os
print 'Content-Type: text/html'
print
print '''
<html>
<head>
<title>Python env sample</title>
</head>
<body>
'''
cgi.print_environ_usage()
print '<hr>'
print '<pre><table>'
for k in os.environ.keys():
key = cgi.escape(k)
env = cgi.escape(os.environ[k])
print '<tr><td>%s</td><td>%s</td></tr>' % ( key, env )
print'</table></pre>'
print '''
</body>
</html>
'''
サンプルプログラムを実行するサンプルプログラムをダウンロードする
説明:
#!/usr/local/bin/python #coding: utf-8pythonのパスの設定.サーバーによっては,/usr/local/bin/pythonに修正が必要です.
文字コードがutf-8であることを明記しています.
print 'Content-Type: text/html' printCGIでhtmlを出力するときのおまじないです.
cgi.print_environ_usage()CGIで利用できる環境変数の一覧を返します.HTMLタグ付きでprintされます.
os.environ.keys()設定されている環境変数を取得します.
cgi.escape(k)< -> <,> -> >などとエスケープされます.