Burn the Beats!

情報系の話題について書いていきたい。

CGI関係の個人的なメモ書き(python3)

個人的なメモ書きです。
python3系のお話

CGIHTTPServerを建てる。

cgiserver.py

import http.server
http.server.test(HandlerClass=http.server.CGIHTTPRequestHandler)

これで http://localhost:8000 に繋げばcgiserver.pyを走らせているディレクトリのindex.htmlが表示される。

CGI書くときの注意

マルチバイトの文字を扱おうとするとエラーを吐かれるので

import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

スクリプトの頭に書いておくこと。