correctly handle IPv6 and IPv4 addresses fix #1143

This commit correctly splits IPv6 addresses into the host and port
parts. This will work for normal IPv4 and IPv6 addresses appended by a
port number as well es for IPv6 addressess without a port, which should
be the normal IPv6 usage.
This commit is contained in:
Sebastian Bögl 2016-02-14 22:21:25 +01:00
parent 9c7e99cbf5
commit d48c560df1

View file

@ -110,8 +110,17 @@ class Addr(object):
@classmethod
def fromstring(cls, str_addr):
"""Initialize Addr from string."""
tup = str_addr.partition(':')
return cls((tup[0], tup[2]))
if str_addr.startswith('['):
# ipv6 addresses starts with [
endIndex = str_addr.rfind(']')
host = str_addr[:endIndex + 1]
port = ''
if len(str_addr) > endIndex + 3 and str_addr[endIndex + 2] == ':':
port = str_addr[endIndex + 3:]
return cls((host, port))
else:
tup = str_addr.partition(':')
return cls((tup[0], tup[2]))
def __str__(self):
if self.tup[1]: