Skip to content

Commit 3d1735d

Browse files
committed
refactor: consolidate port connection checks into a single method
1 parent 07a60af commit 3d1735d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

app/lib/static/util.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,25 @@ def get_system_proxy(sys_plc=None):
6767
return ""
6868

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

7886
@staticmethod
7987
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
88+
return StaticUtil.check_port_connection(port)
8589

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

0 commit comments

Comments
 (0)