34 lines
1.0 KiB
Python
Raw Normal View History

2025-01-18 09:10:52 +08:00
from datetime import datetime
class ProxyUtils:
@staticmethod
def format_hex_data(data):
"""格式化十六进制数据显示"""
return ' '.join([f"{b:02X}" for b in data])
@staticmethod
def extract_pile_id(data):
"""从数据包中提取桩号"""
try:
if len(data) > 10: # 确保数据包足够长
# 桩号在第5-8个字节
pile_id = ''.join([f"{b:02X}" for b in data[3:11]])
return pile_id
return None
except Exception:
return None
@staticmethod
def get_socket_info(socket_obj):
"""获取socket的本地和远程地址信息"""
try:
local_address = socket_obj.getsockname()
remote_address = socket_obj.getpeername()
return local_address, remote_address
except:
return None, None
@staticmethod
def get_current_time():
"""获取当前时间的格式化字符串"""
return datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]