python使用requests實(shí)現(xiàn)發(fā)送帶文件請(qǐng)求功能
1. requests發(fā)送文件功能
Requests 使得上傳多部分編碼文件變得很簡(jiǎn)單
url = 'http://httpbin.org/post'
files = {'file': open('D:/APPs.png', 'rb')}
r = requests.post(url, files=files)
print(r.text)你可以顯式地設(shè)置文件名,文件類型和請(qǐng)求頭:
url = 'http://httpbin.org/post'
files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url, files=files)
print(r.text)如果你想,你也可以發(fā)送作為文件來(lái)接收的字符串:
url = 'http://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files)
print(r.text)如果你發(fā)送一個(gè)非常大的文件作為 multipart/form-data 請(qǐng)求,你可能希望將請(qǐng)求做成數(shù)據(jù)流。默認(rèn)下 requests 不支持, 但有個(gè)第三方包 requests-toolbelt 是支持的。你可以閱讀 toolbelt 文檔 來(lái)了解使用方法。
2. requests發(fā)送多個(gè)文件請(qǐng)求
只要把文件設(shè)到一個(gè)元組的列表中,其中元組結(jié)構(gòu)為 (form_field_name, file_info)
按照如下格式發(fā)送數(shù)據(jù)
data = {'ts_id': tsid}
files = [('images',('1.png', open('/home/1.png', 'rb'),'image/png')),('images',('2.png', open('/home/2.png', 'rb'),'image/png'))]
r = requests.post(url, data=data, files=files)
print r.text3. Django 接收文件
附帶介紹Django里面如何接收?qǐng)D片文件數(shù)據(jù):
讀取文件:
from werkzeug.utils import secure_filename
def upload_file(request):
if request.method == 'POST':
uploaded_files = request.FILES.getlist("images")
try:
for file in uploaded_files:
filename = secure_filename(file.name)
handle_uploaded_file(os.path.join(ft, filename), file)
except Exception as e:
result_json = {"msg": str(e)}
result = {
'json': result_json
}
return JsonResponse(result, safe=False)保存文件:
def handle_uploaded_file(filename, f):
try:
destination = open(filename, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
except Exception as e:
raise Exception('save %s failed: %s' % (filename, str(e)))requests 官網(wǎng):http://docs.python-requests.org/zh_CN/latest/user/quickstart.html#post-multipart-encoded
到此這篇關(guān)于python使用requests實(shí)現(xiàn)發(fā)送帶文件請(qǐng)求的文章就介紹到這了,更多相關(guān)python requests發(fā)送文件請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用sftp實(shí)現(xiàn)上傳和下載功能
這篇文章主要為大家詳細(xì)介紹了Python使用sftp實(shí)現(xiàn)上傳和下載功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
python獲取元素在數(shù)組中索引號(hào)的方法
這篇文章主要介紹了python獲取元素在數(shù)組中索引號(hào)的方法,實(shí)例分析了Python中index方法的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
Python Selenium操作Cookie的實(shí)例方法
在本篇文章里小編給大家整理的是一篇關(guān)于Python Selenium操作Cookie的實(shí)例方法,有需要的朋友們可以學(xué)習(xí)參考下。2021-02-02
Python進(jìn)程間通訊與進(jìn)程池超詳細(xì)講解
進(jìn)程彼此之間互相隔離,要實(shí)現(xiàn)進(jìn)程間通信(IPC),multiprocessing模塊主要通過(guò)隊(duì)列方式,隊(duì)列:隊(duì)列類似于一條管道,元素先進(jìn)先出,需要注意的一點(diǎn)是:隊(duì)列都是在內(nèi)存中操作,進(jìn)程退出,隊(duì)列清空,另外,隊(duì)列也是一個(gè)阻塞的形態(tài)2022-12-12
Python Pygame實(shí)戰(zhàn)之趣味籃球游戲的實(shí)現(xiàn)
這篇文章主要為大家分享了一個(gè)基于Python和Pygame實(shí)現(xiàn)的一個(gè)趣味籃球游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-04-04
Python使用嵌套循環(huán)實(shí)現(xiàn)圖像處理算法
這篇文章主要給大家詳細(xì)介紹Python如何使用嵌套循環(huán)實(shí)現(xiàn)圖像處理算法,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07

