本文整理汇总了Python中urlparse.uses_netloc方法的典型用法代码示例。如果您正苦于以下问题:Python urlparse.uses_netloc方法的具体用法?Python urlparse.uses_netloc怎么用?Python urlparse.uses_netloc使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urlparse
的用法示例。
在下文中一共展示了urlparse.uses_netloc方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: uri_encode_idna
# 需要导入模块: import urlparse [as 别名]
# 或者: from urlparse import uses_netloc [as 别名]
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
示例2: uri_decode_idna
# 需要导入模块: import urlparse [as 别名]
# 或者: from urlparse import uses_netloc [as 别名]
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
示例3: normalize_uri
# 需要导入模块: import urlparse [as 别名]
# 或者: from urlparse import uses_netloc [as 别名]
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: