summaryrefslogtreecommitdiff
path: root/Lib/asyncio/base_events.py
diff options
context:
space:
mode:
authorYury Selivanov <yselivanov@sprymix.com>2016-06-02 16:51:07 -0400
committerYury Selivanov <yselivanov@sprymix.com>2016-06-02 16:51:07 -0400
commita714616d36131860f4f3f18bc2cfaf9a578db053 (patch)
treebb7461f019d61925f098861476baf4710d09e08f /Lib/asyncio/base_events.py
parenta8f895f051588cc5186650f13118b0149ae7e3d5 (diff)
downloadcpython-git-a714616d36131860f4f3f18bc2cfaf9a578db053.tar.gz
asyncio: Fix getaddrinfo to accept service names (for port)
Patch by A. Jesse Jiryu Davis
Diffstat (limited to 'Lib/asyncio/base_events.py')
-rw-r--r--Lib/asyncio/base_events.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index e5feb99857..2b2c18536d 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -102,10 +102,26 @@ def _ipaddr_info(host, port, family, type, proto):
else:
return None
- if port in {None, '', b''}:
+ if port is None:
port = 0
- elif isinstance(port, (bytes, str)):
- port = int(port)
+ elif isinstance(port, bytes):
+ if port == b'':
+ port = 0
+ else:
+ try:
+ port = int(port)
+ except ValueError:
+ # Might be a service name like b"http".
+ port = socket.getservbyname(port.decode('ascii'))
+ elif isinstance(port, str):
+ if port == '':
+ port = 0
+ else:
+ try:
+ port = int(port)
+ except ValueError:
+ # Might be a service name like "http".
+ port = socket.getservbyname(port)
if hasattr(socket, 'inet_pton'):
if family == socket.AF_UNSPEC: