python requests 上传文件_requests上传文件

很久没看requests了,浏览了下官方文档

import requests

url = 'http://httpbin.org/post'

#files with defaultname with params

# files = {'file': ('report11.xls', open('inventory_detail.xlsx', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

#make string to be file

files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}

r = requests.post(url, files=files)

print r.text

files with defaultname with params

{

"args": {},

"data": "",

"files": {

"file": "data:application/vnd.ms-excel;base64,UEsDBBQABgAIAAAAIQBKc9LYbwEAACgGAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAZG9jUHJvcHMvYXBwLnhtbFBLBQYAAAAADQANAFIDAABjJAAAAAA="

},

"form": {},

"headers": {

"Accept": "*/*",

"Accept-Encoding": "gzip, deflate",

"Connection": "close",

"Content-Length": "10387",

"Content-Type": "multipart/form-data; boundary=ba74c1d92ef64feb9186ffaad249e144",

"Host": "httpbin.org",

"User-Agent": "python-requests/2.17.3"

},

"json": null,

"origin": "210.176.98.222",

"url": "http://httpbin.org/post"

}

make string to be file

{

"args": {},

"data": "",

"files": {

"file": "some,data,to,send\nanother,row,to,send\n"

},

"form": {},

"headers": {

"Accept": "*/*",

"Accept-Encoding": "gzip, deflate",

"Connection": "close",

"Content-Length": "184",

"Content-Type": "multipart/form-data; boundary=e7e9e37ed0d348219932a63e95e89188",

"Host": "httpbin.org",

"User-Agent": "python-requests/2.17.3"

},

"json": null,

"origin": "210.176.98.222",

"url": "http://httpbin.org/post"

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值