新增支持自定义端口
This commit is contained in:
@@ -13,6 +13,7 @@ import threading
|
|||||||
import django
|
import django
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import re
|
import re
|
||||||
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db import models
|
from django.db import models
|
||||||
@@ -1004,11 +1005,13 @@ class SimpleFileWatcher:
|
|||||||
class ServerManager:
|
class ServerManager:
|
||||||
"""服务器管理器"""
|
"""服务器管理器"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, host: str = '0.0.0.0', port: int = 8000):
|
||||||
self.process = None
|
self.process = None
|
||||||
self.watcher = None
|
self.watcher = None
|
||||||
self.base_dir = Path(__file__).resolve().parent
|
self.base_dir = Path(__file__).resolve().parent
|
||||||
self.running = True
|
self.running = True
|
||||||
|
self.host = host
|
||||||
|
self.port = int(port)
|
||||||
|
|
||||||
def start_server(self):
|
def start_server(self):
|
||||||
"""启动服务器进程"""
|
"""启动服务器进程"""
|
||||||
@@ -1017,8 +1020,8 @@ class ServerManager:
|
|||||||
|
|
||||||
cmd = [
|
cmd = [
|
||||||
sys.executable, '-m', 'daphne',
|
sys.executable, '-m', 'daphne',
|
||||||
'-b', '0.0.0.0',
|
'-b', self.host,
|
||||||
'-p', '8000',
|
'-p', str(self.port),
|
||||||
'hertz_server_django.asgi:application'
|
'hertz_server_django.asgi:application'
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1214,9 +1217,18 @@ def main():
|
|||||||
# 步骤4: 启动服务器
|
# 步骤4: 启动服务器
|
||||||
print("\n📋 步骤4: 启动服务器...")
|
print("\n📋 步骤4: 启动服务器...")
|
||||||
print("🚀 启动Hertz Server Django (支持HTTP + WebSocket + 热重启)")
|
print("🚀 启动Hertz Server Django (支持HTTP + WebSocket + 热重启)")
|
||||||
|
parser = argparse.ArgumentParser(add_help=False)
|
||||||
|
parser.add_argument('--port', type=int)
|
||||||
|
args, _ = parser.parse_known_args()
|
||||||
|
env_port = os.environ.get('PORT') or os.environ.get('DJANGO_PORT')
|
||||||
|
try:
|
||||||
|
env_port_int = int(env_port) if env_port is not None else None
|
||||||
|
except ValueError:
|
||||||
|
env_port_int = None
|
||||||
|
port = args.port or env_port_int or 8000
|
||||||
print("📡 使用Daphne ASGI服务器")
|
print("📡 使用Daphne ASGI服务器")
|
||||||
print("🌐 HTTP服务: http://127.0.0.1:8000/")
|
print(f"🌐 HTTP服务: http://127.0.0.1:{port}/")
|
||||||
print("🔌 WebSocket服务: ws://127.0.0.1:8000/ws/")
|
print(f"🔌 WebSocket服务: ws://127.0.0.1:{port}/ws/")
|
||||||
print("🔥 自动热重启: 已启用")
|
print("🔥 自动热重启: 已启用")
|
||||||
print("\n按 Ctrl+C 停止服务器\n")
|
print("\n按 Ctrl+C 停止服务器\n")
|
||||||
|
|
||||||
@@ -1236,7 +1248,7 @@ def main():
|
|||||||
return
|
return
|
||||||
|
|
||||||
# 创建服务器管理器
|
# 创建服务器管理器
|
||||||
server_manager = ServerManager()
|
server_manager = ServerManager(port=port)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 启动服务器
|
# 启动服务器
|
||||||
|
|||||||
Reference in New Issue
Block a user