-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
33 lines (26 loc) · 1.05 KB
/
serve.py
File metadata and controls
33 lines (26 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
BizManga ローカル開発サーバー
クリーンURL対応(.htmlなしでアクセス可能)
使い方: python3 serve.py
ブラウザで http://localhost:8000 を開く
停止: Ctrl+C
"""
import http.server
import os
PORT = 8000
class CleanURLHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# Remove query string for file lookup
path = self.path.split('?')[0]
# If path doesn't have extension and isn't a directory, try .html
if path != '/' and '.' not in os.path.basename(path):
html_path = path + '.html'
if os.path.exists(self.directory + html_path):
self.path = html_path + ('?' + self.path.split('?')[1] if '?' in self.path else '')
return super().do_GET()
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.abspath(__file__)))
with http.server.HTTPServer(('', PORT), CleanURLHandler) as httpd:
print(f'BizManga server running at http://localhost:{PORT}')
print('Press Ctrl+C to stop')
httpd.serve_forever()