/ examples / requests_advanced_cpython.py
requests_advanced_cpython.py
 1  import socket
 2  import adafruit_requests
 3  
 4  http = adafruit_requests.Session(socket)
 5  
 6  JSON_GET_URL = "http://httpbin.org/get"
 7  
 8  # Define a custom header as a dict.
 9  headers = {"user-agent": "blinka/1.0.0"}
10  
11  print("Fetching JSON data from %s..." % JSON_GET_URL)
12  response = http.get(JSON_GET_URL, headers=headers)
13  print("-" * 60)
14  
15  json_data = response.json()
16  headers = json_data["headers"]
17  print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
18  print("-" * 60)
19  
20  # Read Response's HTTP status code
21  print("Response HTTP Status Code: ", response.status_code)
22  print("-" * 60)
23  
24  # Read Response, as raw bytes instead of pretty text
25  print("Raw Response: ", response.content)
26  
27  # Close, delete and collect the response data
28  response.close()