Utiliser l'API avec Python 2

API

Exemple

json_python2.py
import urllib2
import json
 
calaos_url = 'https://XXX.XXX.XXX.XXX/api.php' # Calaos server API url
calaos_user = 'XXX'
calaos_password = 'XXX'
 
# Parameters dictionary to be sent to Calaos server
dict_send = {
	'cn_user': calaos_user,
	'cn_pass': calaos_password,
	'action': 'get_home'  # get the complete configuration of the house
	}
 
 
json_send = json.dumps(dict_send) # parameters string in JSON format
data_send = json_send.encode('ascii') # bytes data to be sent
headers_send = {'Content-Type': 'application/json'} # media type of the data to be sent
request = urllib2.Request(calaos_url, data_send, headers_send) # data sent with POST method
response = urllib2.urlopen(request)
print(response.info())
data_receive = response.read() # bytes data received
json_receive = data_receive.decode('ascii') # string received in JSON format
dict_receive = json.loads(json_receive) # dictionary received
print(dict_receive)