/ examples / requests_simpletest_cpython.py
requests_simpletest_cpython.py
 1  # adafruit_requests usage with a CPython socket
 2  import socket
 3  import adafruit_requests
 4  
 5  http = adafruit_requests.Session(socket)
 6  
 7  TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
 8  JSON_GET_URL = "http://httpbin.org/get"
 9  JSON_POST_URL = "http://httpbin.org/post"
10  
11  print("Fetching text from %s" % TEXT_URL)
12  response = http.get(TEXT_URL)
13  print("-" * 40)
14  
15  print("Text Response: ", response.text)
16  print("-" * 40)
17  
18  print("Fetching JSON data from %s" % JSON_GET_URL)
19  response = http.get(JSON_GET_URL)
20  print("-" * 40)
21  
22  print("JSON Response: ", response.json())
23  print("-" * 40)
24  response.close()
25  
26  data = "31F"
27  print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
28  response = http.post(JSON_POST_URL, data=data)
29  print("-" * 40)
30  
31  json_resp = response.json()
32  # Parse out the 'data' key from json_resp dict.
33  print("Data received from server:", json_resp["data"])
34  print("-" * 40)
35  
36  json_data = {"Date": "July 25, 2019"}
37  print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
38  response = http.post(JSON_POST_URL, json=json_data)
39  print("-" * 40)
40  
41  json_resp = response.json()
42  # Parse out the 'json' key from json_resp dict.
43  print("JSON Data received from server:", json_resp["json"])
44  print("-" * 40)