Qilan2 commited on
Commit
4fc2689
·
verified ·
1 Parent(s): b27b8a8

Update server2.py

Browse files
Files changed (1) hide show
  1. server2.py +15 -14
server2.py CHANGED
@@ -451,16 +451,20 @@ def replace_ip_and_country_in_file(file_path):
451
  except Exception as e:
452
  print(f"发生其他错误: {e}")
453
 
454
- def is_port_in_use(port):
455
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
456
- # 尝试绑定到指定的端口
457
- result = sock.connect_ex(('localhost', port))
458
- if result == 0:
459
- print(f"端口 {port} 正在使用中")
460
- return True
461
- else:
462
- print(f"端口 {port} 未被使用")
463
- return False
 
 
 
 
464
 
465
  def SillyTavern():
466
  print('-----------SillyTavern开始-------------')
@@ -543,11 +547,8 @@ if os.path.exists('/sillytavern/start.sh'):
543
  frp_thread1.start()
544
  frp_thread2 = threading.Thread(target=frp, args=(config_data, '/content/frp/frpc.ini', False))
545
  frp_thread2.start()
 
546
  while True:
547
- if not is_port_in_use(8000):
548
- os.chdir('/sillytavern')
549
- subprocess.Popen('yes | sh /sillytavern/start.sh', shell=True)
550
- time.sleep(15)
551
  if backup_thread.is_alive():
552
  print("备份线程仍在运行...")
553
  else:
 
451
  except Exception as e:
452
  print(f"发生其他错误: {e}")
453
 
454
+ import socket
455
+ import threading
456
+ import time
457
+ def monitor_port():
458
+ while True:
459
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
460
+ result = sock.connect_ex(('localhost', 8000))
461
+ if result == 0:
462
+ print(f"端口 {port} 正在使用中")
463
+ else:
464
+ print(f"端口 {port} 未被使用")
465
+ subprocess.Popen('yes | sh /sillytavern/start.sh', shell=True)
466
+ time.sleep(15)
467
+ time.sleep(5)
468
 
469
  def SillyTavern():
470
  print('-----------SillyTavern开始-------------')
 
547
  frp_thread1.start()
548
  frp_thread2 = threading.Thread(target=frp, args=(config_data, '/content/frp/frpc.ini', False))
549
  frp_thread2.start()
550
+ threading.Thread(target=monitor_port, daemon=True).start()#检测端口状态
551
  while True:
 
 
 
 
552
  if backup_thread.is_alive():
553
  print("备份线程仍在运行...")
554
  else: