ruff格式化
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from .remote import main
|
from .remote import main
|
||||||
|
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
@@ -10,16 +10,17 @@ import asyncio
|
|||||||
# UUID文件路径
|
# UUID文件路径
|
||||||
UUID_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "client_uuid.json")
|
UUID_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "client_uuid.json")
|
||||||
|
|
||||||
|
|
||||||
# 生成或获取客户端唯一ID
|
# 生成或获取客户端唯一ID
|
||||||
def get_unique_id():
|
def get_unique_id():
|
||||||
# 检查是否已经有保存的UUID
|
# 检查是否已经有保存的UUID
|
||||||
if os.path.exists(UUID_FILE):
|
if os.path.exists(UUID_FILE):
|
||||||
try:
|
try:
|
||||||
with open(UUID_FILE, 'r') as f:
|
with open(UUID_FILE, "r") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
if 'client_id' in data:
|
if "client_id" in data:
|
||||||
print("从本地文件读取客户端ID")
|
print("从本地文件读取客户端ID")
|
||||||
return data['client_id']
|
return data["client_id"]
|
||||||
except (json.JSONDecodeError, IOError) as e:
|
except (json.JSONDecodeError, IOError) as e:
|
||||||
print(f"读取UUID文件出错: {e},将生成新的UUID")
|
print(f"读取UUID文件出错: {e},将生成新的UUID")
|
||||||
|
|
||||||
@@ -28,14 +29,15 @@ def get_unique_id():
|
|||||||
|
|
||||||
# 保存UUID到文件
|
# 保存UUID到文件
|
||||||
try:
|
try:
|
||||||
with open(UUID_FILE, 'w') as f:
|
with open(UUID_FILE, "w") as f:
|
||||||
json.dump({'client_id': client_id}, f)
|
json.dump({"client_id": client_id}, f)
|
||||||
print("已保存新生成的客户端ID到本地文件")
|
print("已保存新生成的客户端ID到本地文件")
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
print(f"保存UUID时出错: {e}")
|
print(f"保存UUID时出错: {e}")
|
||||||
|
|
||||||
return client_id
|
return client_id
|
||||||
|
|
||||||
|
|
||||||
# 生成客户端唯一ID
|
# 生成客户端唯一ID
|
||||||
def generate_unique_id():
|
def generate_unique_id():
|
||||||
# 结合主机名、系统信息和随机UUID生成唯一ID
|
# 结合主机名、系统信息和随机UUID生成唯一ID
|
||||||
@@ -43,22 +45,14 @@ def generate_unique_id():
|
|||||||
unique_id = f"{system_info}-{uuid.uuid4()}"
|
unique_id = f"{system_info}-{uuid.uuid4()}"
|
||||||
return unique_id
|
return unique_id
|
||||||
|
|
||||||
|
|
||||||
def send_heartbeat(server_url, client_id):
|
def send_heartbeat(server_url, client_id):
|
||||||
"""向服务器发送心跳"""
|
"""向服务器发送心跳"""
|
||||||
sys = platform.system()
|
sys = platform.system()
|
||||||
try:
|
try:
|
||||||
headers = {
|
headers = {"Client-ID": client_id, "User-Agent": f"HeartbeatClient/{client_id[:8]}"}
|
||||||
'Client-ID': client_id,
|
data = json.dumps({"system": sys})
|
||||||
'User-Agent': f'HeartbeatClient/{client_id[:8]}'
|
response = requests.post(f"{server_url}/api/clients", headers=headers, data=data)
|
||||||
}
|
|
||||||
data = json.dumps({
|
|
||||||
'system': sys
|
|
||||||
})
|
|
||||||
response = requests.post(
|
|
||||||
f"{server_url}/api/clients",
|
|
||||||
headers=headers,
|
|
||||||
data=data
|
|
||||||
)
|
|
||||||
|
|
||||||
if response.status_code == 201:
|
if response.status_code == 201:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
@@ -72,6 +66,7 @@ def send_heartbeat(server_url, client_id):
|
|||||||
logger.debug(f"发送心跳时出错: {e}")
|
logger.debug(f"发送心跳时出错: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
# 配置
|
# 配置
|
||||||
SERVER_URL = "http://hyybuth.xyz:10058" # 更改为你的服务器地址
|
SERVER_URL = "http://hyybuth.xyz:10058" # 更改为你的服务器地址
|
||||||
@@ -96,5 +91,6 @@ async def main():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"发生意外错误: {e}")
|
print(f"发生意外错误: {e}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user