aio-libs / aio-libs/aiosmtpd

Connection refused when setting port to zero and inside a docker container

Abierto
#276 9 comentarios 0 reacciones 1 asignado Reclamado por @pepoluan Ver en GitHub
enhancement integration
Lenguaje dominante
Python
Estrellas
373
Forks
105
Merge medio
4 min
PR fusionados (30 d)
2

Descripción

I am trying to develop a test email server that should run in a docker container. The docker container is run with --network=host -P; though for unit tests I would like to use --network=none.

I am deliberately specifying that port 0 should be used (let the OS pick an available port). I then have a function to get the port that was actually used. The server code is

```python
#! /usr/bin/env python3

import asyncio #pylint: disable=unused-import
from aiosmtpd.controller import Controller

class DemoEmailHandler:
async def handle_DATA(self, server, session, envelope):
message = {
'peer' : session.peer,
'mailfrom' : envelope.mail_from,
'to': envelope.rcpt_tos,
'data' : envelope.content}
print(message)
return '250 Message accepted for delivery'

class DemoEmailServer:
def __init__(self):
self._handler = DemoEmailHandler()
self._server = Controller(
handler=self._handler,
hostname="0.0.0.0",
port=0
)
self._server.start()
self._port = self._server.server.sockets[0].getsockname()[1]

def stop(self):
"""Tell the server to stop"""
self._server.stop()

def getPort(self) -> int:
"""Return the port the SMTP (email) server is actually on"""
return self._port

SERVER = DemoEmailServer()
print(SERVER.getPort())
SERVER.stop()
```
I keep getting connection refused

```
Traceback (most recent call last):
File "./demo_email_server.py", line 36, in
SERVER = DemoEmailServer()
File "./demo_email_server.py", line 25, in __init__
self._server.start()
File "/usr/local/lib/python3.8/dist-packages/aiosmtpd-1.4.2-py3.8.egg/aiosmtpd/controller.py", line 223, in start
self._trigger_server()
File "/usr/local/lib/python3.8/dist-packages/aiosmtpd-1.4.2-py3.8.egg/aiosmtpd/controller.py", line 313, in _trigger_server
s = stk.enter_context(create_connection((hostname, self.port), 1.0))
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
```

when running inside a docker container. If I run on the host then the server starts up fine.

(My host and container are both Ubuntu 20.04, python 3.8.5, the container has aiosmtpd 1.4.2 installed from source).

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.