python處理multipart/form-data的請求方法
更新時間:2018年12月26日 14:20:15 作者:hqzxsc2006
今天小編就為大家分享一篇python處理multipart/form-data的請求方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
方法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,不然可能會報錯
res = requests.post(url, files=files)
print res.request.body
print res.request.headers
方法2:
安裝requests_toolbelt
pip install requests-toolbelt
實現(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的請求方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于python使用Pillow做動態(tài)圖在圖中生成二維碼以及圖像處理
這篇文章主要介紹了基于python使用Pillow做動態(tài)圖在圖中生成二維碼以及圖像處理,分享pillow的一些簡單使用,喜歡的話大家可以參考文章內(nèi)容下去試試奧2022-02-02
python爬取企查查企業(yè)信息之selenium自動模擬登錄企查查
這篇文章主要介紹了python爬取企查查企業(yè)信息之自動模擬登錄企查查以及selenium獲取headers,selenium獲取cookie,需要的朋友可以參考下2021-04-04
Pandas實現(xiàn)復(fù)制dataframe中的每一行
這篇文章主要介紹了Pandas實現(xiàn)復(fù)制dataframe中的每一行方式,2024-02-02
Python+matplotlib實現(xiàn)填充螺旋實例
這篇文章主要介紹了Python+matplotlib實現(xiàn)填充螺旋實例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
python對數(shù)組進行排序,并輸出排序后對應(yīng)的索引值方式
今天小編就為大家分享一篇python對數(shù)組進行排序,并輸出排序后對應(yīng)的索引值方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02

