需求:对方提供处理文件的接口,本地将待处理文件压缩后,通过http post multipart方式上传,等待处理完成后从相应连接下载结果
代码:
import os import time import zipfile import requests from requests_toolbelt.multipart.encoder import MultipartEncoder class Func4Fuxi(object): def __init__(self): self.remote_result = 0 # 压缩文件 def zip_dir(self, dirname, zipfilename): filelist = [] if os.path.isfile(dirname): filelist.append(dirname) else: for root, dirs, files in os.walk(dirname): for name in files: filelist.append(os.path.join(root, name)) zf = zipfile.ZipFile(zipfilename, mode="w", compression=zipfile.zlib.DEFLATED, allowZip64=True) for tar in filelist: arcname = tar[len(dirname):] zf.write(tar, arcname) zf.close() # 解压文件 def unzip_file(self, zipfilename, unziptodir): if not os.path.exists(unziptodir): os.mkdir(unziptodir) zfobj = zipfile.ZipFile(zipfilename) for name in zfobj.namelist(): name = name.replace('\\', '/') if name.endswith('/'): os.mkdir(os.path.join(unziptodir, name)) else: ext_filename = os.path.join(unziptodir, name) ext_dir = os.path.dirname(ext_filename) if not os.path.exists(ext_dir): os.mkdir(ext_dir) outfile = open(ext_filename, 'wb') outfile.write(zfobj.read(name)) outfile.close() # 下载 def download_result(self, filename): filename.replace('\\', '/') file = filename.split('/')[-1] URL = '--------------' re = requests.get(URL+'?name='+file, stream=True) self.remote_result = re.status_code if self.remote_result == 200: print("find result,try to download") f = open("download_"+file, "wb") for chunk in re.iter_content(chunk_size=512): if chunk: f.write(chunk) print("download result success") return self.remote_result # 上传 def upload_zip(self, filename): self.remote_result = 0 filename.replace('\\', '/') file = filename.split('/')[-1] file_tup = (file, open(filename, 'rb'), 'application/zip') URL = '-----------------' #fields属性根据对方接口说明设置 m = MultipartEncoder( fields={'name': file, 'zipfile': file_tup} ) re = requests.post(URL, data=m, headers={'Content-Type': m.content_type}) self.remote_result = re.status_code if self.remote_result == 200: print("upload success") else: print("upload failed") return self.remote_result
补充知识:Python模拟浏览器上传文件脚本(Multipart/form-data格式)
http协议本身的原始方法不支持multipart/form-data请求,这个请求由原始方法演变而来的。
multipart/form-data的基础方法是post,也就是说是由post方法来组合实现的,与post方法的不同之处:请求头,请求体。
multipart/form-data的请求头必须包含一个特殊的头信息:
Content-Type,且其值也必须规定为multipart/form-data,同时还需要规定一个内容分割符用于分割请求体中的多个post的内容,如文件内容和文本内容自然需要分割开来,不然接收方就无法正常解析和还原这个文件了。
具体的头信息如下:
Content-Type: multipart/form-data; boundary=${bound}
实例:
import os, random, sys, requests from requests_toolbelt.multipart.encoder import MultipartEncoder url = 'http://127.0.0.1/sendmsg' argvstr = sys.argv[1:] argv_dict = {} for argv in argvstr : argv = str(argv).replace("\r\n" , "") DICT = eval(argv) argv_dict.update(DICT) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0', 'Referer': url } multipart_encoder = MultipartEncoder( fields={ 'username': argv_dict['username'], 'pwd': argv_dict['pwd'], 'type': 'txt', 'friendfield': argv_dict['friendfield'], 'friend': argv_dict['friend'], 'content': argv_dict['content'], 'file': (os.path.basename(argv_dict['file']) , open(argv_dict['file'], 'rb'), 'application/octet-stream') #file为路径 }, boundary='-----------------------------' + str(random.randint(1e28, 1e29 - 1)) ) headers['Content-Type'] = multipart_encoder.content_type #请求头必须包含一个特殊的头信息,类似于Content-Type: multipart/form-data; boundary=${bound} r = requests.post(url, data=multipart_encoder, headers=headers) print(r.text) #注意,不要设置cookies等其他参数,否则会报错 # 例子/usr/local/python36/bin/python3 /opt/lykchat/test_upload.py "{'username':'lykchat','pwd':'123456','type':'img','friendfield':'1','friend':'xxxx','content':'恭喜发财','file':'/root/b.jpg'}" #等同于curl -F "file=@/root/a" 'http://127.0.0.1/sendmsg?username=lykchat&pwd=123456&type=img&friendfield=1&friend=xxxx&content=恭喜发财'
以上这篇python MultipartEncoder传输zip文件实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/184258/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)