亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python處理multipart/form-data的請求方法

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

方法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,不然可能會報(bào)錯
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的請求方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論