我有一個可以拍照的python程序,我想知道如何編寫一個將這些圖片發送到特定URL的程序。Python中的捲曲等效
如果有問題,我在Raspberry Pi上運行。
(請原諒我的簡單,我很新的這一切)
我有一個可以拍照的python程序,我想知道如何編寫一個將這些圖片發送到特定URL的程序。Python中的捲曲等效
如果有問題,我在Raspberry Pi上運行。
(請原諒我的簡單,我很新的這一切)
我一直在使用的請求包以及可以使用。以下是請求文檔中的example POST。
如果您覺得您要使用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()
的請求庫最受支持和先進的方式來做到這一點。
可能使用[請求](http://docs.python-requests.org/en/latest/index.html) – gtgaxiola