Eclipse で実行させるのと、WWWサーバー( ここでは AN HTTP Server )で実行させるのとでは、やはりもっといろいろな切り口の理解が必要になります。 とにかく、CGI としてベタな QUERY_STRING を使用してベタな format で作成後、f-string でヒアドキュメントで HTML 部分を簡潔にして、最後に cgi モジュールで GETとPOST を兼用にしました。(FieldStorage() の戻りの内容が変更できないようなので、単純な dict に入れ替えています。) ▼ GET/POST 共用 : 結局 cgi モジュール で作成 : f-string ヒアドキュメント埋め込み
import cgi
import cgitb
cgitb.enable()
import sys
import io
import os
import urllib.parse
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
print("Content-Type: text/html; charset=utf-8")
print( "Expires: Thu, 19 Nov 1981 08:52:00 GMT" )
print( "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" )
print( "Pragma: no-cache" )
print()
form = cgi.FieldStorage()
fields = {}
fld_names = {"氏名": "field1", "フリガナ":"field2"}
for value in fld_names.values():
if value not in form:
fields[value] = ""
else:
fields[value] = form[value].value
out_client = f"""<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post">
<p>氏名 : <input type="text" name="{fld_names["氏名"]}" value="{fields[fld_names["氏名"]]}"></p>
<p>フリガナ : <input type="text" name="{fld_names["フリガナ"]}" value="{fields[fld_names["フリガナ"]]}"></p>
<p>送信 : <input type="submit" name="send" value="送信"></p>
</form>
</body>
</html>"""
print(out_client)
▼ GET のみ f-string を使った、ヒアドキュメント埋め込み
import cgitb
cgitb.enable()
import sys
import io
import os
import urllib.parse
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
print("Content-Type: text/html; charset=utf-8")
print( "Expires: Thu, 19 Nov 1981 08:52:00 GMT" )
print( "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" )
print( "Pragma: no-cache" )
print()
try:
qs = os.environ["QUERY_STRING"]
except KeyError as e:
qs = ""
get = urllib.parse.parse_qs(qs)
fld_names = {"氏名": "field1", "フリガナ":"field2"}
for value in fld_names.values():
if get.get(value) is None:
get[value] = [""]
out_client = f"""<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form>
<p>氏名 : <input type="text" name="{fld_names["氏名"]}" value="{get.get(fld_names["氏名"])[0]}"></p>
<p>フリガナ : <input type="text" name="{fld_names["フリガナ"]}" value="{get.get(fld_names["フリガナ"])[0]}"></p>
<p>送信 : <input type="submit" name="send" value="送信"></p>
</form>
</body>
</html>"""
print(out_client)
▼ GET のみ : format を使った、クォートの作成が面倒な埋め込み
import cgitb
cgitb.enable()
import sys
import io
import os
import urllib.parse
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
print("Content-Type: text/html; charset=utf-8")
print( "Expires: Thu, 19 Nov 1981 08:52:00 GMT" )
print( "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0" )
print( "Pragma: no-cache" )
print()
try:
qs = os.environ["QUERY_STRING"]
except KeyError as e:
qs = ""
get = urllib.parse.parse_qs(qs)
fld_names = {"氏名": "field1", "フリガナ":"field2"}
for value in fld_names.values():
if get.get(value) is None:
get[value] = [""]
out_client = """<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form>
<p>氏名 : <input type="text" name=\"""" + fld_names["氏名"] + """\" value="{""" + fld_names["氏名"] + """}"></p>
<p>フリガナ : <input type="text" name=\"""" + fld_names["フリガナ"] + """\" value="{""" + fld_names["フリガナ"] + """}"></p>
<p>送信 : <input type="submit" name="send" value="送信"></p>
</form>
</body>
</html>"""
out_client = out_client.format(
field1=get.get(fld_names["氏名"])[0],
field2=get.get(fld_names["フリガナ"])[0]
)
print(out_client)
AN HTTP Server の設定テストにはまだまだ使える(重宝する) AN HTTP Server の正しい使用方法
|
|
【Pythonの最新記事】
- Python : Excel の新しいブックのデフォルトのシートのセルに直接値をセットして、オートフィルを Range オブジェクトから実行する
- Python : shift_jis の3列の csv フォーマットのデータを ttk.Treeview に表示する
- Python : ttk.Treeview で表形式を使用して環境変数の一覧表示
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 1 )
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 4 ) : リダイレクトと関数とログ出力
- XAMPP + Python( 3.8 ) で WEBアプリの基礎部分構築 : その ( 3 ) : cgi.FieldStorage() から ディクショナリ
- XAMPP + Python( 3.8 ) でWEBアプリの基礎部分構築 : その ( 2 ) : QUERY_STRING と 画面定義
- Python + MySQL + IFRAME + Bootstrap : 問い合せ WEB アプリテンプレート
- MySQL Connector/Python の使用方法概要のまとめ
- Python3 : 言語的デザインの特徴と要点
- Python ドキュメントに沿った、テキストファイル読み込みの理解
- Python3 でメール送信 ( さくらインターネット )
- Eclipse + Python(Pydev) : pywin32(COM使用の為) + MySQL Connector/ODBC でループ処理をしながら更新
- Eclipse + Python(Pydev) : MySQL Connector/Python でループ処理をしながら更新
- Pleiades Eclipse 4.7 Oxygen で Python を使って MySQL にアクセスする
- Python 3.4 : インストールと最初にする事( Windows )
- Python3.x : URL を読み出す( bytes から string )
- Python3.1 : URLを読み出して、バイナリのままファイル出力する
- Python3.1 : クッキーを保持して二つの URL にアクセスする
- Python3.1 : URLを読み出してファイルに出力






