国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python處理multipart/form-data的請(qǐng)求方法

2020-01-04 13:41:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

方法1:

import requestsurl = "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.bodyprint res.request.headers

方法2:

安裝requests_toolbelt

pip install requests-toolbelt

實(shí)現(xiàn)代碼

a.發(fā)送文件中的數(shù)據(jù)

from requests_toolbelt import MultipartEncoderimport requestsm = 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 MultipartEncoderimport requestsm = 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è)參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 定远县| 漾濞| 德兴市| 区。| 阜城县| 藁城市| 安图县| 静乐县| 麻栗坡县| 什邡市| 通海县| 伊金霍洛旗| 南部县| 梅州市| 呈贡县| 贞丰县| 富宁县| 会昌县| 乌拉特前旗| 铜鼓县| 科技| 中西区| 册亨县| 金平| 武胜县| 客服| 凤凰县| 磴口县| 临潭县| 江山市| 那曲县| 天等县| 郯城县| 西峡县| 资中县| 台南县| 东明县| 鲁甸县| 洛隆县| 麦盖提县| 若尔盖县|