我们在上一篇博文中详细介绍了这个网站,那么今天我们就来用程序来实现如何将本地的文件上传到我们的网盘。因为这个网盘在国外的环境下也可以访问,所以这也可以当做我们的一个传输的中介进行国外网站程序的下载。
关于send.cm使用方法的博客
最终目标
实现程序
我们首先使用访问我们的网站,接着我们使用我们的密钥进行访问,然后最后用POST上传程序,最后用我们的函数返回网址(下次等我有时间之后我会详细说明)
# 程序名称 : SMDisk.py
# 运行版本 : Python 3.7
# 运行系统 : Windows 10
# 运行需求 : Pyanywhere 本地
# 制作日期 : 2021年3月4日
# 制作人 : ZesenYuan
import requests
import json
def test_file_address(path):
# 禁止上传的文件格式
endswith = ["php","pl","cgi","py","sh","shtm","xap","swf","phtml","asax","cer","asa","cgi","php5","php6","php7","pht"]
# 将列表转换成元组
endswith_tuple = tuple(endswith)
# 获取是否在我们的元组中
bool_endswith = path.endswith(endswith_tuple)
# 检测判断
if bool_endswith == "True":
endswith_real = "False"
return endswith_real
else:
endswith_real = "True"
return endswith_real
# 定义一个默认的密钥进行访问
def upload_file_api(file,key = "xxxx"):
print("检测格式中...")
endswith = test_file_address(file)
# 检测我们是否为禁止上传的文件类型
if endswith == "True":
print("文件支持...")
else:
print("不支持的格式!")
exit()
# 定义一个会话,多次访问这个软件
file_api_session = requests.session()
# 在访问时候加上我们的密钥
file_api = file_api_session.get("https://send.cm/api/upload/server?key=" + key)
print("正在检测密钥...")
# print(file_api.text)
# 将我们的响应值转换成文本
file_api = file_api.text
# 我们的访问值是json文件格式,所以我们将其转换成Json
file_api_json = json.loads(file_api)
print("密钥正确...")
# 我们取出我们相应的ID值和请求时间,结果
api_result = file_api_json.get("result")
api_time = file_api_json.get("server_time")
api_id = file_api_json.get("sess_id")
# print(api_result,api_time,api_id)
# 解析相关代码,构造请求Data表单
files = {'file':open(file,'rb')}
upload_data={"sess_id":api_id}
print("准备文件成功...")
# 上传代码
print("开始上传...")
upload_file = file_api_session.post(api_result,upload_data,files=files)
upload_file_text = upload_file.text
print("上传成功...")
# 转换成我们的Json文件
upload_file_text_json = json.loads(upload_file_text)
# 获取列表中的第一项
upload_file_text_json = upload_file_text_json[0]
# 获取我们服务器的响应值
file_status = upload_file_text_json.get("file_status")
if file_status == "OK":
# 获取我们的存储链接
file_code = upload_file_text_json.get("file_code")
# 组合我们的存储链接
finally_address = "https://send.cm/"+file_code
else:
finally_address = None
print("最终链接组成成功...")
# 返回最终的结果
return finally_address,api_time
website = upload_file_api("C:\\Users\\Administrator\\Desktop\\IDM-Send专用版本.exe")
print(website)
之前我有一个详细讲关于OS检测后缀名的程序,那么建议访问查看其原理