Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

urequests.get in Micropython randomly results in OSError 103 and 104 as well as ValueError #785

Open
nanowiz opened this issue Jan 5, 2024 · 2 comments

Comments

@nanowiz
Copy link

nanowiz commented Jan 5, 2024

I am using urequest.get in Micropython running on a Raspberry Pi PICO W in a loop containing the following to access Ecobee's server for my furnace status:

url = "https://api.ecobee.com/1/thermostatSummary?format=json&body={\"selection\":{\"selectionType\":\"registered\",\"selectionMatch\":\"\",\"includeEquipmentStatus\":true}}"
headers = {
        'Content-Type': 'text/json',
        'Authorization': f'Bearer {accessToken}'
}
response = urequests.get(url, headers=headers)
rstatus = response.status_code
data = response.json()
response.close()

This works most of the time, but the code will randomly crash with either OSError 104 or OSError 103 or ValueError. The following are REPL outputs of these errors.

Traceback (most recent call last):
File "", line 157, in
File "", line 52, in furnaceStatus
File "requests/init.py", line 180, in get
File "requests/init.py", line 125, in request
OSError: -104

Traceback (most recent call last):
File "", line 166, in
File "", line 52, in furnaceStatus
File "requests/init.py", line 180, in get
File "requests/init.py", line 130, in request
ValueError: HTTP error: BadStatusLine:
[]

I understand that the OSError 104 and 103 are network response issues. It would be much better if they can be handled with a returned status code rather than causing an OSError. According to the githib code at line 130 of request in requests/init.py, it is complaining about an invalid response presumabily from the internet response. Again, it would be more desirable to be handled with a status code response rather than causing a program crash.

@ned-pcs
Copy link

ned-pcs commented Jan 18, 2024

OSError 104 is ECONNRESET. Which can be caused by one end or the other closing a socket too soon.
But one should usually enclose network functions in try/except blocks anyway:

url = "https://api.ecobee.com/1/thermostatSummary?format=json&body={\"selection\":{\"selectionType\":\"registered\",\"selectionMatch\":\"\",\"includeEquipmentStatus\":true}}"
headers = {
        'Content-Type': 'text/json',
        'Authorization': f'Bearer {accessToken}'
}
try:
    response = urequests.get(url, headers=headers)
    rstatus = response.status_code
    data = response.json()
    response.close()
except OSError, ValueError:
    data = None
    rstatus = 500

@nanowiz
Copy link
Author

nanowiz commented Jan 20, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants