Python: uploading file via HTTP with pyCurl and Requests

Here is a simple way to upload file in Python. You should have PyCurl installed.

import pycurl
from cStringIO import StringIO
c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.HTTPPOST, [('title', 'test'), (('file', (c.FORM_FILE, filename)))])
c.setopt(c.VERBOSE, 1)
bodyOutput = StringIO()
headersOutput = StringIO()
c.setopt(c.WRITEFUNCTION, bodyOutput.write)
c.setopt(c.URL, "" )
c.setopt(c.HEADERFUNCTION, headersOutput.write)
print bodyOutput.getvalue()

File upload with Requests by Kenneth Reitz:

import requests
f = open (filename)
r ='', data =  {'title':'test_file},  files =  {'file':f})
print r.status_code
print r.headers

Leave a Reply

Your email address will not be published.