Logically it's fine. The separate components work as intended. I can receive and parse HTTP requests over the TCP socket I have open and I can communicate with the Google API, no problem. The issue is when I try to do both.
Side note: The socket is set to timeout after 300 ms, so it doesn't block permanently waiting for a connection. That would cause a problem being able to poll other components like the ADC.
Code: Select all
# s is an IPv4 TCP socket
s.settimeout(0.3)
while True:
# Wait for an incoming HTTP request on Port 80
connection_received = True
addr = None
conn = None
try:
conn, addr = s.accept()
except OSError:
connection_received = False
request_valid = True
if connection_received:
# Read the data from the socket (HTTP Request text)
print('Received a connection from %s' % str(addr))
try:
request = conn.recv(1024)
except OSError:
request_valid = False
conn.close()
if request_valid:
# Make an API request if the incoming HTTP request was valid
request = str(request)
print('Content = %s' % request)
params = get_query_string_params(request)
update_state(params) # Makes a call to the Google Geo Location API
# Other code here to poll ADC for unrelated functionality
Code: Select all
url = "https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY"
payload = {"content type": "applications/jsonrequests", "content length": "0"}
print('Making Location API Request ...')
r = urequests.post(url, json=payload, ) # Reset occurs here
text = r.text
# Response is parsed below here
Code: Select all
Making Location API Request ...
ets Jan 8 2013,rst cause:2, boot mode:(3,7)
load 0x40100000, len 31024, room 16
tail 0
chksum 0xb4
load 0x3ffe8000, len 1104, room 8
tail 8
chksum 0x74
load 0x3ffe8450, len 824, room 0
tail 8
chksum 0x45
csum 0x45