urequests and timeout?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
zmaier
Posts: 14
Joined: Thu May 16, 2019 12:44 pm

urequests and timeout?

Post by zmaier » Fri May 31, 2019 6:44 am

Hi everyone.

when loosing the wlan-connection (e.g. i turn of the router) my programm hangs at :
response = urequests.post(db, data=payload,timeout=timeout_sec)

it is not raising a exception, the program hangs for ever at this line.

Any idea why?

How do you implement a simple post-command?

THX
Martin

I am using this urequests.py :
https://github.com/micropython/micropyt ... equests.py

fefe
Posts: 8
Joined: Tue May 28, 2019 5:52 pm

Re: urequests and timeout?

Post by fefe » Fri May 31, 2019 4:46 pm

If you lose wifi connectivity it should throw an exception.

I would try wrapping the request in a try block and check to see if the wifi goes out and handle that exception. Here is a very basic example using a get request. If you're doing a post you should check if status_code != 201.

Code: Select all

while res.status_code != 200:
        try:
            gc.collect()
            res = req.get(url=url)
            gc.collect()
        except:
            # no internet connection on chip
            if not wlan_sta.isconnected():
                  time.sleep(30)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: urequests and timeout?

Post by pythoncoder » Fri May 31, 2019 5:48 pm

fefe wrote:
Fri May 31, 2019 4:46 pm
If you lose wifi connectivity it should throw an exception...
I wish it were so. You might like to read my observations on this issue.
Peter Hinch
Index to my micropython libraries.

Post Reply