Spaces:
Runtime error
Runtime error
Update websocket_uploader.py
Browse files- websocket_uploader.py +87 -76
websocket_uploader.py
CHANGED
@@ -6,6 +6,7 @@ import sys
|
|
6 |
import base64
|
7 |
import os
|
8 |
import time
|
|
|
9 |
|
10 |
# 配置日志
|
11 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -33,95 +34,98 @@ async def upload_files_via_websocket(space_id, user_token, website_url):
|
|
33 |
|
34 |
logger.info(f"找到 {len(files_to_upload)} 个文件需要上传")
|
35 |
|
36 |
-
# 构建WebSocket URL
|
37 |
-
ws_url = website_url.replace('http://', 'ws://').replace('https://', 'wss://') + '/
|
38 |
|
39 |
try:
|
40 |
# 连接到WebSocket服务器
|
41 |
logger.info(f"正在连接到 WebSocket 服务器: {ws_url}")
|
42 |
|
43 |
-
#
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
@sio.event
|
49 |
-
async def connect():
|
50 |
logger.info("WebSocket 连接成功")
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
'space_id': space_id,
|
54 |
'user_token': user_token
|
55 |
-
}
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
except Exception as e:
|
86 |
logger.error(f"WebSocket 连接或上传过程中发生错误: {e}")
|
87 |
return False
|
88 |
|
89 |
-
async def upload_files(sio, files_to_upload, space_id, user_token):
|
90 |
-
"""上传文件列表"""
|
91 |
-
success_count = 0
|
92 |
-
failed_count = 0
|
93 |
-
|
94 |
-
for file_path in files_to_upload:
|
95 |
-
try:
|
96 |
-
filename = os.path.basename(file_path)
|
97 |
-
logger.info(f"正在上传文件: {filename}")
|
98 |
-
|
99 |
-
# 读取文件内容并编码为base64
|
100 |
-
with open(file_path, 'rb') as f:
|
101 |
-
file_content = f.read()
|
102 |
-
file_b64 = base64.b64encode(file_content).decode('utf-8')
|
103 |
-
|
104 |
-
# 发送文件上传请求
|
105 |
-
await sio.emit('file_upload', {
|
106 |
-
'space_id': space_id,
|
107 |
-
'user_token': user_token,
|
108 |
-
'filename': filename,
|
109 |
-
'content': file_b64
|
110 |
-
})
|
111 |
-
|
112 |
-
success_count += 1
|
113 |
-
# 稍微延迟避免服务器压力过大
|
114 |
-
await asyncio.sleep(0.5)
|
115 |
-
|
116 |
-
except Exception as e:
|
117 |
-
logger.error(f"上传文件 {file_path} 时发生错误: {e}")
|
118 |
-
failed_count += 1
|
119 |
-
|
120 |
-
logger.info(f"上传完成! 成功: {success_count}, 失败: {failed_count}")
|
121 |
-
|
122 |
-
# 上传完成后断开连接
|
123 |
-
await sio.disconnect()
|
124 |
-
|
125 |
def main():
|
126 |
if len(sys.argv) != 4:
|
127 |
print("用法: python3 websocket_uploader.py <space_id> <user_token> <website_url>")
|
@@ -136,7 +140,14 @@ def main():
|
|
136 |
logger.info(f"Website URL: {website_url}")
|
137 |
|
138 |
# 运行异步上传
|
139 |
-
asyncio.run(upload_files_via_websocket(space_id, user_token, website_url))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
main()
|
|
|
6 |
import base64
|
7 |
import os
|
8 |
import time
|
9 |
+
import ssl
|
10 |
|
11 |
# 配置日志
|
12 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
34 |
|
35 |
logger.info(f"找到 {len(files_to_upload)} 个文件需要上传")
|
36 |
|
37 |
+
# 构建WebSocket URL - 使用原始WebSocket而不是socket.io
|
38 |
+
ws_url = website_url.replace('http://', 'ws://').replace('https://', 'wss://') + '/ws'
|
39 |
|
40 |
try:
|
41 |
# 连接到WebSocket服务器
|
42 |
logger.info(f"正在连接到 WebSocket 服务器: {ws_url}")
|
43 |
|
44 |
+
# 创建SSL上下文(如果是wss://)
|
45 |
+
ssl_context = None
|
46 |
+
if ws_url.startswith('wss://'):
|
47 |
+
ssl_context = ssl.create_default_context()
|
48 |
+
ssl_context.check_hostname = False
|
49 |
+
ssl_context.verify_mode = ssl.CERT_NONE
|
50 |
|
51 |
+
async with websockets.connect(ws_url, ssl=ssl_context) as websocket:
|
|
|
|
|
|
|
52 |
logger.info("WebSocket 连接成功")
|
53 |
+
|
54 |
+
# 发送注册消息
|
55 |
+
register_msg = {
|
56 |
+
'type': 'register',
|
57 |
'space_id': space_id,
|
58 |
'user_token': user_token
|
59 |
+
}
|
60 |
+
await websocket.send(json.dumps(register_msg))
|
61 |
+
logger.info("已发送注册消息")
|
62 |
+
|
63 |
+
# 等待注册确认
|
64 |
+
response = await websocket.recv()
|
65 |
+
response_data = json.loads(response)
|
66 |
+
|
67 |
+
if response_data.get('type') == 'registered':
|
68 |
+
logger.info("注册成功,开始上传文件")
|
69 |
+
|
70 |
+
# 上传文件
|
71 |
+
success_count = 0
|
72 |
+
failed_count = 0
|
73 |
+
|
74 |
+
for file_path in files_to_upload:
|
75 |
+
try:
|
76 |
+
filename = os.path.basename(file_path)
|
77 |
+
logger.info(f"正在上传文件: {filename}")
|
78 |
+
|
79 |
+
# 读取文件内容并编码为base64
|
80 |
+
with open(file_path, 'rb') as f:
|
81 |
+
file_content = f.read()
|
82 |
+
file_b64 = base64.b64encode(file_content).decode('utf-8')
|
83 |
+
|
84 |
+
# 发送文件上传消息
|
85 |
+
upload_msg = {
|
86 |
+
'type': 'file_upload',
|
87 |
+
'space_id': space_id,
|
88 |
+
'user_token': user_token,
|
89 |
+
'filename': filename,
|
90 |
+
'content': file_b64
|
91 |
+
}
|
92 |
+
|
93 |
+
await websocket.send(json.dumps(upload_msg))
|
94 |
+
|
95 |
+
# 等待上传结果
|
96 |
+
upload_response = await websocket.recv()
|
97 |
+
upload_result = json.loads(upload_response)
|
98 |
+
|
99 |
+
if upload_result.get('type') == 'upload_success':
|
100 |
+
logger.info(f"文件 {filename} 上传成功")
|
101 |
+
success_count += 1
|
102 |
+
else:
|
103 |
+
logger.error(f"文件 {filename} 上传失败: {upload_result}")
|
104 |
+
failed_count += 1
|
105 |
+
|
106 |
+
# 稍微延迟避免服务器压力过大
|
107 |
+
await asyncio.sleep(0.5)
|
108 |
+
|
109 |
+
except Exception as e:
|
110 |
+
logger.error(f"上传文件 {file_path} 时发生错误: {e}")
|
111 |
+
failed_count += 1
|
112 |
+
|
113 |
+
logger.info(f"上传完成! 成功: {success_count}, 失败: {failed_count}")
|
114 |
+
|
115 |
+
# 发送完成消息
|
116 |
+
complete_msg = {'type': 'upload_complete'}
|
117 |
+
await websocket.send(json.dumps(complete_msg))
|
118 |
+
|
119 |
+
return success_count > 0
|
120 |
+
|
121 |
+
else:
|
122 |
+
logger.error(f"注册失败: {response_data}")
|
123 |
+
return False
|
124 |
|
125 |
except Exception as e:
|
126 |
logger.error(f"WebSocket 连接或上传过程中发生错误: {e}")
|
127 |
return False
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
def main():
|
130 |
if len(sys.argv) != 4:
|
131 |
print("用法: python3 websocket_uploader.py <space_id> <user_token> <website_url>")
|
|
|
140 |
logger.info(f"Website URL: {website_url}")
|
141 |
|
142 |
# 运行异步上传
|
143 |
+
result = asyncio.run(upload_files_via_websocket(space_id, user_token, website_url))
|
144 |
+
|
145 |
+
if result:
|
146 |
+
logger.info("所有文件上传成功")
|
147 |
+
sys.exit(0)
|
148 |
+
else:
|
149 |
+
logger.error("文件上传失败")
|
150 |
+
sys.exit(1)
|
151 |
|
152 |
if __name__ == "__main__":
|
153 |
main()
|