Python3 で簡易ウェブサーバーを作成する手順をメモしておきます。
1.フォルダレイアウト
C:py
- cgi-bin
- index.py
- index.tpl
2.実装
index.py
print('Content-type: text/html; charset=UTF-8rn') fopen = open("cgi-bin/index.tpl", encoding="utf-8") lines = fopen.read() fopen.close() print(lines)
index.tpl
<html> <head> <title>aaa</title> </head> <body> aaabbbccc </body> </html>
3.コマンド
C:py> python -m http.server --cgi 8000
4.ブラウザでアクセス
http://localhost:8000/cgi-bin/index.py