Skip to content

Commit

Permalink
[fix] when there is ::1 as localhost
Browse files Browse the repository at this point in the history
When in /etc/hosts there is ::1 the binding is failing on localhost in a docker container
see https://stackoverflow.com/questions/50115923/python-asyncio-cannot-bind-on-1-ipv6-loopback-in-docker
  • Loading branch information
bamthomas committed May 1, 2018
1 parent 830ae85 commit f63b448
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions aioimaplib/aioimaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ def _find_pending_async_cmd_by_tag(self, tag):
class IMAP4(object):
TIMEOUT_SECONDS = 10

def __init__(self, host='localhost', port=IMAP4_PORT, loop=asyncio.get_event_loop(), timeout=TIMEOUT_SECONDS, conn_lost_cb=None, ssl_context=None):
def __init__(self, host='127.0.0.1', port=IMAP4_PORT, loop=asyncio.get_event_loop(), timeout=TIMEOUT_SECONDS, conn_lost_cb=None, ssl_context=None):
self.timeout = timeout
self.port = port
self.host = host
Expand Down Expand Up @@ -808,7 +808,7 @@ def extract_exists(response):


class IMAP4_SSL(IMAP4):
def __init__(self, host='localhost', port=IMAP4_SSL_PORT, loop=asyncio.get_event_loop(),
def __init__(self, host='127.0.0.1', port=IMAP4_SSL_PORT, loop=asyncio.get_event_loop(),
timeout=IMAP4.TIMEOUT_SECONDS, ssl_context=None):
super().__init__(host, port, loop, timeout, None, ssl_context)

Expand Down
2 changes: 1 addition & 1 deletion aioimaplib/tests/imapserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def wait_state(self, state, user):
def get_connection(self, user):
return self._server_state.get_connection(user)

def run_server(self, host='localhost', port=1143, fetch_chunk_size=0, ssl_context=None):
def run_server(self, host='127.0.0.1', port=1143, fetch_chunk_size=0, ssl_context=None):
def create_protocol():
protocol = ImapProtocol(self._server_state, fetch_chunk_size, self.capabilities, self.loop)
self._connections.append(protocol)
Expand Down
4 changes: 2 additions & 2 deletions aioimaplib/tests/ssl_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def create_temp_self_signed_cert():
""" Create a self signed SSL certificate in temporary files for host
'localhost'
'127.0.0.1'
Returns a tuple containing the certificate file name and the key
file name.
Expand All @@ -24,7 +24,7 @@ def create_temp_self_signed_cert():
cert.get_subject().L = "London"
cert.get_subject().O = "aioimaplib"
cert.get_subject().OU = "aioimaplib"
cert.get_subject().CN = 'localhost'
cert.get_subject().CN = '127.0.0.1'
cert.set_serial_number(1000)
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(10 * 365 * 24 * 60 * 60)
Expand Down
4 changes: 2 additions & 2 deletions aioimaplib/tests/test_imapserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _init_server(self, loop, capabilities=None, ssl_context=None):
else:
self.imapserver = MockImapServer(loop=loop)
self.server = self.imapserver.run_server(
host='localhost', port=12345, fetch_chunk_size=64, ssl_context=ssl_context
host='127.0.0.1', port=12345, fetch_chunk_size=64, ssl_context=ssl_context
)

@asyncio.coroutine
Expand All @@ -164,7 +164,7 @@ def _shutdown_server(self):
@asyncio.coroutine
def login_user(self, login, password, select=False, lib=imaplib.IMAP4):
imap_client = yield from asyncio.wait_for(
self.loop.run_in_executor(None, functools.partial(lib, host='localhost', port=12345)), 1)
self.loop.run_in_executor(None, functools.partial(lib, host='127.0.0.1', port=12345)), 1)

yield from asyncio.wait_for(
self.loop.run_in_executor(None, functools.partial(imap_client.login, login, password)), 1)
Expand Down
6 changes: 3 additions & 3 deletions aioimaplib/tests/test_imapserver_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def __init__(self, methodName='runTest'):

@asyncio.coroutine
def test_server_greetings_and_capabilities(self):
pending_imap = self.loop.run_in_executor(None, functools.partial(imaplib.IMAP4, host='localhost', port=12345))
pending_imap = self.loop.run_in_executor(None, functools.partial(imaplib.IMAP4, host='127.0.0.1', port=12345))
imap_client = yield from asyncio.wait_for(pending_imap, 1)

self.assertEqual('NONAUTH', imap_client.state)

@asyncio.coroutine
def test_server_login(self):
pending_imap = self.loop.run_in_executor(None, functools.partial(imaplib.IMAP4, host='localhost', port=12345))
pending_imap = self.loop.run_in_executor(None, functools.partial(imaplib.IMAP4, host='127.0.0.1', port=12345))
imap_client = yield from asyncio.wait_for(pending_imap, 1)

pending_login = self.loop.run_in_executor(None, functools.partial(imap_client.login, 'user', 'pass'))
Expand Down Expand Up @@ -514,7 +514,7 @@ def test_client_can_connect_to_server_over_ssl(self):

pending_imap = self.loop.run_in_executor(None, functools.partial(
imaplib.IMAP4_SSL,
host='localhost',
host='127.0.0.1',
port=12345,
ssl_context=ssl_context)
)
Expand Down

0 comments on commit f63b448

Please sign in to comment.