Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:json_python2 [2017/01/21 14:53] (current)
mifrey created
Line 1: Line 1:
 +===== Use the API with Python 2 =====
 +[[protocole_json|API]]
 +==== Example ====
 +<code python 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)
 +</​code>​