forked from adafruit/Adafruit_CircuitPython_Requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests_advanced_ethernet.py
58 lines (46 loc) · 1.72 KB
/
requests_advanced_ethernet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import board
import busio
from digitalio import DigitalInOut
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_requests as requests
cs = DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Initialize ethernet interface with DHCP
eth = WIZNET5K(spi_bus, cs)
# Initialize a requests object with a socket and ethernet interface
requests.set_socket(socket, eth)
JSON_GET_URL = "http://httpbin.org/get"
attempts = 3 # Number of attempts to retry each request
failure_count = 0
response = None
# Define a custom header as a dict.
headers = {"user-agent": "blinka/1.0.0"}
print("Fetching JSON data from %s..." % JSON_GET_URL)
while not response:
try:
response = requests.get(JSON_GET_URL, headers=headers)
failure_count = 0
except AssertionError as error:
print("Request failed, retrying...\n", error)
failure_count += 1
if failure_count >= attempts:
raise AssertionError(
"Failed to resolve hostname, \
please check your router's DNS configuration."
) from error
continue
print("-" * 60)
json_data = response.json()
headers = json_data["headers"]
print("Response's Custom User-Agent Header: {0}".format(headers["User-Agent"]))
print("-" * 60)
# Read Response's HTTP status code
print("Response HTTP Status Code: ", response.status_code)
print("-" * 60)
# Read Response, as raw bytes instead of pretty text
print("Raw Response: ", response.content)
# Close, delete and collect the response data
response.close()