1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| print('[INFO] Sending data with PycURL')
c = pycurl.Curl()
c.setopt(c.URL, 'http://demo.eco-sensors.ch/include/save_aqi_n.php')
c.setopt(c.HTTPHEADER, ['Accept: application/json','Content-Type: application/json'])
c.setopt(c.POST, 1)
try:
# If you want to set a total timeout, say, 3 seconds
c.setopt(c.TIMEOUT_MS, 3000)
print("!!! CHECK HERE !!!")
## depending on whether you want to print details on stdout, uncomment either
# curl.setopt(c.VERBOSE, 1) # to print entire request flow
## or
# curl.setopt(p.WRITEFUNCTION, lambda x: None) # to keep stdout clean
#body_as_dict = {"dev_id": "12", "path": "def", "target": "ghi"}
body_as_dict = data
body_as_json_string = json.dumps(body_as_dict) # dict to json
body_as_file_object = StringIO(body_as_json_string)
# prepare and send. See also: pycurl.READFUNCTION to pass function instead
c.setopt(c.READDATA, body_as_file_object)
c.setopt(c.POSTFIELDSIZE, len(body_as_json_string))
c.perform()
status_code = c.getinfo(pycurl.RESPONSE_CODE)
if status_code != 200:
print("Server returned HTTP status code {}".format(status_code))
#print('Device error: {}'.format(e))
c.close()
return 1
except:
c.close()
// Log something
continue
return -1 |
Partager