<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form name="form" method="POST" action="/cgi-bin/python_test/test1.py">
name: <input type="text" size="30" name="name"/><br/>
mail: <input type="text" size="30" name="mail"/><br/>
<input type="submit" value="submit" name="button"/>
</form>
</body>
</html>
上記のページで[Submit]ボタンを押すとtest1.pyが実行される。test1.py
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi
# エラー発生時にレポートを表示
import cgitb
cgitb.enable()
html0 = """
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8"/>
<html>
<head>
<title>Test 1</title>
</head>
<body>
"""
html1 = """
</body>
</html>
"""
#######################################################################
# Main function
#######################################################################
if __name__ == '__main__':
print(html0)
form = cgi.FieldStorage()
print('<b>name: </b>' + form['name'].value + '<br/>')
print('<b>mail: </b>' + form['mail'].value + '<br/>')
print(html1)
実行結果(ブラウザに渡されるHTMLソース)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; CHARSET=utf-8">
<title>Test 1</title>
</head>
<body>
<b>name: </b>aa<br>
<b>mail: </b>bb<br>
</body>
</html>
0 件のコメント:
コメントを投稿