2013-10-27 47 views
0

我需要发送重复的数据请求。正如我听到我不能发送重复,因为请求使用字典,我不能在字典中得到重复。如何用python中的重复数据发送请求?

什么,我需要获得(来自小提琴手嗅日志)

------WebKitFormBoundaryJm0Evrx7PZJnQkNw 
Content-Disposition: form-data; name="file[]"; filename="" 
Content-Type: application/octet-stream 


------WebKitFormBoundaryJm0Evrx7PZJnQkNw 
Content-Disposition: form-data; name="file[]"; filename="qwe.txt" 
Content-Type: text/plain 

example content of file qwe.txt blablabla 

我的脚本:

requests.post(url, files={'file[]': open('qwe.txt','rb'), 'file[]':''}) 

=>只拿到这个(日志从提琴手)。一个文件[]消失。

--a7fbfa6d52fc4ddd8b82ec8f7055c88b 
Content-Disposition: form-data; name="file[]"; filename="qwe.txt" 

example content of file qwe.txt blablabla 

我想:

requests.post(url, data={"file[]":""},files={'file[]': open('qwe.txt','rb')}) 

但其不:文件名= “” 作为内容型

--a7fbfa6d52fc4ddd8b82ec8f7055c88b 
Content-Disposition: form-data; name="file[]" 

--a7fbfa6d52fc4ddd8b82ec8f7055c88b 
Content-Disposition: form-data; name="file[]"; filename="qwe.txt" 
Content-Type: text/plain 

example content of file qwe.txt blablabla 

有什么办法在python-请求手动添加呢?

回答

1

requests 1.1.0开始,您可以使用元组列表而不是字典作为files参数传递。在每个元组中的第一个元素是提交的多部分形式的名称,可以由内容遵循任一,或通过选择含有文件名,内容和(任选地)内容类型的另一元组,那么你的情况:

files = [('file[]', ("", "", "application/octet-stream")), 
     ('file[]', ('qwe.txt', open('qwe.txt','rb'), 'text/plain'))] 
requests.post(url, files=files) 

应该产生你描述的结果。

+0

喜欢它,它的工作原理!谢谢你,你太棒了! – Emily

相关问题