本文整理匯總了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: