本文整理匯總了Python中urllib.parse.urlsplit方法的典型用法代碼示例。如果您正苦於以下問題:Python parse.urlsplit方法的具體用法?Python parse.urlsplit怎麽用?Python parse.urlsplit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib.parse
的用法示例。
在下文中一共展示了parse.urlsplit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: extract_url_path_and_query
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def extract_url_path_and_query(full_url=None, no_query=False):
"""
Convert http://foo.bar.com/aaa/p.html?x=y to /aaa/p.html?x=y
:param no_query:
:type full_url: str
:param full_url: full url
:return: str
"""
if full_url is None:
full_url = request.url
split = urlsplit(full_url)
result = split.path or "/"
if not no_query and split.query:
result += '?' + split.query
return result
# ################# End Client Request Handler #################
# ################# Begin Middle Functions #################
示例2: _detect_http_redirection
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def _detect_http_redirection(http_response: HTTPResponse, server_host_name: str, server_port: int) -> Optional[str]:
"""If the HTTP response contains a redirection to the same server, return the path to the new location.
"""
next_location_path = None
if 300 <= http_response.status < 400:
location_header = _extract_first_header_value(http_response, "Location")
if location_header:
parsed_location = urlsplit(location_header)
is_relative_url = False if parsed_location.hostname else True
if is_relative_url:
# Yes, to a relative URL; follow the redirection
next_location_path = location_header
else:
is_absolute_url_to_same_hostname = parsed_location.hostname == server_host_name
absolute_url_port = 443 if parsed_location.port is None else parsed_location.port
is_absolute_url_to_same_port = absolute_url_port == server_port
if is_absolute_url_to_same_hostname and is_absolute_url_to_same_port:
# Yes, to an absolute URL to the same server; follow the redirection
next_location_path = f"{parsed_location.path}"
if parsed_location.query:
next_location_path += f"?{parsed_location.query}"
return next_location_path
示例3: delete
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def delete(url):
url = unquote(url)
match, project = Cache.match(url)
if match:
path = Cache.path(url, project, include_file=True)
# Rather then wait for last updated statistics to expire, remove the
# project cache if applicable.
if project:
apiurl, _ = Cache.spliturl(url)
if project.isdigit():
# Clear target project cache upon request acceptance.
project = osc.core.get_request(apiurl, project).actions[0].tgt_project
Cache.delete_project(apiurl, project)
if os.path.exists(path):
if conf.config['debug']: print('CACHE_DELETE', url, file=sys.stderr)
os.remove(path)
# Also delete version without query. This does not handle other
# variations using different query strings. Handy for PUT with ?force=1.
o = urlsplit(url)
if o.query != '':
url_plain = SplitResult(o.scheme, o.netloc, o.path, '', o.fragment).geturl()
Cache.delete(url_plain)
示例4: path
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def path(url, project, include_file=False, makedirs=False):
if not Cache.CACHE_DIR:
raise Exception('Cache.init() must be called first')
parts = [Cache.CACHE_DIR]
o = urlsplit(url)
parts.append(o.hostname)
if project:
parts.append(project)
directory = os.path.join(*parts)
if not os.path.exists(directory) and makedirs:
os.makedirs(directory)
if include_file:
parts.append(hashlib.sha1(url.encode('utf-8')).hexdigest())
return os.path.join(*parts)
return directory
示例5: handle_incident
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def handle_incident(self, icd):
url = icd.get("url")
if isinstance(url, bytes):
try:
url = url.decode(encoding="utf-8")
except UnicodeEncodeError as e:
logger.warning("Error decoding URL %s", url, exc_info=True)
return
if url.startswith('tftp://'):
# python fails parsing tftp://, ftp:// works, so ...
logger.info("do download")
x = parse.urlsplit(url[1:])
if x.netloc == '0.0.0.0':
logger.info("Discarding download from INADDR_ANY")
return
try:
con = icd.con
except AttributeError:
con = None
t=TftpClient()
t.download(con, x.netloc, 69, x.path[1:], url)
示例6: unshorten
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def unshorten(self, uri, type=None):
domain = urlsplit(uri).netloc
if not domain:
return uri, "No domain found in URI!"
had_google_outbound, uri = self._clear_google_outbound_proxy(uri)
if re.search(self._adfly_regex, domain, re.IGNORECASE) or type == 'adfly':
return self._unshorten_adfly(uri)
if re.search(self._adfocus_regex, domain, re.IGNORECASE) or type == 'adfocus':
return self._unshorten_adfocus(uri)
if re.search(self._linkbucks_regex, domain, re.IGNORECASE) or type == 'linkbucks':
return self._unshorten_linkbucks(uri)
if re.search(self._lnxlu_regex, domain, re.IGNORECASE) or type == 'lnxlu':
return self._unshorten_lnxlu(uri)
if re.search(self._shst_regex, domain, re.IGNORECASE):
return self._unshorten_shst(uri)
if re.search(self._hrefli_regex, domain, re.IGNORECASE):
return self._unshorten_hrefli(uri)
if re.search(self._anonymz_regex, domain, re.IGNORECASE):
return self._unshorten_anonymz(uri)
return uri, 200
示例7: transform_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def transform_url(url, qparams=None, **kwargs):
""" Modify url
:param url: url to transform (can be relative)
:param qparams: additional query params to add to end of url
:param kwargs: pieces of URL to modify - e.g. netloc=localhost:8000
:return: Modified URL
.. versionadded:: 3.2.0
"""
if not url:
return url
link_parse = urlsplit(url)
if qparams:
current_query = dict(parse_qsl(link_parse.query))
current_query.update(qparams)
link_parse = link_parse._replace(query=urlencode(current_query))
return urlunsplit(link_parse._replace(**kwargs))
示例8: test_spa_get
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def test_spa_get(app, client):
"""
Test 'single-page-application' style redirects
This uses json only.
"""
with capture_flashes() as flashes:
with capture_passwordless_login_requests() as requests:
response = client.post(
"/login",
json=dict(email="matt@lp.com"),
headers={"Content-Type": "application/json"},
)
assert response.headers["Content-Type"] == "application/json"
token = requests[0]["login_token"]
response = client.get("/login/" + token)
assert response.status_code == 302
split = urlsplit(response.headers["Location"])
assert "localhost:8081" == split.netloc
assert "/login-redirect" == split.path
qparams = dict(parse_qsl(split.query))
assert qparams["email"] == "matt@lp.com"
assert len(flashes) == 0
示例9: test_tf_link_spa
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def test_tf_link_spa(app, client, get_message):
# Verify two-factor required when using magic link and SPA
# This currently isn't supported and should redirect to an error.
with app.mail.record_messages() as outbox:
response = client.post(
"/us-signin/send-code",
data=dict(identity="matt@lp.com", chosen_method="email"),
follow_redirects=True,
)
assert response.status_code == 200
assert b"Sign In" in response.data
matcher = re.match(
r".*(http://[^\s*]*).*", outbox[0].body, re.IGNORECASE | re.DOTALL
)
magic_link = matcher.group(1)
response = client.get(magic_link, follow_redirects=False)
split = urlsplit(response.location)
assert "localhost:8081" == split.netloc
assert "/login-error" == split.path
qparams = dict(parse_qsl(split.query))
assert qparams["tf_required"] == "1"
assert qparams["email"] == "matt@lp.com"
示例10: test_spa_get
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def test_spa_get(app, client):
"""
Test 'single-page-application' style redirects
This uses json only.
"""
with capture_flashes() as flashes:
with capture_registrations() as registrations:
response = client.post(
"/register",
json=dict(email="dude@lp.com", password="awesome sunset"),
headers={"Content-Type": "application/json"},
)
assert response.headers["Content-Type"] == "application/json"
token = registrations[0]["confirm_token"]
response = client.get("/confirm/" + token)
assert response.status_code == 302
split = urlsplit(response.headers["Location"])
assert "localhost:8081" == split.netloc
assert "/confirm-redirect" == split.path
qparams = dict(parse_qsl(split.query))
assert qparams["email"] == "dude@lp.com"
# Arguably for json we shouldn't have any - this is buried in register_user
# but really shouldn't be.
assert len(flashes) == 1
示例11: _rewrite_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def _rewrite_url(self, request):
with self._lock:
rewrite_rules = self._rewrite_rules[:]
original_netloc = urlsplit(request.path).netloc
for pattern, replacement in rewrite_rules:
modified, count = pattern.subn(replacement, request.path)
if count > 0:
request.path = modified
break
modified_netloc = urlsplit(request.path).netloc
if original_netloc != modified_netloc:
# Modify the Host header if it exists
if 'Host' in request.headers:
request.headers['Host'] = modified_netloc
示例12: filename
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def filename(self):
'''Returns the filename to be used for saving the repo file.
The filename is derived from the repo url by injecting a suffix
after the name and before the file extension. This suffix is a
partial md5 checksum of the full repourl. This avoids multiple
repos from being written to the same file.
'''
urlpath = unquote(urlsplit(self.repourl, allow_fragments=False).path)
basename = os.path.basename(urlpath)
if not basename.endswith(REPO_SUFFIX):
basename += REPO_SUFFIX
if self.add_hash:
suffix = '-' + md5(self.repourl.encode('utf-8')).hexdigest()[:5] # nosec
else:
suffix = ''
final_name = suffix.join(os.path.splitext(basename))
return final_name
示例13: build_absolute_uri
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def build_absolute_uri(self, location=None):
"""
Build an absolute URI from the location and the variables available in
this request. If no ``location`` is specified, bulid the absolute URI
using request.get_full_path(). If the location is absolute, convert it
to an RFC 3987 compliant URI and return it. If location is relative or
is scheme-relative (i.e., ``//example.com/``), urljoin() it to a base
URL constructed from the request variables.
"""
if location is None:
# Make it an absolute url (but schemeless and domainless) for the
# edge case that the path starts with '//'.
location = '//%s' % self.get_full_path()
bits = urlsplit(location)
if not (bits.scheme and bits.netloc):
current_uri = '{scheme}://{host}{path}'.format(scheme=self.scheme,
host=self.get_host(),
path=self.path)
# Join the constructed URL with the provided location, which will
# allow the provided ``location`` to apply query strings to the
# base path as well as override the host, if it begins with //
location = urljoin(current_uri, location)
return iri_to_uri(location)
示例14: translate_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def translate_url(url, lang_code):
"""
Given a URL (absolute or relative), try to get its translated version in
the `lang_code` language (either by i18n_patterns or by translated regex).
Return the original URL if no translated version is found.
"""
parsed = urlsplit(url)
try:
match = resolve(parsed.path)
except Resolver404:
pass
else:
to_be_reversed = "%s:%s" % (match.namespace, match.url_name) if match.namespace else match.url_name
with override(lang_code):
try:
url = reverse(to_be_reversed, args=match.args, kwargs=match.kwargs)
except NoReverseMatch:
pass
else:
url = urlunsplit((parsed.scheme, parsed.netloc, url, parsed.query, parsed.fragment))
return url
示例15: stored_name
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlsplit [as 別名]
def stored_name(self, name):
parsed_name = urlsplit(unquote(name))
clean_name = parsed_name.path.strip()
hash_key = self.hash_key(clean_name)
cache_name = self.hashed_files.get(hash_key)
if cache_name is None:
if self.manifest_strict:
raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
cache_name = self.clean_name(self.hashed_name(name))
unparsed_name = list(parsed_name)
unparsed_name[2] = cache_name
# Special casing for a @font-face hack, like url(myfont.eot?#iefix")
# http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
if '?#' in name and not unparsed_name[3]:
unparsed_name[2] += '?'
return urlunsplit(unparsed_name)