def valid_pub_url(url, proxy=False):
"""Verify that the publisher URL contains only valid characters.
If 'proxy' is set to True, some checks are relaxed."""
if not url:
return False
# First split the URL and check if the scheme is one we support
o = urlsplit(url)
if not o[0] in _valid_proto:
return False
if o[0] == "file":
path = urlparse(url, "file", allow_fragments=0)[2]
path = url2pathname(path)
if not os.path.abspath(path):
return False
# No further validation to be done.
return True
# Next verify that the network location is valid
if six.PY3:
host = urllib.parse.splitport(o[1])[0]
else:
host = urllib.splitport(o[1])[0]
if proxy:
# We may have authentication details in the proxy URI, which
# we must ignore when checking for hostname validity.
host_parts = host.split("@")
if len(host_parts) == 2:
host = host[1]
if not host or _invalid_host_chars.match(host):
return False
if _hostname_re.match(host):
return True
return False
评论列表
文章目录