0

I have a python program that takes pictures and I am wondering how I would write a program that sends those pictures to a particular URL.

If it matters, I am running this on a Raspberry Pi.

(Please excuse my simplicity, I am very new to all this)

1
  • 1
    Probably using Requests Commented Jan 18, 2014 at 5:50

4 Answers 4

2

Many folks turn to the requests library for this sort of thing.

For something lower level, you might use urllib2

Sign up to request clarification or add additional context in comments.

Comments

1

I've been using the requests package as well. Here's an example POST from the requests documentation.

Comments

0

If you are feeling that you want to use CURL, try PyCurl.
Install it using:

sudo pip install pycurl

Here is an example of how to send data using it:

import pycurl
import json
import urllib
import cStringIO

url = 'your_url'
first_param = '12'
dArrayData = [{'data' : 'first'}, {'data':'second'}]
json_to_send = json.dumps(dArrayData, separators=(',',':'), sort_keys=False)

curlClient = pycurl.Curl()
curlClient.setopt(curlClient.USERAGENT, 'curl-user-agent')

# Sets the url of the service
curlClient.setopt(curlClient.URL, url)

# Sets the request to be of the type POST
curlClient.setopt(curlClient.POST, True)

# Sets the params of the post request
send_params = 'first_param=' + first_param + '&data=' + urllib.quote(json_to_send)
curlClient.setopt(curlClient.POSTFIELDS, send_params)

# Setting the buffer for the response to be written to
bufResponse = cStringIO.StringIO()
curlClient.setopt(curlClient.WRITEFUNCTION, bufResponse.write)

# Setting to fail on error
curlClient.setopt(curlClient.FAILONERROR, True)

# Sets the time out for the connections
curlClient.setopt(pycurl.CONNECTTIMEOUT, 25)
curlClient.setopt(pycurl.TIMEOUT, 25)

response = ''

try:
    # Performs the operation
    curlClient.perform()

except pycurl.error as err:
    errno, errString = err
    print '========'
    print 'ERROR sending the data:'
    print '========'
    print 'CURL error code:', errno
    print 'CURL error Message:', errString
else:
    response = bufResponse.getvalue()
    # Do what ever you want with the response.. Json it or what ever..
finally:
    curlClient.close()
    bufResponse.close()

Comments

0

The requests library is most supported and advanced way to do this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.