Spaces:
Runtime error
Runtime error
Delete remote_uploader.py
Browse files- remote_uploader.py +0 -64
remote_uploader.py
DELETED
@@ -1,64 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
import argparse
|
3 |
-
import os
|
4 |
-
|
5 |
-
def upload_file(server_url, api_key, space_id, file_path):
|
6 |
-
"""
|
7 |
-
通过 API 将文件上传到服务器。
|
8 |
-
|
9 |
-
:param server_url: Flask 服务器的 URL (例如 http://127.0.0.1:5001)
|
10 |
-
:param api_key: 您的用户 API 密钥
|
11 |
-
:param space_id: 与此次上传关联的 Space ID
|
12 |
-
:param file_path: 要上传的文件的本地路径
|
13 |
-
"""
|
14 |
-
if not os.path.exists(file_path):
|
15 |
-
print(f"错误: 文件未找到 -> {file_path}")
|
16 |
-
return
|
17 |
-
|
18 |
-
# 构建完整的 API 端点 URL
|
19 |
-
upload_url = f"{server_url.rstrip('/')}/api/remote_upload"
|
20 |
-
|
21 |
-
# 准备请求头和数据
|
22 |
-
headers = {
|
23 |
-
'X-API-Key': api_key
|
24 |
-
}
|
25 |
-
data = {
|
26 |
-
'space_id': space_id
|
27 |
-
}
|
28 |
-
|
29 |
-
# 读取文件内容
|
30 |
-
try:
|
31 |
-
with open(file_path, 'rb') as f:
|
32 |
-
files = {
|
33 |
-
'file': (os.path.basename(file_path), f)
|
34 |
-
}
|
35 |
-
|
36 |
-
print(f"正在上传 '{os.path.basename(file_path)}' 到 {upload_url}...")
|
37 |
-
|
38 |
-
# 发送 POST 请求
|
39 |
-
response = requests.post(upload_url, headers=headers, data=data, files=files)
|
40 |
-
|
41 |
-
# 打印服务器响应
|
42 |
-
print(f"\n服务器响应 ({response.status_code}):")
|
43 |
-
try:
|
44 |
-
print(response.json())
|
45 |
-
except requests.exceptions.JSONDecodeError:
|
46 |
-
print(response.text)
|
47 |
-
|
48 |
-
except IOError as e:
|
49 |
-
print(f"读取文件时出错: {e}")
|
50 |
-
except requests.exceptions.RequestException as e:
|
51 |
-
print(f"请求时出错: {e}")
|
52 |
-
|
53 |
-
|
54 |
-
if __name__ == "__main__":
|
55 |
-
parser = argparse.ArgumentParser(description="远程上传文件到您的个人网盘。")
|
56 |
-
parser.add_argument("file_path", help="要上传的文件的路径。")
|
57 |
-
parser.add_argument("--key", required=True, help="您的 API 密钥。")
|
58 |
-
parser.add_argument("--space", required=True, help="与此次上传关联的 Space ID。")
|
59 |
-
parser.add_argument("--server", default="http://127.0.0.1:5001", help="服务器的 URL 地址。 (默认: http://127.0.0.1:5001)")
|
60 |
-
|
61 |
-
args = parser.parse_args()
|
62 |
-
|
63 |
-
upload_file(args.server, args.key, args.space, args.file_path)
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|