-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
32 lines (27 loc) · 976 Bytes
/
server.py
File metadata and controls
32 lines (27 loc) · 976 Bytes
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
#!/usr/bin/env python3
import http.server
import socketserver
import webbrowser
import threading
import time
PORT = 3000
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
super().end_headers()
def open_browser():
time.sleep(1)
webbrowser.open(f'http://localhost:{PORT}')
if __name__ == "__main__":
# 启动浏览器
threading.Thread(target=open_browser).start()
# 启动服务器
with socketserver.TCPServer(("", PORT), MyHTTPRequestHandler) as httpd:
print(f"服务器运行在 http://localhost:{PORT}")
print("按 Ctrl+C 停止服务器")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n服务器已停止")