how to make urequests asynchronous?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
kenjican
Posts: 13
Joined: Sat Sep 15, 2018 1:32 am

Re: how to make urequests asynchronous?

Post by kenjican » Sun Jan 30, 2022 5:35 am

@curt
Thanks for advises. But TCP and SSL is a must.
@marcidy
await sWriter.drain() works. But I found that asyncio.open_connection doesn't support "ssl=True" parameter
I try to make ssl sokcet:

Code: Select all

sWriter = ussl.wrap_socket(sWriter.s,server_hostname=ip)
It shows : OSError: (-29312, 'MBEDTLS_ERR_SSL_CONN_EOF')
Any idea?

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: how to make urequests asynchronous?

Post by marcidy » Sun Jan 30, 2022 6:21 am

try creating the socket like in examples/network/http_client.py:

Code: Select all

try:                                                                            
    import usocket as _socket                                                   
except:                                                                         
    import _socket                                                              
try:                                                                            
    import ussl as ssl                                                          
except:                                                                         
    import ssl                                                                  
                                                                                
                                                                                
def main(use_stream=True):                                                      
    s = _socket.socket()                                                        
                                                                                
    ai = _socket.getaddrinfo("google.com", 443)                                 
    print("Address infos:", ai)                                                 
    addr = ai[0][-1]                                                            
                                                                                
    print("Connect address:", addr)                                             
    s.connect(addr)                                                             
                                                                                
    s = ssl.wrap_socket(s)                                                      
    print(s)                                                                    
                                                                                
    if use_stream:                                                              
        # Both CPython and MicroPython SSLSocket objects support read() and        
        # write() methods.                                                      
        s.write(b"GET / HTTP/1.0\r\n\r\n")                                      
        print(s.read(4096))                                                     
    else:                                                                       
        # MicroPython SSLSocket objects implement only stream interface, not       
        # socket interface                                                      
        s.send(b"GET / HTTP/1.0\r\n\r\n")                                       
        print(s.recv(4096))                                                     
                                                                                
    s.close()
Then create the stream with the created socket. let me know if it works, I haven't gone this far :D

kenjican
Posts: 13
Joined: Sat Sep 15, 2018 1:32 am

Re: how to make urequests asynchronous?

Post by kenjican » Tue Feb 01, 2022 1:10 am

Thanks for great advices. I modified urequests with uasyncio.stream . It seems working.
I put the code in urequests_ff

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

Re: how to make urequests asynchronous?

Post by pythoncoder » Tue Feb 01, 2022 10:45 am

Congratulations on getting this working. Why have you created your own copy of uasyncio.Stream? As far as I can see the code is identical to the official built-in version.
Peter Hinch
Index to my micropython libraries.

kenjican
Posts: 13
Joined: Sat Sep 15, 2018 1:32 am

Re: how to make urequests asynchronous?

Post by kenjican » Tue Feb 01, 2022 2:11 pm

em.. To modify urequests.py would be easier,just because aysncio is build-in. I have no idea how to modify build-in modules.
If asyncio module has requests function, it would be great. I think people need it.

kenjican
Posts: 13
Joined: Sat Sep 15, 2018 1:32 am

Re: how to make urequests asynchronous?

Post by kenjican » Wed Feb 02, 2022 3:18 pm

Sorry, I might misunderstood your question.
The code I modified are from these two places:
1: urequests: https://github.com/micropython/micropyt ... /urequests
2: asyncio : https://github.com/micropython/micropyt ... d/uasyncio

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

Re: how to make urequests asynchronous?

Post by pythoncoder » Wed Feb 02, 2022 7:28 pm

I understand why you modified urequests. I'm puzzled why you copied uasyncio code to your own module unchanged, when that code is already built in to firmware. You'd save space by simply using the built-in Stream:

Code: Select all

from uasyncio import core, Stream
Peter Hinch
Index to my micropython libraries.

kenjican
Posts: 13
Joined: Sat Sep 15, 2018 1:32 am

Re: how to make urequests asynchronous?

Post by kenjican » Fri Feb 04, 2022 3:50 am

Thanks for the advice. Yes, to import is a resource-saving solution.Much better than copy :D

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

Re: how to make urequests asynchronous?

Post by pythoncoder » Fri Feb 04, 2022 9:33 am

It also means that you get the benefit of any bugfixes implemented by the maintainers.
Peter Hinch
Index to my micropython libraries.

Post Reply