Utiliser l'API avec Python 3

API

Exemple

json_python3.py
import urllib.request, urllib.parse
import json
import ssl
 
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 = urllib.request.Request(calaos_url, data_send, headers_send) # data sent with POST method
context = ssl._create_unverified_context() # do not verify the SSL certificate
 
with urllib.request.urlopen(request, context=context) as response:
    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)