The online approach to sending Multipart/form-data using Python is mostly based on
Ulrlib2 's analog post method, as follows:
Import Urllib2
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 contentfr.close () Data.append ('--%s--\r\n '%boundary) httpbody= ' \ r \ n '. Join (data) print type (httpbody) Print httpbodypostdataurl= ' http ://xxxxxxxx ' Req=urllib2. Request (postdataurl,data=httpbody)
After testing, using the above method to send a binary file, the server error, data problems!
The problem is ' \ r \ n '. The code of the join (data), which has binary data inside it, may be a problem by converting the data to the UTF-8 format.
Search for a lot of data, you can use the requests Library to submit Multipart/form-data format
A multipart/form-data form data, caught in HTTP inside the following:
#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
< binary file data is not displayed >
---------------------------7df23df2a0870-
The above data in the requests can be simulated as:
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 ')
}
Send the above post request, which is a simple
Response=requests.post (Url,files=files)
It's so simple.
On the official website, requests simulates a form data format as follows:
Files = {' name ': (<filename>, <file object>,<content type>, <per-part headers>)}
This line simulates the post data as:
Content-disposition:form-data; Name= ' name ';filename=<filename>
Content-type: <content type>
<file object>
--boundary
If filename and Content-type are not written, then the data that responds to the analog post will not have both.
Often using requests does not automatically manage cookies as you would with URLLIB2, but if you get a cookie
The cookie can be sent out in a requests request.
The cookie format used by requests is as follows:
newcookie={}
newcookie[' key1 ']= ' value1 '
newcookie[' key2]= ' value2 '
newcookie[' Key3 ']= ' value3 '
Sending cookies can be used:
Response=requests.post (Url,cookies=newcookie)
That's it.
Send multipart/form-data requests using Python's requests