Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import collections.abc
import concurrent.futures
import heapq
import ipaddress
import itertools
import logging
import os
Expand Down Expand Up @@ -1333,6 +1334,9 @@ async def create_server(
hosts = [None]
elif (isinstance(host, str) or
not isinstance(host, collections.abc.Iterable)):
if (isinstance(host, ipaddress.IPv4Address) or
isinstance(host, ipaddress.IPv6Address)):
host = host.exploded
hosts = [host]
else:
hosts = host
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_asyncio/test_base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import math
import os
import ipaddress
import socket
import sys
import threading
Expand Down Expand Up @@ -1180,6 +1181,26 @@ async def main():
else:
raise

@unittest.skipUnless(hasattr(socket, 'AF_INET6'), 'no IPv6 support')
def test_create_server_ipaddr_ipv6(self):
async def main():
srv = await asyncio.start_server(
lambda: None, ipaddress.IPv6Address('::1'), 0, loop=self.loop)
try:
self.assertGreater(len(srv.sockets), 0)
finally:
srv.close()
await srv.wait_closed()

try:
self.loop.run_until_complete(main())
except OSError as ex:
if (hasattr(errno, 'EADDRNOTAVAIL') and
ex.errno == errno.EADDRNOTAVAIL):
self.skipTest('failed to bind to ::1')
else:
raise

def test_create_datagram_endpoint_wrong_sock(self):
sock = socket.socket(socket.AF_INET)
with sock:
Expand Down Expand Up @@ -1509,6 +1530,11 @@ def test_create_server_host_port_sock(self):
MyProto, '0.0.0.0', 0, sock=object())
self.assertRaises(ValueError, self.loop.run_until_complete, fut)

def test_create_server_ipaddr_ipv4_host_port_sock(self):
fut = self.loop.create_server(
MyProto, ipaddress.IPv4Address('0.0.0.0'), 0, sock=object())
self.assertRaises(ValueError, self.loop.run_until_complete, fut)

def test_create_server_no_host_port_sock(self):
fut = self.loop.create_server(MyProto)
self.assertRaises(ValueError, self.loop.run_until_complete, fut)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Support `ipaddress.IPv4` and `ipaddress.IPv6` objects for creating asyncio
server.