-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
46 lines (39 loc) · 1.3 KB
/
server.py
File metadata and controls
46 lines (39 loc) · 1.3 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
from http.server import HTTPServer, SimpleHTTPRequestHandler
import webbrowser
import threading
import subprocess
import os
import sys
class GameHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.path = '/index.html'
return SimpleHTTPRequestHandler.do_GET(self)
def run_game():
python_executable = sys.executable
game_path = os.path.join(os.path.dirname(__file__), 'main.py')
try:
subprocess.Popen([python_executable, game_path], cwd=os.path.dirname(__file__))
except Exception as e:
print(f"Error starting game: {e}")
def run_server():
server_address = ('', 8000)
httpd = HTTPServer(server_address, GameHandler)
print("Server running at http://localhost:8000")
print("Game window should open separately")
httpd.serve_forever()
if __name__ == "__main__":
# Start the game
run_game()
# Start the server in a separate thread
server_thread = threading.Thread(target=run_server)
server_thread.daemon = True
server_thread.start()
# Open the browser
webbrowser.open('http://localhost:8000')
# Keep the main thread running
try:
while True:
pass
except KeyboardInterrupt:
print("\nShutting down server...")