python處理multipart/form-data的請(qǐng)求方法
方法1:
import requests
url = "http://www.xxxx.net/login"
#參數(shù)拼湊,附件上傳格式如picurl參數(shù),其他表單參數(shù)值拼成tuple格式:
2-tuples (filename, fileobj),
3-tuples (filename, fileobj, contentype),
4-tuples (filename, fileobj, contentype, custom_headers)
files = {"username": (None, "billy"), "password": (None, "abcd1234"),
'picUrl': ('pic.png', open('E:\\download\\pic.png', 'rb'), 'image/png')}
#如需headers,不需要賦值Content-Type,不然可能會(huì)報(bào)錯(cuò)
res = requests.post(url, files=files)
print res.request.body
print res.request.headers
方法2:
安裝requests_toolbelt
pip install requests-toolbelt
實(shí)現(xiàn)代碼
a.發(fā)送文件中的數(shù)據(jù)
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(
fields={'field0': 'value', 'field1': 'value',
'field2': ('filename', open('file.py', 'rb'), 'text/plain')},
)
r = requests.post('http://httpbin.org/post', data=m,
headers={'Content-Type': m.content_type})
b.不需要文件
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(fields={'field0': 'value', 'field1': 'value'})
r = requests.post('http://httpbin.org/post', data=m,
headers={'Content-Type': m.content_type})
以上這篇python處理multipart/form-data的請(qǐng)求方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
用Python獲取智慧校園每日課表并自動(dòng)發(fā)送至郵箱
很多小伙伴們都在為查看智慧校園課表而煩惱,今天特地整理了這篇文章,不僅可以用Python獲取智慧校園每日課表,還會(huì)自動(dòng)發(fā)至你郵箱,需要的朋友可以參考下2021-05-05
基于python使用Pillow做動(dòng)態(tài)圖在圖中生成二維碼以及圖像處理
這篇文章主要介紹了基于python使用Pillow做動(dòng)態(tài)圖在圖中生成二維碼以及圖像處理,分享pillow的一些簡(jiǎn)單使用,喜歡的話大家可以參考文章內(nèi)容下去試試奧2022-02-02
python爬取企查查企業(yè)信息之selenium自動(dòng)模擬登錄企查查
這篇文章主要介紹了python爬取企查查企業(yè)信息之自動(dòng)模擬登錄企查查以及selenium獲取headers,selenium獲取cookie,需要的朋友可以參考下2021-04-04
Pandas實(shí)現(xiàn)復(fù)制dataframe中的每一行
這篇文章主要介紹了Pandas實(shí)現(xiàn)復(fù)制dataframe中的每一行方式,2024-02-02
Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例
這篇文章主要介紹了Python+matplotlib實(shí)現(xiàn)填充螺旋實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
python對(duì)數(shù)組進(jìn)行排序,并輸出排序后對(duì)應(yīng)的索引值方式
今天小編就為大家分享一篇python對(duì)數(shù)組進(jìn)行排序,并輸出排序后對(duì)應(yīng)的索引值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python HTTP庫(kù) requests 的簡(jiǎn)單使用詳情
requests是Python的一個(gè)HTTP客戶端庫(kù),基于urllib標(biāo)準(zhǔn)庫(kù),在urllib標(biāo)準(zhǔn)庫(kù)的基礎(chǔ)上做了高度封裝,因此更加簡(jiǎn)潔好用,下面就由小編來(lái)給大家詳細(xì)介紹吧,需要的朋友可以參考下2021-09-09

