最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年12月26日 14:20:15   作者:hqzxsc2006  
今天小編就為大家分享一篇python處理multipart/form-data的請(qǐng)求方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

方法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)文章

最新評(píng)論

醴陵市| 房山区| 工布江达县| 柳州市| 交城县| 兴和县| 桃江县| 开封市| 五寨县| 化隆| 保亭| 英德市| 天长市| 英超| 桂东县| 南木林县| 长宁区| 若尔盖县| 伊吾县| 宜丰县| 卢氏县| 大埔区| 高密市| 千阳县| 合山市| 巴里| 临泉县| 嘉善县| 鄱阳县| 巧家县| 加查县| 平乡县| 新绛县| 洪泽县| 洛隆县| 德钦县| 普兰县| 芒康县| 许昌县| 循化| 丽水市|