2014-01-18 74 views
0

我有一个可以拍照的python程序,我想知道如何编写一个将这些图片发送到特定URL的程序。Python中的卷曲等效

如果有问题,我在Raspberry Pi上运行。

(请原谅我的简单,我很新的这一切)

+1

可能使用[请求](http://docs.python-requests.org/en/latest/index.html) – gtgaxiola

回答

1

我一直在使用的请求包以及可以使用。以下是请求文档中的example POST

0

如果您觉得您要使用CURL,请尝试PyCurl
安装它使用:

须藤PIP安装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() 
0

的请求库最受支持和先进的方式来做到这一点。