pythonsocketswifiespressif-idf

What is the difference between `asyncio.open_connection` and `socket.socket.accept()`


I'm trying to connect a device ESP32C3 (implementing Espressif's SDK ESP-IDF). This device creates a WiFi hotspot and streams some data.

I use Windows WiFi connection panel to connect the ESP32C3 device by providing the appropriate password. Then, I see it in ipconfig output.

We did a quick prototyping using a Python client script, this script is able to open the connection and retrieve streamed data. This script opens the connection using reader, writer = await asyncio.open_connection(HOST, PORT).

Now we wanted to integrate this in some legacy code we have that used to open a connection using:

with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
    s.bind((HOST,PORT))
    s.listen()
    conn, addr = s.accept()
    print("Accepted")

But then s.accept() hangs forever and connection is not established.

Is there any reason why asyncio.open_connection(HOST, PORT) would work while socket.socket.accept() fails?


Solution

  • What is the difference between asyncio.open_connection and socket.socket.accept()

    These functions have basically nothing in common. They are not used for the same purpose, but for the opposite purpose: asyncio.open_connection is called in the client to establish a connection to the server. socket.accept is called in the server to accept a connection from a client.

    So, open_connection is not a replacement for accept - this would be socket.connect instead. Similar accept is not a replacement for open_connection, this would be asyncio.start_server instead.