網(wǎng)站首頁 編程語言 正文
python 使用第三方庫requests-toolbelt 上傳文件流,內(nèi)容如下所示:
# pip install requests-toolbelt 使用第三方庫上傳文件流
from requests_toolbelt.multipart.encoder import MultipartEncoder
補充:Python使用requests和requests_toolbelt上傳文件
一、文件上傳(Form 表單方式)【先將文件讀取至內(nèi)存中,再將內(nèi)存中的文件信息上傳至服務(wù)器】
1、單文件上傳
①文件上傳代碼,運行后logo.png文件上傳至服務(wù)器:
import requests
files = {'file1': open('logo.png', 'rb')}
response = requests.post('http://www.hangge.com/upload.php', files=files)
print(response.text)
②顯式地設(shè)置文件名,文件類型和請求頭:
import requests
files = {'file1':
('logo.png', # 文件名
open('logo.png', 'rb'), # 文件流
'image/png', # 請求頭Content-Type字段對應(yīng)的值
{'Expires': '0'})
}
response = requests.post('http://www.hangge.com/upload.php', files=files)
print(response.text)
2、多文件上傳
①有時需要在一個請求中同時發(fā)送多個文件,同樣使用files參數(shù)傳入一個數(shù)組即可:
import requests
files = [
('file1', ('1.png', open('logo.png', 'rb'), 'image/png')),
('file2', ('2.png', open('logo.png', 'rb'), 'image/png'))
]
response = requests.post('http://www.hangge.com/upload.php', files=files)
print(response.text)
3、上傳文件時需要附帶其它參數(shù)
①如果我們需要在上傳文件的同時傳遞一些其它參數(shù),也是可以的:
import requests
data = {
"name": "hangge.com",
"age": 100
}
files = [
('file1', ('1.png', open('logo.png', 'rb'), 'image/png')),
('file2', ('2.png', open('logo.png', 'rb'), 'image/png'))
]
response = requests.post('http://www.hangge.com/upload.php', data=data, files=files)
print(response.text)
二、流式上傳文件【邊讀取文件邊上傳文件】
1、requests-toolbelt 擴展庫
①有時我們需要上傳一個非常大的文件(比如1G左右),如果像上面的方式直接使用Requests提交,可能會造成內(nèi)存不足而崩潰。
②所以發(fā)送大文件時還是建議將請求做成數(shù)據(jù)流。不過默認(rèn)情況下Requests不支持流式上傳,但有個第三方包requests-toolbelt 是支持的(本質(zhì)還是multipart/form-data上傳)
③requests-toolbelt是python請求的實用程序集合。
2、下載安裝requests-toolbelt第三方庫
pip install requests-toolbelt
3、使用流式上傳文件:
實例:使用requests-toolbelt 來實現(xiàn)文件的流式上傳:
①不同于requests全部讀到內(nèi)存中上傳,requests-toolbelt是邊讀邊上傳。
②其本質(zhì)還是multipart/form-data 方式提交數(shù)據(jù),所以服務(wù)端代碼不需要變化。
import requests
from requests_toolbelt import MultipartEncoder
# 邊讀取文件邊上傳文件
m = MultipartEncoder(
fields={'name': 'logo.com', # 字段1
"age": '100', # 字段2
'file1': ('1.png', open('logo.png', 'rb'), 'image/png'), # 文件1
'file2': ('2.png', open('logo.png', 'rb'), 'image/png') # 文件2
}
)
r = requests.post('http://www.hangge.com/upload.php', data=m, headers={'Content-Type': m.content_type})
print(r.text)
4、監(jiān)聽上傳進度
①requests-toolbelt庫還提供了個監(jiān)視器MultipartEncoderMonitor,該監(jiān)視器接受一個回調(diào)函數(shù),我們可以在回調(diào)中實時跟蹤進度。
import requests
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
def my_callback(monitor):
progress = (monitor.bytes_read / monitor.len) * 100
print("\r 文件上傳進度:%d%%(%d/%d)" % (progress, monitor.bytes_read, monitor.len), end=" ")
e = MultipartEncoder(
fields={'name': 'logo.com', # 參數(shù)1
"age": '100', # 參數(shù)2
'file1': ('1.png', open('logo.png', 'rb'), 'image/png'), # 文件1
'file2': ('2.png', open('logo.png', 'rb'), 'image/png') # 文件2
}
)
m = MultipartEncoderMonitor(e, my_callback)
r = requests.post('http://www.hangge.com/upload.php', data=m, headers={'Content-Type': m.content_type})
print(r.text)
②運行效果如下,可以看到提交過程中會實時顯示進度:
原文鏈接:https://www.cnblogs.com/hanfanfan/p/16711582.html
相關(guān)推薦
- 2023-03-15 Pandas操作兩個Excel實現(xiàn)數(shù)據(jù)對應(yīng)行的合并_python
- 2022-06-30 Python使用Pillow添加水印_python
- 2023-03-26 WPF實現(xiàn)頁面的切換的示例代碼_C#教程
- 2023-01-05 Qt操作SQLite數(shù)據(jù)庫的教程詳解_C 語言
- 2022-10-04 Android實現(xiàn)圓圈倒計時_Android
- 2022-10-23 Android文件存儲SharedPreferences源碼解析_Android
- 2023-06-17 詳解Flask數(shù)據(jù)庫的連接與使用_python
- 2022-11-09 PostgreSQL索引失效會發(fā)生什么_PostgreSQL
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支