Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: consolidate port connection checks into a single method #78

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor: enhance port connection checks
SakuraPuare committed Mar 13, 2025
commit dcc83fd4349a19233bfa0b03357a4c4edc141381
21 changes: 13 additions & 8 deletions app/lib/static/util.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@
import platform
import re
import socket
import telnetlib
import time

from altfe.interface.root import interRoot
@@ -70,18 +69,24 @@ def get_system_proxy(sys_plc=None):
def is_local_connect(add, prt):
# 检测本地是否可通
try:
telnetlib.Telnet(add, port=prt, timeout=1)
return True
port = int(port)
if port >= 0 and port <= 65535:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(1)
return s.connect_ex((add, port)) == 0
except:
return False

@staticmethod
def is_prot_in_use(port):
port = int(port)
if port >= 0 and port <= 65535:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0
return False
try:
port = int(port)
if port >= 0 and port <= 65535:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(1)
return s.connect_ex(("localhost", port)) == 0
except:
return False

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