diff --git a/boltons/urlutils.py b/boltons/urlutils.py index e737ae9..f02dce1 100644 --- a/boltons/urlutils.py +++ b/boltons/urlutils.py @@ -898,16 +898,19 @@ def parse_url(url_text): if hostinfo: host, sep, port_str = hostinfo.partition(u':') if sep: - if u']' in port_str: - host = hostinfo # wrong split, was an ipv6 - else: - try: - port = int(port_str) - except ValueError: - if port_str: # empty ports ok according to RFC 3986 6.2.3 - raise URLParseError('expected integer for port, not %r' - % port_str) - port = None + if host and host[0] == u'[' and u']' in port_str: + host_right, _, port_str = port_str.partition(u']') + host = host + u':' + host_right + u']' + if port_str and port_str[0] == u':': + port_str = port_str[1:] + + try: + port = int(port_str) + except ValueError: + if port_str: # empty ports ok according to RFC 3986 6.2.3 + raise URLParseError('expected integer for port, not %r' + % port_str) + port = None family, host = parse_host(host)