75 lines
2.9 KiB
Python
75 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
project_root = Path(__file__).parent
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
if __name__ == "__main__":
|
|
print("🚀 使用Waitress启动原始FastAPI扑克服务器...")
|
|
print("📍 服务器地址: http://127.0.0.1:8005")
|
|
print("🎮 游戏界面: http://127.0.0.1:8005/client/index.html")
|
|
print("✅ 使用原始ArenaGame实现")
|
|
print("🪟 Windows优化版本")
|
|
|
|
try:
|
|
from shortdeck_server.main import app
|
|
from waitress import serve
|
|
|
|
def wsgi_app(environ, start_response):
|
|
import asyncio
|
|
import json
|
|
from urllib.parse import parse_qs
|
|
|
|
method = environ['REQUEST_METHOD']
|
|
path = environ['PATH_INFO']
|
|
query_string = environ.get('QUERY_STRING', '')
|
|
|
|
scope = {
|
|
'type': 'http',
|
|
'method': method,
|
|
'path': path,
|
|
'query_string': query_string.encode(),
|
|
'headers': [(k.lower().replace('_', '-').encode(), v.encode())
|
|
for k, v in environ.items() if k.startswith('HTTP_')],
|
|
}
|
|
|
|
response_data = {'status': 500, 'headers': [], 'body': b''}
|
|
|
|
async def receive():
|
|
content_length = int(environ.get('CONTENT_LENGTH', 0))
|
|
if content_length > 0:
|
|
body = environ['wsgi.input'].read(content_length)
|
|
return {'type': 'http.request', 'body': body}
|
|
return {'type': 'http.request', 'body': b''}
|
|
|
|
async def send(message):
|
|
if message['type'] == 'http.response.start':
|
|
response_data['status'] = message['status']
|
|
response_data['headers'] = message.get('headers', [])
|
|
elif message['type'] == 'http.response.body':
|
|
response_data['body'] += message.get('body', b'')
|
|
|
|
try:
|
|
loop = asyncio.new_event_loop()
|
|
asyncio.set_event_loop(loop)
|
|
loop.run_until_complete(app(scope, receive, send))
|
|
loop.close()
|
|
except Exception as e:
|
|
response_data['status'] = 500
|
|
response_data['body'] = f"Server Error: {str(e)}".encode()
|
|
|
|
status = f"{response_data['status']} OK"
|
|
headers = [(h[0].decode(), h[1].decode()) for h in response_data['headers']]
|
|
start_response(status, headers)
|
|
return [response_data['body']]
|
|
|
|
serve(wsgi_app, host='127.0.0.1', port=8005, threads=4)
|
|
|
|
except KeyboardInterrupt:
|
|
print("\n 服务器停止")
|
|
except Exception as e:
|
|
print(f" 服务器错误: {e}")
|
|
import traceback
|
|
traceback.print_exc() |