Skip to content

Commit f82da58

Browse files
authoredMar 13, 2025
Merge pull request #78 from SakuraPuare/master
refactor: consolidate port connection checks into a single method
2 parents 07a60af + dcc83fd commit f82da58

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed
 

‎app/lib/static/util.py

+13-8
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
@@ -70,18 +69,24 @@ def get_system_proxy(sys_plc=None):
7069
def is_local_connect(add, prt):
7170
# 检测本地是否可通
7271
try:
73-
telnetlib.Telnet(add, port=prt, timeout=1)
74-
return True
72+
port = int(port)
73+
if port >= 0 and port <= 65535:
74+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
75+
s.settimeout(1)
76+
return s.connect_ex((add, port)) == 0
7577
except:
7678
return False
7779

7880
@staticmethod
7981
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
82+
try:
83+
port = int(port)
84+
if port >= 0 and port <= 65535:
85+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
86+
s.settimeout(1)
87+
return s.connect_ex(("localhost", port)) == 0
88+
except:
89+
return False
8590

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

0 commit comments

Comments
 (0)
Please sign in to comment.