[New-bugs-announce] [issue42795] Asyncio loop.create_server doesn't bind to any interface if host is a sequence with jus the empty string

Paolo Lammens report at bugs.python.org
Thu Dec 31 06:37:17 EST 2020


New submission from Paolo Lammens <lammenspaolo at gmail.com>:

When a sequence containing just the empty string (e.g. `['']`) is passed as the `host` parameter of `loop.create_server`, the server seems not to bind to any network interface. Since, per the [documentation](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_server) for `create_server`, the empty string means "bind to all interfaces", 

> If host is an empty string or None all interfaces are assumed and a list of multiple 
> sockets will be returned (most likely one for IPv4 and another one for IPv6).


and also

> The host parameter can also be a sequence (e.g. list) of hosts to bind to.

I would have expected a list containing the empty string to also work as expected, i.e. "binding to all hosts in the sequence", so binding to "" and thus to every interface.

Example:

Server script:

```python
import asyncio


async def server():
    async def connection_callback(reader, writer: asyncio.StreamWriter):
        print(f"got connection from {writer.get_extra_info('peername')}")
        writer.close()
        await writer.wait_closed()

    s = await asyncio.start_server(connection_callback, host=[''], port=4567)
    async with s:
        print("starting server")
        await s.serve_forever()


asyncio.run(server())
```

Client script:

```python
import asyncio


async def client():
    reader, writer = await asyncio.open_connection("127.0.0.1", 4567)
    print(f"connected to {writer.get_extra_info('peername')}")
    writer.close()
    await writer.wait_closed()


asyncio.run(client())
```

Expected:

- Server:
  ```
  starting server
  got connection from ('127.0.0.1', xxxxx)
  ```

- Client:
  ```
  connected to ('127.0.0.1', xxxxx)
  ```

Actual:

- Server:
  ```
  starting server
  ```

- Client: a ConnectionError is raised (the host machine refused the connection)

----------
components: asyncio
messages: 384109
nosy: asvetlov, plammens, yselivanov
priority: normal
severity: normal
status: open
title: Asyncio loop.create_server doesn't bind to any interface if host is a sequence with jus the empty string
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42795>
_______________________________________


More information about the New-bugs-announce mailing list