Python urlparse.uses_netloc() Examples

The following are 3 code examples of urlparse.uses_netloc(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urlparse , or try the search function .
Example #1
Source File: user.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def uri_encode_idna(uri):
    '''
    Do IDNA encoding for hostnames, if possible
    '''
    scheme, netloc, path, query, fragment  = urlparse.urlsplit(uri)
    if scheme.lower() in urlparse.uses_netloc and netloc is not None:
        user_password, host_port = urllib.splituser(netloc)
        if host_port is not None:
            host, port = urllib.splitport(host_port)
            if host is not None and host[:1] + host[-1:] != '[]':
                # NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
                host = ''.join([ chr(ord(ch)) for ch in host ])
                try:
                    host = urllib.quote(unicodedata.normalize('NFKC', urllib.unquote(host).decode('utf-8')).encode('utf-8'))
                except:
                    pass
                try:
                    host = urllib.quote(encode_idna(urllib.unquote(host)))
                except:
                    pass
                host_port = host + (port is not None and (':' + port) or '')
                netloc = (user_password is not None and (user_password + '@') or '') + host_port
        pass
    uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
    # NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
    uri = ''.join([ chr(ord(ch)) for ch in uri ])
    return uri 
Example #2
Source File: user.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def uri_decode_idna(uri):
    '''
    Do IDNA decoding for hostnames, if possible
    '''
    scheme, netloc, path, query, fragment  = urlparse.urlsplit(uri)
    if scheme.lower() in urlparse.uses_netloc and netloc is not None:
        user_password, host_port = urllib.splituser(netloc)
        if host_port is not None:
            host, port = urllib.splitport(host_port)
            if host is not None and host[:1] + host[-1:] != '[]':
                # NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
                host = ''.join([ chr(ord(ch)) for ch in host ])
                try:
                    host = urllib.quote(decode_idna(urllib.unquote(host)))
                except:
                    pass
                try:
                    host = urllib.quote(unicodedata.normalize('NFKC', urllib.unquote(host).decode('utf-8')).encode('utf-8'))
                except:
                    pass
                host_port = host + (port is not None and (':' + port) or '')
                netloc = (user_password is not None and (user_password + '@') or '') + host_port
        pass
    uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
    # NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
    uri = ''.join([ chr(ord(ch)) for ch in uri ])
    return uri 
Example #3
Source File: metadata.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def normalize_uri(uri):
    '''
    normalize a URI; also converts IRIs to URIs
    '''
    uri = ''.join([ (ord(x) in xrange(33, 127)) and x or urllib.quote(x, safe='') for x in u''.join(uri.decode('UTF-8').split()).encode('UTF-8') ])
    try:
        scheme, netloc, path, query, fragment = urlparse.urlsplit(uri)
        uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
        if scheme in urlparse.uses_netloc:
            if netloc is not None:
                user, hostport = urllib.splituser(netloc)
                if hostport is not None:
                    host, port = urllib.splitport(hostport)
                    if host is not None:
                        if host[:1] != '[':
                            # hostname segments get downcased and IDNA-encoded
                            ohost = []
                            for part in host.split('.'):
                                part = urllib.unquote(part)
                                try:
                                    part = part.decode('UTF-8').lower().encode('UTF-8')
                                    try:
                                        part = part.decode('UTF-8').encode('idna')
                                        pass
                                    except KeyboardInterrupt, k:
                                        raise
                                    except:
                                        pass
                                    pass
                                except KeyboardInterrupt, k:
                                    raise
                                except: