http - python requests post 大文件和获取进度条
PHP中文网
PHP中文网 2017-04-17 18:01:34
0
3
746

准备用HTTP 在局域网内 上传一些文件,文件一般都在1G左右

r = requests.post('****',
                 data={'path':'2016/07/08/5ASD5SDFASDFASDF/cad.zip'},
                 files={'file': open(filename, 'rb')}
                 )
                     

这样上传小文件可以, 但是上传大文件时候会py会报 memeryError的内存错误。
如何解决此问题呢?

PHP中文网
PHP中文网

认证高级PHP讲师

全部回覆(3)
迷茫
from requests_toolbelt import  *

m = MultipartEncoder(fields={'file': ('filename',open(f, 'rb'))},

                     boundary='---------------------------7de1ae242c06ca'
                    )

import time

def my_callback(monitor):
    # Your callback function
    print monitor.bytes_read

m = MultipartEncoderMonitor(m, my_callback)

req_headers = {'Content-Type': m.content_type,
               'path':'2016/07/09/5ASD5SDFASDFASDF/{}.zip'.format(time.time()),}

r = requests.post(url, data=m, headers=req_headers)

用了一個擴充函式庫,可以完美解決2個問題 requests_toolbelt

洪涛

建設樓主使用requests的串流上傳。
以下是一些說明

Streaming Uploads

Requests supports streaming uploads, which allow you to send large streams or files without reading them into memory. To stream and upload, simply provide a file-like object for your body:

with open('massive-body', 'rb') as f:
    requests.post('http://some.url/streamed', data=f)

http://www.python-requests.org/en/master/user/advanced/#streaming-uploads

Peter_Zhu

推薦你使用poster模組
https://atlee.ca/software/poster/

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!