卷曲等效于Python

时间:2014-01-18 05:46:52

标签: python linux curl

我有一个拍摄照片的python程序,我想知道如何编写将这些图片发送到特定URL的程序。

如果重要的话,我在Raspberry Pi上运行它。

(请原谅我的简单,我对这一切都很陌生)

4 个答案:

答案 0 :(得分:1)

许多人为了这类事情而转向requests库。

对于较低级别的内容,您可以使用urllib2

答案 1 :(得分:1)

我一直在使用请求包。这是请求文档中的example POST

答案 2 :(得分:0)

如果您觉得要使用CURL,请尝试PyCurl
使用以下命令安装:

  

sudo pip install pycurl

以下是如何使用它发送数据的示例:

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()

答案 3 :(得分:0)

请求库是最受支持和最先进的方法。