pycurl上传大文件

在Python中可以利用pycurl上传大文件,从而避免用requests库可能导致的memory error异常

import pycurl
from StringIO import StringIO

def upload_package_pycurl(self, file_path):
    buffer = StringIO()
    url = 'https://' + self.url
    curl = pycurl.Curl()
    curl.setopt(curl.POST, 1)
    curl.setopt(curl.URL, url)
    curl.setopt(curl.HTTPPOST, [("file", (curl.FORM_FILE, file_path))])
    curl.setopt(curl.HTTPHEADER, ['ACCESS-TOKEN:' + self.token])
    curl.setopt(curl.SSL_VERIFYHOST, False)
    curl.setopt(curl.SSL_VERIFYPEER, False)
    curl.setopt(curl.WRITEDATA, buffer)
    # curl.setopt(curl.VERBOSE, 1)
    curl.perform()
    print(buffer.getvalue())
    curl.close()

等价于下面命令:

# -s slient;-k insecure; -X method; -H header; -d data; -F file

curl -k -X POST -H "Content-Type:multipart/form-data" -H "ACCESS-TOKEN:${token}" -F "file=@${file_path}" https://${ip}:${port}/upload

 

pycurl模块常用方法:

https://www.jianshu.com/p/53e9082633e8 

你可能感兴趣的:(Python)