Differences
This shows you the differences between two versions of the page.
— |
fr:json_python2 [2017/03/20 10:49] (current) mifrey created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== Utiliser l'API avec Python 2 ===== | ||
+ | [[protocole_json|API]] | ||
+ | ==== Exemple ==== | ||
+ | <code python json_python2.py> | ||
+ | import urllib2 | ||
+ | import json | ||
+ | |||
+ | calaos_url = ' | ||
+ | calaos_user = ' | ||
+ | calaos_password = ' | ||
+ | |||
+ | # Parameters dictionary to be sent to Calaos server | ||
+ | dict_send = { | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | |||
+ | |||
+ | json_send = json.dumps(dict_send) # parameters string in JSON format | ||
+ | data_send = json_send.encode(' | ||
+ | headers_send = {' | ||
+ | request = urllib2.Request(calaos_url, | ||
+ | response = urllib2.urlopen(request) | ||
+ | print(response.info()) | ||
+ | data_receive = response.read() # bytes data received | ||
+ | json_receive = data_receive.decode(' | ||
+ | dict_receive = json.loads(json_receive) # dictionary received | ||
+ | print(dict_receive) | ||
+ | </ |