Page 1 of 1

asyncio tcp_echo_client example from python.org not working

Posted: Tue Apr 06, 2021 2:33 pm
by jcolo
Hi,

I am trying to run the example listed here

https://docs.python.org/3/library/async ... io-streams

which seems to me it should run in micropython

Code: Select all

import network
import machine
import utime as time

import uasyncio as asyncio

async def tcp_echo_client(message):

    #reader, writer = await asyncio.open_connection( '127.0.0.1', 8888)
    reader, writer = await asyncio.open_connection( '192.168.12.122', 8888)
    print('Send: {}'.format(message))
    writer.write(message.encode())

    await writer.drain()
    data = await reader.read(100)

    print('Received: '.format(data.decode()))
    print('Close the connection')

    writer.close()

    await writer.wait_closed()
    
    
def main():

    wlan = network.WLAN(network.STA_IF)
    ssid, paswd = open(wifi.txt, tr).readlines()[0].strip().split(,)
    wlan.active(True)
    wlan.connect(ssid, paswd)
    t=time.ticks_ms()
     while(not wlan.isconnected()):
        time.sleep(0.25)
    print(wlan.ifconfig())

    asyncio.run(tcp_echo_client('Hello World!'))

if __name__ == __main__:
        main()
I must say that the same example blocks indefinitely on ubuntu 20.04 with python 3.8.5. So the python documentation example for streams does not work in any of both platforms

This is the error I get in micropython

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 40, in <module>
  File "<stdin>", line 34, in main
  File "uasyncio/core.py", line 1, in run
  File "uasyncio/core.py", line 1, in run_until_complete
  File "uasyncio/core.py", line 1, in run_until_complete
  File "<stdin>", line 13, in tcp_echo_client
  File "uasyncio/stream.py", line 1, in drain
OSError: [Errno 104] ECONNRESET


Re: asyncio tcp_echo_client example from python.org not working

Posted: Wed Apr 07, 2021 7:01 am
by pythoncoder
I think this error implies a communication problem with your network peer.

Re: asyncio tcp_echo_client example from python.org not working

Posted: Wed Apr 07, 2021 8:17 am
by jcolo
it is controller's IP.

It failed with localhost so I tried the assigned by dhcp own address.

Failed as well in linux/python 3.8.5. It just hangs indefinitely. At least with micropython it rises an exception

Thanks

JC

BTW: I am using an esp32 dev kit C.

Re: asyncio tcp_echo_client example from python.org not working [solved]I have discussed this with the

Posted: Thu May 06, 2021 3:20 pm
by jcolo
I have discussed this the python maintainers. I was wrong on using the example verbatim. They agree the language induces to confusion. They have agreed to make a pull request to update the documentation.

The example should be like this

Where client and server run on different processes.

Code: Select all

import asyncio

async def handle_echo(reader, writer):
    data = await reader.read(100)
    message = data.decode()
    addr = writer.get_extra_info('peername')

    print(f"Received {message!r} from {addr!r}")

    print(f"Send: {message!r}")
    writer.write(data)
    await writer.drain()

    print("Close the connection")
    writer.close()

async def main():
    server = await asyncio.start_server(
        handle_echo, '127.0.0.1', 8888)

    addr = server.sockets[0].getsockname()
    print(f'Serving on {addr}')

    async with server:
        await server.serve_forever()

asyncio.run(main())