本文整理匯總了Python中urllib.parse.urlunsplit方法的典型用法代碼示例。如果您正苦於以下問題:Python parse.urlunsplit方法的具體用法?Python parse.urlunsplit怎麽用?Python parse.urlunsplit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib.parse
的用法示例。
在下文中一共展示了parse.urlunsplit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: validate_
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def validate_(self, value, context=None):
url = self.valid_url(value)
if not url:
raise StopValidationError(self.messages['invalid_url'])
if self.verify_exists:
url_string = urlquote(urlunsplit((
url['scheme'],
(url['host6'] or url['host4'] or url['hostn_enc']) + ':' + (url['port'] or ''),
url['path'],
url['query'],
url['frag'])
).encode('utf-8'), safe=VALID_CHAR_STRING)
try:
urlopen(url_string)
except URLError:
raise StopValidationError(self.messages['not_found'])
示例2: transform_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [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))
示例3: _fetch_tle
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def _fetch_tle(self, path, sate_id, date=None):
url = urlparse.urljoin(self.url, path)
url = urlparse.urlparse(url)
qargs = {'satellite_number': sate_id}
if date is not None:
date_str = date.strftime("%Y-%m-%d")
qargs['date'] = date_str
query_string = urlencode(qargs)
url = urlparse.urlunsplit((url.scheme, url.netloc, url.path, query_string, url.fragment))
headers = {'user-agent': 'orbit-predictor', 'Accept': 'application/json'}
try:
response = requests.get(url, headers=headers)
except requests.exceptions.RequestException as error:
logger.error("Exception requesting TLE: %s", error)
raise
if response.ok and 'lines' in response.json():
lines = tuple(response.json()['lines'])
return lines
else:
raise ValueError("Error requesting TLE: %s", response.text)
示例4: translate_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [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
示例5: stored_name
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [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)
示例6: _send_websocket_redirect
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def _send_websocket_redirect(self, scope: dict, send: Callable) -> None:
# If the HTTP version is 2 we should redirect with a https
# scheme not wss.
scheme = "wss"
if scope.get("http_version", "1.1") == "2":
scheme = "https"
new_url = urlunsplit(
(scheme, self.host, scope["raw_path"].decode(), scope["query_string"].decode(), "")
)
await send(
{
"type": "websocket.http.response.start",
"status": 307,
"headers": [(b"location", new_url.encode())],
}
)
await send({"type": "websocket.http.response.body"})
示例7: parse_image_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def parse_image_url(url: str) -> str:
"""
Convert the image URL into a sized Discord avatar.
Parameters
----------
url : str
The URL to convert.
Returns
-------
str
The converted URL, or '' if the URL isn't in the proper format.
"""
types = [".png", ".jpg", ".gif", ".jpeg", ".webp"]
url = parse.urlsplit(url)
if any(url.path.lower().endswith(i) for i in types):
return parse.urlunsplit((*url[:3], "size=128", url[-1]))
return ""
示例8: docker_init_app
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def docker_init_app(app):
if "POSTGRES_PORT_5432_TCP_ADDR" in os.environ:
scheme = "postgresql"
host = os.environ["POSTGRES_PORT_5432_TCP_ADDR"]
user = os.environ.get("POSTGRES_ENV_POSTGRES_USER") or "postgres"
password = os.environ.get("POSTGRES_ENV_POSTGRES_PASSWORD")
db = os.environ.get("POSTGRES_ENV_POSTGRES_DB") or user
if user and password:
netloc = f"{user}:{password}@{host}"
elif user:
netloc = f"{user}@{host}"
else:
netloc = host
if not app.config.get("SQLALCHEMY_DATABASE_URI"):
app.config["SQLALCHEMY_DATABASE_URI"] = urlunsplit(
(scheme, netloc, db, None, None)
)
if "REDIS_PORT_6379_TCP_ADDR" in os.environ:
scheme = "redis"
host = os.environ["REDIS_PORT_6379_TCP_ADDR"]
port = 6379
netloc = f"{host}:{port}"
app.config.setdefault("REDIS_URL", urlunsplit((scheme, netloc, "", None, None)))
示例9: _guess_links
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def _guess_links(self):
path_segments = self.path.split("/")
maybe_contains_feed = []
maybe_feed = []
root = urlunsplit((self.scheme, self.netloc, "", "", ""))
maybe_contains_feed.append(ScoredLink(root, 0.5))
for i in range(len(path_segments)):
path = "/".join(path_segments[:i])
url = urlunsplit((self.scheme, self.netloc, path, "", ""))
maybe_contains_feed.append(ScoredLink(url, 1.0 / (i + 3)))
for k in MAYBE_FEEDS:
path = "/".join(path_segments[:i] + [k])
url = urlunsplit((self.scheme, self.netloc, path, "", ""))
maybe_feed.append(ScoredLink(url, 1.0 / (i + 4)))
links = maybe_contains_feed + maybe_feed
self._merge_links(links)
示例10: append_query_params
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def append_query_params(original_url, **kwargs):
"""
Add additional query string arguments to the given url.
Example call:
new_url = append_query_params(
original_url, error='this is an error',
another_arg='this is another argument')
"""
scheme, netloc, path, query_string, fragment = urlsplit(original_url)
query_params = parse_qs(query_string)
if kwargs is not None:
for key, value in kwargs.items():
query_params[key] = [value]
new_query_string = urlencode(query_params, doseq=True)
new_url = urlunsplit((scheme, netloc, path, new_query_string, fragment))
return new_url
示例11: _build_sitelink_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def _build_sitelink_url(site, title):
netloc_builder = []
split_index = site.find('wiki')
language = site[:split_index]
netloc_builder.append(language.replace('_', '-'))
project = site[split_index:]
if project == 'wiki':
project = 'wikipedia'
if language == 'commons':
project = 'wikimedia'
netloc_builder.append(project)
netloc_builder.append('org')
url = urlunsplit(
(
'https',
'.'.join(netloc_builder),
'/wiki/%s' % title.replace(' ', '_'),
'',
'',
)
)
LOGGER.debug('Site: %s - Title: %s - Full URL: %s', site, title, url)
return url
示例12: _send_response
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def _send_response(response_url, response_body):
try:
json_response_body = json.dumps(response_body)
except Exception as e:
msg = "Failed to convert response to json: {}".format(str(e))
logger.error(msg, exc_info=True)
response_body = {'Status': 'FAILED', 'Data': {}, 'Reason': msg}
json_response_body = json.dumps(response_body)
logger.debug("CFN response URL: {}".format(response_url))
logger.debug(json_response_body)
headers = {'content-type': '', 'content-length': str(len(json_response_body))}
split_url = urlsplit(response_url)
host = split_url.netloc
url = urlunsplit(("", "", *split_url[2:]))
while True:
try:
connection = HTTPSConnection(host)
connection.request(method="PUT", url=url, body=json_response_body, headers=headers)
response = connection.getresponse()
logger.info("CloudFormation returned status code: {}".format(response.reason))
break
except Exception as e:
logger.error("Unexpected failure sending response to CloudFormation {}".format(e), exc_info=True)
time.sleep(5)
示例13: smart_urlquote
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def smart_urlquote(url):
"Quotes a URL if it isn't already quoted."
# Handle IDN before quoting.
scheme, netloc, path, query, fragment = urlsplit(url)
try:
netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE
except UnicodeError: # invalid domain part
pass
else:
url = urlunsplit((scheme, netloc, path, query, fragment))
url = unquote(force_str(url))
# See http://bugs.python.org/issue2637
url = quote(url, safe=b'!*\'();:@&=+$,/?#[]~')
return force_text(url)
示例14: __call__
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def __call__(self, value):
try:
super(URLValidator, self).__call__(value)
except ValidationError as e:
# Trivial case failed. Try for possible IDN domain
if value:
value = force_text(value)
scheme, netloc, path, query, fragment = urlsplit(value)
try:
netloc = netloc.encode('idna').decode('ascii') # IDN -> ACE
except UnicodeError: # invalid domain part
raise e
url = urlunsplit((scheme, netloc, path, query, fragment))
super(URLValidator, self).__call__(url)
else:
raise
else:
url = value
示例15: compress
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import urlunsplit [as 別名]
def compress(self, data_list):
user = quote_plus(data_list[1])
passwd = quote_plus(data_list[2])
auth = user
if passwd:
auth += ':'
auth += passwd
parsed = urlsplit(data_list[0])
if auth:
host = auth + '@' + parsed.netloc
return urlunsplit((
parsed.scheme,
host,
parsed.path,
parsed.query,
parsed.fragment
))
return parsed.url