jq post 表单提交文件_一文看懂 requests 库文件上传

很多小伙伴在使用 python requests 库时不知道怎么上传图片或其他文件,今天小编就带大家一步一个脚印,搞定各种文件上传。

预热知识

requests 库的基本使用

前面我们讲过 requests 库的使用,这里就不再介绍了,需要的小伙伴可以参考:5分钟入门一个python库——requests

文件上传编码类型

在文件上传时,所使用的编码类型是multipart/form-data,属于表单数据,它既可以发送文本数据,也支持二进制数据上传,另外还有一种表单数据编码类型是application/x-www-form-urlencoded,是表单数据向服务器提交时所采用的默认编码类型,这两者的区别是:

x-www-form-urlencoded: 会把表单数据转换成一个字串,如:

name1=value1&name2=value2...
6e86f3943251f50fd23ee5c6af4be797.png

multipart/form-data: 会把整个表单以控件为单位分割,并为每个部分加上:

Content-Disposition ( form-data 或者file ),
Content-Type(默认为text/plain),
name(控件名称)等信息,并加上分割符(boundary)

bf3833528812bd48c91f306b2e9b1ab4.png07438bcc468d6f24662c4dfdc6ce6aa0.png

requests files 参数详解

files = {'file': open(r'001.jpg', 'rb')}
response = requests.post('http://httpbin.org/post', files=files)

上传文件时需要使用到 files这个参数(必须是一个字典),才能使表单数据以 multipart/form-data编码类型发送,我们在传 files参数是可以有多种形式:

name: file-like-objects (不仅仅支持 open() 打开文件,还可以是其他的 IO 流)

files = {'file': open(r'001.jpg', 'rb')}

name: file-tuple

# file-tuple = ('filename', fileobj)
files = {'file': ('myfile.jpg', open(r'001.jpg', 'rb'))}

# or file-tuple = ('filename', fileobj, 'content_type')
files = {'file': ('myfile.jpg', open(r'001.jpg', 'rb'), 'image/jpeg')}

# or file-tuple = ('filename', fileobj, 'content_type', custom_headers) 
files = {'file': ('myfile.jpg', open(r'001.jpg', 'rb'), 'image/jpeg', , {'Custom-Headers': '123456'})}

注意这里的 custom_headers 不会放到请求 headers 中去, 而是放每一个分隔块中

import requests

files = {'file': ('myfile.jpg', open(r'001.jpg', 'rb'), 'image/jpeg', , {'Custom-Headers': '123456'})}
response = requests.post('http://httpbin.org/post', files=files)
Content-Type: multipart/form-data; boundary=51dac4ba893e0423801fc2d08ea44f6f

--51dac4ba893e0423801fc2d08ea44f6f
Content-Disposition: form-data; name="file"; filename="myfile.jpg"
Content-Type: image/jpeg
Custom-Headers: 123456

其他案例

1. 下载文件并上传
download = requests.get('http://www.httpbin.org/image/jpeg')
files = {'file': ('myfile.jpg', download.content, 'image/jpeg')}
response = requests.post('http://httpbin.org/post', files=files}
2. 为文件添加 Mimetype
file_path = './001.jpg'
files = {'file': ('myfile.jpg', open(file_path, 'rb'), mimetypes.guess_type(image)[0])}
response = requests.post('http://httpbin.org/post', files=files}
>>>import mimetypes
>>>mimetypes.guess_type('./001.jpg')
('image/jpeg', None)
3. 既有 data 参数又有 files 参数
data = {'image_type': 1}
files = {'image_file': open(r'img.jpg', 'rb')}
response = requests.post('http://httpbin.org/post', data=data, files=files}
13cacced680501c060d6c9479196cf2b.png
4. 发送作为文件来接收的字符串
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
response = requests.post('http://httpbin.org/post', files=files}

b6202ce054d6a46e6a96cac7aef98eb1.png

嘿!你还在看吗 dff757b0f35271a0f66a6a671a49d8d0.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值