python?使用第三方庫requests-toolbelt?上傳文件流的示例
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)存中的文件信息上傳至服務器】
1、單文件上傳
①文件上傳代碼,運行后logo.png文件上傳至服務器:
import requests
files = {'file1': open('logo.png', 'rb')}
response = requests.post('http://www.hangge.com/upload.php', files=files)
print(response.text)②顯式地設置文件名,文件類型和請求頭:
import requests
files = {'file1':
('logo.png', # 文件名
open('logo.png', 'rb'), # 文件流
'image/png', # 請求頭Content-Type字段對應的值
{'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ù)流。不過默認情況下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ù),所以服務端代碼不需要變化。
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)②運行效果如下,可以看到提交過程中會實時顯示進度:

到此這篇關于python使用第三方庫requests-toolbelt上傳文件流的文章就介紹到這了,更多相關python上傳文件流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
tensorflow實現(xiàn)殘差網(wǎng)絡方式(mnist數(shù)據(jù)集)
這篇文章主要介紹了tensorflow實現(xiàn)殘差網(wǎng)絡方式(mnist數(shù)據(jù)集),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python matplotlib畫圖與中文設置操作實例分析
這篇文章主要介紹了Python matplotlib畫圖與中文設置操作,結(jié)合實例形式分析了Python使用matplotlib進行圖形繪制及中文設置相關操作技巧,需要的朋友可以參考下2019-04-04
python遍歷迭代器自動鏈式處理數(shù)據(jù)的實例代碼
迭代器也是用來遍歷對象成員的,下面這篇文章主要給大家介紹了關于python遍歷迭代器自動鏈式處理數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-01-01

