import requests from urllib.parse import quote from requests_toolbelt import MultipartEncoder import os import math url = 'http://127.0.0.1:5244/api/auth/login' d = {'Username': 'admin', 'Password': 'admin'} r = requests.post(url, data=d) #print(r.text) import json data = json.loads(r.text) print('data',data,'\r\n', data.get('data').get('token')) token = data.get('data').get('token') url2 = "http://127.0.0.1:5244/api/fs/form" # file_path= '/Users/l/Desktop/想要成为影之实力者_第05集.mp4' #需要上传的文件 file_path= '/Users/l/Desktop/防御全开_第27集.mp4' #需要上传的文件 import hashlib def get_md5(path): m = hashlib.md5() with open(path, 'rb') as f: for line in f: m.update(line) md5code = m.hexdigest() return md5code def upload_slice_file(url, file_path,token): chunk_size = 1024*1024*2 #filename = file_path.split("\\")[-1:][0] filename = file_path.split('/')[-1] total_size = os.path.getsize(file_path) current_chunk = 1 total_chunk = math.ceil(total_size/chunk_size) while current_chunk <= total_chunk: start = (current_chunk - 1)*chunk_size end = min(total_size, start+chunk_size) with open(file_path, 'rb') as f: f.seek(start) file_chunk_data = f.read(end-start) data = MultipartEncoder( fields={ "filename": filename, "totalSize": str(total_size), "currentChunk": str(current_chunk), "totalChunk": str(total_chunk), "md5": get_md5(file_path), "file": (filename, file_chunk_data, 'application/octet-stream') } ) filename_new = quote(file_path,'utf-8') headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36", "Accept": "application/json", "Accept-Encoding": "gzip, deflate", "Connection": "keep-alive", 'Authorization':token, 'file-path':'/tianyi/test3/'+filename_new, "Content-Type": data.content_type } print('/tianyi/test3/'+filename_new) with requests.put(url, headers=headers, data=data) as response: # assert response.status_code == 200 print(response.text) print('current_chunk--', current_chunk) current_chunk = current_chunk + 1 upload_slice_file(url2, file_path,token)
https://www.cnblogs.com/zhuosanxun/p/15100588.html
菜鸟的自白
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!