-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-tester.py
More file actions
60 lines (48 loc) · 1.91 KB
/
web-tester.py
File metadata and controls
60 lines (48 loc) · 1.91 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
"""
간단한 HTTP 정적 웹 서버
로컬호스트 3000번 포트에서 실행됩니다.
"""
import http.server
import os
import socketserver
PORT = 3000
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# .html 확장자 없이 접근 가능하도록 처리
path = self.path.split('?')[0] # 쿼리 파라미터 제거
# 경로가 / 로 끝나면 index.html 추가
if path.endswith('/'):
path += 'index.html'
# .html 확장자가 없고, 파일이 아닌 경우 .html 추가 시도
elif not os.path.isfile('.' + path) and not path.endswith('.html'):
# 확장자가 없으면 .html 추가
if not os.path.splitext(path)[1]:
html_path = path + '.html'
if os.path.isfile('.' + html_path):
path = html_path
# 원래 경로 저장하고 새 경로로 변경
original_path = self.path
self.path = path
# 부모 클래스의 do_GET 호출
http.server.SimpleHTTPRequestHandler.do_GET(self)
# 원래 경로 복원
self.path = original_path
def end_headers(self):
# CORS 헤더 추가 (필요시)
self.send_header('Access-Control-Allow-Origin', '*')
super().end_headers()
def main():
# 현재 스크립트의 디렉토리로 이동
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Handler = MyHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"서버가 http://localhost:{PORT} 에서 실행 중입니다.")
print("종료하려면 Ctrl+C를 누르세요.")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n서버를 종료합니다.")
httpd.shutdown()
if __name__ == "__main__":
main()