使用python的requests 發送multipart/form-data 請求,requestsmultipart

來源:互聯網
上載者:User

使用python的requests 發送multipart/form-data 請求,requestsmultipart

 網上關於使用python 的發送multipart/form-data的方法,多半是採用

ulrlib2 的類比post方法,如下:

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)


經過測試,使用上述方法發送一段二進位檔案的時候,伺服器報錯,資料有問題!

問題就出在    '\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)

這樣就可以了

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.