Return a dictionary of parsed url

    Including scheme, netloc, path, params, query, fragment, uri, username,
    password, host, port and http_host
504479
def parse_url(url):
    
    try:
        url = unicode(url)
    except UnicodeDecodeError:
        pass

    if py3k:
        make_utf8 = lambda x: x
    else:
        make_utf8 = lambda x: isinstance(x, unicode) and x.encode('utf-8') or x

    if '://' in url:
        scheme, url = url.split('://', 1)
    else:
        scheme = 'http'
    url = 'http://' + url
    parsed = urlparse.urlsplit(url)
    r = ObjectDict()
    r['scheme'] = make_utf8(scheme)
    r['netloc'] = make_utf8(parsed.netloc)
    r['path'] = make_utf8(parsed.path)
    r['query'] = make_utf8(parsed.query)
    r['fragment'] = make_utf8(parsed.fragment)
    r['uri'] = make_utf8(parsed.path)
    if parsed.query:
        r['uri'] += '?' + make_utf8(parsed.query)
    r['username'] = make_utf8(parsed.username)
    r['password'] = make_utf8(parsed.password)
    host = make_utf8(parsed.hostname.encode('idna').decode('utf-8'))
    r['host'] = r['hostname'] = host
    try:
        r['port'] = parsed.port
    except ValueError:
        r['port'] = None
    if r['port']:
        r['http_host'] = '%s:%d' % (r['host'], r['port'])
    else:
        r['http_host'] = r['host']

    return r
667623
def _parse_uri_ssh(unt):
    """"""
    if '@' in unt.netloc:
        user, host_port = unt.netloc.split('@', 1)
    else:
        user, host_port = None, unt.netloc

    if ':' in host_port:
        host, port = host_port.split(':', 1)
    else:
        host, port = host_port, None

    if not user:
        user = None
    if not port:
        port = smart_open_ssh.DEFAULT_PORT
    else:
        port = int(port)

    return Uri(scheme=unt.scheme, uri_path=unt.path, user=user, host=host, port=port)
123572
def parts_to_url(parts=None, scheme=None, netloc=None, path=None, query=None, fragment=None):
    

    if isinstance(parts, _urllib_parse.SplitResult):
        scheme, netloc, path, query, fragment = parts
    elif parts and isinstance(parts, dict):
        scheme = parts.get('scheme', 'http')
        netloc = parts.get('netloc', '')
        path = parts.get('path', [])
        query = parts.get('query', {})
        fragment = parts.get('fragment', '')

    if isinstance(path, (list, tuple)):
        path = '/' + '/'.join(path).strip('/')
    if isinstance(query, (dict, tuple)):
        query = _unquote(_urlencode(query, doseq=True))

    return _urlunsplit((scheme, netloc, path, query, fragment)) or None
1347828
def host_from_uri(uri):
    
    default_ports = {
        'HTTP': '80',
        'HTTPS': '443',
    }

    sch, netloc, path, par, query, fra = urlparse(uri)
    if ':' in netloc:
        netloc, port = netloc.split(':', 1)
    else:
        port = default_ports.get(sch.upper())

    return netloc, port
340020
def _parse_string_host(host_str):
        
        host_str = EsParser._fix_host_prefix(host_str)
        parsed_url = urlparse(host_str)
        host = {HostParsing.HOST: parsed_url.hostname}
        if parsed_url.port:
            host[HostParsing.PORT] = parsed_url.port
        if parsed_url.scheme == HostParsing.HTTPS:
            host[HostParsing.PORT] = parsed_url.port or EsParser.SSL_DEFAULT_PORT
            host[HostParsing.USE_SSL] = True
            host[HostParsing.SCHEME] = HostParsing.HTTPS
        elif parsed_url.scheme:
            host[HostParsing.SCHEME] = parsed_url.scheme
        if parsed_url.username or parsed_url.password:
            host[HostParsing.HTTP_AUTH] = '%s:%s' % (parsed_url.username, parsed_url.password)
        if parsed_url.path and parsed_url.path != '/':
            host[HostParsing.URL_PREFIX] = parsed_url.path
        return host
1259727
def url_to_parts(url):
    

    if not url:
        return None

    scheme, netloc, path, query, fragment = _urlsplit(url)

    if not path or path == '/':
        path = []
    else:
        path = path.strip('/').split('/')

    if not query:
        query = {}
    else:
        query = _parse_qs(query)

    return _urllib_parse.SplitResult(scheme, netloc, path, query, fragment)
937919
def get_params_from_sqlalchemy_url(db_url):
        # type: (str) -> Dict[str,Any]
        
        result = urlsplit(db_url)
        return {'database': result.path[1:], 'host': result.hostname, 'port': result.port,
                'username': result.username, 'password': result.password, 'driver': result.scheme}
376161
def parse_redis_url(url):
    """"""

    # create config with some sane defaults
    redis_config = {
        "DB": 0,
        "PASSWORD": None,
        "HOST": "localhost",
        "PORT": 6379,
        "SSL": False
    }

    if not url:
        return redis_config

    url = urlparse.urlparse(url)
    # Remove query strings.
    path = url.path[1:]
    path = path.split('?', 2)[0]

    if path:
        redis_config.update({"DB": int(path)})
    if url.password:
        redis_config.update({"PASSWORD": url.password})
    if url.hostname:
        redis_config.update({"HOST": url.hostname})
    if url.port:
        redis_config.update({"PORT": int(url.port)})
    if url.scheme in ['https', 'rediss']:
        redis_config.update({"SSL": True})

    return redis_config
351003
def get_local_urls(port):
    ''''''
    url_list = []
    for name, info in psutil.net_if_addrs().items():
        for addr in info:
            if AddressFamily.AF_INET == addr.family:
                url_list.append('http://{}:{}'.format(addr.address, port))
    return url_list
351868
def generate_url(scheme=None, host=None, port=None, path=None, query=None):
    
    url = ""
    if scheme is not None:
        url += "%s://" % scheme
    if host is not None:
        url += host
    if port is not None:
        url += ":%s" % str(port)
    if path is not None:
        url += ensure_url_path_starts_with_slash(path)
    if query is not None:
        url += "?%s" % (urlencode(query))
    return url
469499
def url_host(url: str) -> str:
    
    from urllib.parse import urlparse
    res = urlparse(url)
    return res.netloc.split(':')[0] if res.netloc else ''
1038345
def parse_url_query_params(url, fragment=True):
    
    parsed_url = urlparse(url)
    if fragment:
        url_query = parse_qsl(parsed_url.fragment)
    else:
        url_query = parse_qsl(parsed_url.query)
    # login_response_url_query can have multiple key
    url_query = dict(url_query)
    return url_query
538619
def parse_dsn(dsn):
    """"""
    parsed_dsn = urlparse(dsn)
    parsed_path = parse_path(parsed_dsn.path)
    return {
        'scheme': parsed_dsn.scheme,
        'sender': parsed_dsn.username,
        'token': parsed_dsn.password,
        'domain': parsed_dsn.hostname,
        'port': parsed_dsn.port or 80,
        'version': parsed_path.get('version'),
        'project': parsed_path.get('project'),
    }
357337
def urlopen(url, timeout=20, redirects=None):
    """"""
    scheme, host, path, params, query, frag = urlparse(url)

    if not scheme in ('http', 'https'):
        return urllib.urlopen(url)
    if params: path = '%s;%s' % (path, params)
    if query:  path = '%s?%s' % (path, query)
    if frag:   path = '%s#%s' % (path, frag)

    if scheme == 'https':
        # If ssl is not compiled into Python, you will not get an exception
        # until a conn.endheaders() call.   We need to know sooner, so use
        # getattr.
        try:
            import M2Crypto
        except ImportError:
            if not hasattr(socket, 'ssl'):
                raise RuntimeError, 'no built-in SSL Support'

            conn = TimeoutHTTPS(host, None, timeout)
        else:
            ctx = M2Crypto.SSL.Context()
            ctx.set_session_timeout(timeout)
            conn = M2Crypto.httpslib.HTTPSConnection(host, ssl_context=ctx)
            conn.set_debuglevel(1)

    else:
        conn = TimeoutHTTP(host, None, timeout)

    conn.putrequest('GET', path)
    conn.putheader('Connection', 'close')
    conn.endheaders()
    response = None
    while 1:
        response = conn.getresponse()
        if response.status != 100:
            break
        conn._HTTPConnection__state = httplib._CS_REQ_SENT
        conn._HTTPConnection__response = None

    status = response.status

    # If we get an HTTP redirect, we will follow it automatically.
    if status >= 300 and status < 400:
        location = response.msg.getheader('location')
        if location is not None:
            response.close()
            if redirects is not None and redirects.has_key(location):
                raise RecursionError(
                    'Circular HTTP redirection detected.'
                    )
            if redirects is None:
                redirects = {}
            redirects[location] = 1
            return urlopen(location, timeout, redirects)
        raise HTTPResponse(response)

    if not (status >= 200 and status < 300):
        raise HTTPResponse(response)

    body = StringIO(response.read())
    response.close()
    return body
832043
def _get_url_base(self, url):
        
        s = urlsplit(url)
        return urlunsplit([s.scheme, s.netloc, '', '', ''])
1179396
def parse(url):
    
    parts = split(url)
    if parts.scheme:
        username, password, host, port = split_netloc(parts.netloc)
        subdomain, domain, tld = split_host(host)
    else:
        username = password = subdomain = domain = tld = port = ''
    return URL(parts.scheme, username, password, subdomain, domain, tld,
               port, parts.path, parts.query, parts.fragment, url)
392368
def urlsplit(url):
    
    p = '((?P<scheme>.*)?.*://)?(?P<host>[^:/ ]+).?(?P<port>[0-9]*).*'
    m = re.search(p, url)
    scheme = m.group('scheme')
    host = m.group('host')
    port = m.group('port')

    _scheme, _netloc, path, query, _fragment = tuple(py_urlsplit(url))

    return scheme, host, port, path, query