使用python的requests 发送multipart/form-data 请求
2016-11-01 21:24
2576 查看
点击打开链接
ulrlib2 的模拟post方法,如下:
import urllib2
[python] view
plain copy
boundary='-------------------------7df3069603d6'
data=[]
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="app_id"\r\n')
data.append('xxxxxx')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="version"\r\n')
data.append('xxxxx')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="platform"\r\n')
data.append('xxxxx')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="libzip"; filename="C:\Users\danwang3\Desktop\libmsc.zip"')
data.append('Content-Type: application/octet-stream\r\n')
fr=open('C:\Users\danwang3\Desktop\libmsc.zip')
content=fr.read()
data.append(content)
print content
fr.close()
data.append('--%s--\r\n'%boundary)
httpBody='\r\n'.join(data)
print type(httpBody)
print httpBody
postDataUrl='http://xxxxxxxx'
req=urllib2.Request(postDataUrl,data=httpBody)
经过测试,使用上述方法发送一段二进制文件的时候,服务器报错,数据有问题!
问题就出在 '\r\n'.join(data)的编码,data内部拥有二进制数据,通过这种编码,可能是把数据转换为utf-8格式,当然有问题。
搜索了很多资料,查到可以使用requests库提交multipart/form-data 格式的数据
一个multipart/form-data 的表单数据,在http里面抓包如下:
#Content-Disposition: form-data;name="app_id"
123456
#-----------------------------7df23df2a0870
#Content-Disposition: form-data;name="version"
2256
-----------------------------7df23df2a0870
Content-Disposition:form-data; name="platform"
ios
-----------------------------7df23df2a0870
Content-Disposition: form-data;name="libzip";filename="C:\Users\danwang3\Desktop\libmsc.zip"
Content-Type: application/x-zip-compressed
<二进制文件数据未显示>
---------------------------7df23df2a0870—
上述数据在requests里面可以模拟为:
files={'app_id':(None,'123456'),
'version':(None,'2256'),
'platform':(None,'ios'),
'libzip':('libmsc.zip',open('C:\Users\danwang3\Desktop\libmsc.zip','rb'),'application/x-zip-compressed')
}
发送上述post请求,也就是简单的
response=requests.post(url,files=files)
就这么简单
在官方网站上,requests模拟一个表单数据的格式如下:
files = {'name': (<filename>, <file object>,<content type>, <per-part headers>)}
这一行模拟出来的post数据为:
Content-Disposition: form-data; name=’name’;filename=<filename>
Content-Type: <content type>
<file object>
--boundary
如果filename 和 content-Type不写,那么响应模拟post的数据就不会有二者。
通常使用requests 不像使用urllib2那样可以自动管理cookie,不过如果获取到cookie
可以在requests请求里面一并将cookie发送出去
requests使用的cookie格式如下:
newCookie={}
newCookie['key1']='value1'
newCookie['key2]='value2'
newCookie['key3']='value3'
发送cookie可以使用:
response=requests.post(url,cookies=newCookie)
这样就可以了
ulrlib2 的模拟post方法,如下:
import urllib2
[python] view
plain copy
boundary='-------------------------7df3069603d6'
data=[]
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="app_id"\r\n')
data.append('xxxxxx')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="version"\r\n')
data.append('xxxxx')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="platform"\r\n')
data.append('xxxxx')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="libzip"; filename="C:\Users\danwang3\Desktop\libmsc.zip"')
data.append('Content-Type: application/octet-stream\r\n')
fr=open('C:\Users\danwang3\Desktop\libmsc.zip')
content=fr.read()
data.append(content)
print content
fr.close()
data.append('--%s--\r\n'%boundary)
httpBody='\r\n'.join(data)
print type(httpBody)
print httpBody
postDataUrl='http://xxxxxxxx'
req=urllib2.Request(postDataUrl,data=httpBody)
经过测试,使用上述方法发送一段二进制文件的时候,服务器报错,数据有问题!
问题就出在 '\r\n'.join(data)的编码,data内部拥有二进制数据,通过这种编码,可能是把数据转换为utf-8格式,当然有问题。
搜索了很多资料,查到可以使用requests库提交multipart/form-data 格式的数据
一个multipart/form-data 的表单数据,在http里面抓包如下:
#Content-Disposition: form-data;name="app_id"
123456
#-----------------------------7df23df2a0870
#Content-Disposition: form-data;name="version"
2256
-----------------------------7df23df2a0870
Content-Disposition:form-data; name="platform"
ios
-----------------------------7df23df2a0870
Content-Disposition: form-data;name="libzip";filename="C:\Users\danwang3\Desktop\libmsc.zip"
Content-Type: application/x-zip-compressed
<二进制文件数据未显示>
---------------------------7df23df2a0870—
上述数据在requests里面可以模拟为:
files={'app_id':(None,'123456'),
'version':(None,'2256'),
'platform':(None,'ios'),
'libzip':('libmsc.zip',open('C:\Users\danwang3\Desktop\libmsc.zip','rb'),'application/x-zip-compressed')
}
发送上述post请求,也就是简单的
response=requests.post(url,files=files)
就这么简单
在官方网站上,requests模拟一个表单数据的格式如下:
files = {'name': (<filename>, <file object>,<content type>, <per-part headers>)}
这一行模拟出来的post数据为:
Content-Disposition: form-data; name=’name’;filename=<filename>
Content-Type: <content type>
<file object>
--boundary
如果filename 和 content-Type不写,那么响应模拟post的数据就不会有二者。
通常使用requests 不像使用urllib2那样可以自动管理cookie,不过如果获取到cookie
可以在requests请求里面一并将cookie发送出去
requests使用的cookie格式如下:
newCookie={}
newCookie['key1']='value1'
newCookie['key2]='value2'
newCookie['key3']='value3'
发送cookie可以使用:
response=requests.post(url,cookies=newCookie)
这样就可以了
相关文章推荐
- 使用python的requests 发送multipart/form-data 请求
- Python3下requests库发送multipart/form-data类型请求
- python使用requests库提交multipart/form-data请求
- Python 使用requests发送POST请求
- 使用RestTemplate发送multipart/form-data格式的数据
- requests库发送multipart/form-data格式请求
- Python 使用requests发送POST请求
- 使用python2实现http multipart/form-data数据传输
- Multipart/form-data POST文件上传详解 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form me
- 实现使用HTTP协议发送multipart/form-data类型的HTTP表单
- 使用requests库提交multipart/form-data 格式的请求
- 实现使用HTTP协议发送multipart/form-data类型的HTTP表单
- Python 发送 multipart/form-data 数据
- python中使用requests 模拟浏览器发送请求数据
- Python使用requests发送POST请求实例代码
- python 开发模块之Requests-multipart/form-data
- Python发送form-data请求及拼接form-data内容的方法
- Python发送form-data请求及拼接form-data内容的方法
- python 发送上传文件multipart/form-data
- 实现使用HTTP协议发送multipart/form-data类型的HTTP表单