Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 186aca0

Browse files
committedMar 13, 2025
refactor: enhance port connection checks
1 parent 07a60af commit 186aca0

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed
 

‎app/lib/static/util.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import platform
33
import re
44
import socket
5-
import telnetlib
65
import time
76

87
from altfe.interface.root import interRoot
@@ -67,21 +66,25 @@ def get_system_proxy(sys_plc=None):
6766
return ""
6867

6968
@staticmethod
70-
def is_local_connect(add, prt):
71-
# 检测本地是否可通
69+
def check_port_connection(port, host="localhost", timeout=1):
7270
try:
73-
telnetlib.Telnet(add, port=prt, timeout=1)
74-
return True
71+
port = int(port)
72+
if port < 0 or port > 65535:
73+
return False
74+
75+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
76+
s.settimeout(timeout)
77+
return s.connect_ex((host, port)) == 0
7578
except:
7679
return False
80+
81+
@staticmethod
82+
def is_local_connect(add, prt):
83+
return StaticUtil.check_port_connection(prt, add)
7784

7885
@staticmethod
7986
def is_prot_in_use(port):
80-
port = int(port)
81-
if port >= 0 and port <= 65535:
82-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
83-
return s.connect_ex(("localhost", port)) == 0
84-
return False
87+
return StaticUtil.check_port_connection(port)
8588

8689
@staticmethod
8790
def format_time(date_string, style, to="%Y-%m-%d %H:%M:%S"):

0 commit comments

Comments
 (0)
Please sign in to comment.