SJISで書かれたWebフロントエンドページを自PCのブラウザで表示する方法
sjis_server.py
# -*- coding: shift_jis -*-
from http.server import HTTPServer, SimpleHTTPRequestHandler
import re
class ShiftJISHandler(SimpleHTTPRequestHandler):
def end_headers(self):
if self.path.endswith(".html"):
self.send_header("Content-Type", "text/html; charset=Shift_JIS")
super().end_headers()
if __name__ == "__main__":
port = 8000
print(f"📡 サーバーを起動中: http://localhost:{port}")
httpd = HTTPServer(("localhost", port), ShiftJISHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n🛑 サーバーを停止しました")
をUTF-8で保存。
以下のコマンドでサーバを起動:
python sjis_server.py
ブラウザで http://localhost:8000/ にアクセス。