本文整理匯總了Python中urllib.parse.ParseResult方法的典型用法代碼示例。如果您正苦於以下問題:Python parse.ParseResult方法的具體用法?Python parse.ParseResult怎麽用?Python parse.ParseResult使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib.parse
的用法示例。
在下文中一共展示了parse.ParseResult方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: urlparams
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def urlparams(url_, hash=None, **query):
"""Add a fragment and/or query paramaters to a URL.
New query params will be appended to exising parameters, except duplicate
names, which will be replaced.
"""
url = urlparse.urlparse(url_)
fragment = hash if hash is not None else url.fragment
# Use dict(parse_qsl) so we don't get lists of values.
query_dict = dict(urlparse.parse_qsl(url.query))
query_dict.update(query)
query_string = urlencode(
[(k, v) for k, v in query_dict.items() if v is not None])
new = urlparse.ParseResult(url.scheme, url.netloc, url.path, url.params,
query_string, fragment)
return new.geturl()
示例2: url_to_string
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def url_to_string(url):
"""Convert url from ``list`` or ``ParseResult`` to string."""
if isinstance(url, list):
return ParseResult(
scheme=url[0],
netloc=url[1],
path=url[2],
params=None,
query=None,
fragment=None,
).geturl()
if isinstance(url, ParseResult):
return url.geturl()
if isinstance(url, str):
return url
raise ValueError('url value not recognized')
示例3: get_logo_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def get_logo_url(base_url, logo_file):
base_url = parse.urlparse(base_url)
netloc = base_url.netloc
if base_url.netloc.startswith('localhost'):
netloc = 'notify.tools'
elif base_url.netloc.startswith('www'):
# strip "www."
netloc = base_url.netloc[4:]
logo_url = parse.ParseResult(
scheme=base_url.scheme,
netloc='static-logos.' + netloc,
path=logo_file,
params=base_url.params,
query=base_url.query,
fragment=base_url.fragment
)
return parse.urlunparse(logo_url)
示例4: _uri_sort_query
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def _uri_sort_query(cls, uri_parsed: UrlParseResult) -> UrlParseResult:
if uri_parsed.query == '':
return uri_parsed
query_dict = parse_qs(uri_parsed.query, keep_blank_values=True)
if 'variables' not in query_dict:
return uri_parsed
variables = query_dict['variables'][0]
variables_dict = json_loads(variables)
variables_dict_sorted = OrderedDict((k, variables_dict[k]) for k in sorted(variables_dict))
query_dict['variables'][0] = json_dumps(variables_dict_sorted)
query_sorted = urlencode(query_dict, doseq=True)
return super()._uri_sort_query(UrlParseResult(
uri_parsed.scheme,
uri_parsed.netloc,
uri_parsed.path,
uri_parsed.params,
query_sorted,
uri_parsed.fragment
))
示例5: geturl
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def geturl(self, include_params=True):
params = self.params
query = self.query
fragment = self.fragment
if not include_params:
params = ""
query = ""
fragment = ""
r = ParseResult(scheme=self.scheme,
netloc=self.netloc,
path=self.path,
params=params,
query=query,
fragment=fragment)
return r.geturl()
示例6: s3_exists
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def s3_exists(url: ParseResult) -> bool:
"""Return is an S3 resource exists.
Parameters
----------
url: ParseResult
The parsed URL.
Returns
-------
bool
True if it exists. False otherwise.
"""
s3 = boto3.resource('s3')
try:
bucket = s3.Bucket(url.netloc)
path = url.path[1:] # Not consider starting '/'
objs = list(bucket.objects.filter(Prefix=path))
return len(objs) > 0
except s3.meta.client.exceptions.NoSuchBucket:
return False
示例7: s3_remote_file
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def s3_remote_file(url: ParseResult) -> bool:
"""Check if an existing S3 hosted artifact is a file or a folder.
Parameters
----------
url: ParseResult
The parsed URL.
Returns
-------
bool
True if it's a file, False if it's a folder.
"""
s3 = boto3.resource('s3')
bucket = s3.Bucket(url.netloc)
path = url.path[1:] # Not consider starting '/'
objs = list(bucket.objects.filter(Prefix=path))
if len(objs) == 1 and objs[0].key == path:
return True
return False
示例8: build_request
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def build_request(self, path=None, headers=None, endpoint=None):
headers = {} if headers is None else headers
if endpoint is None:
endpoint = self._endpoint
if path is None:
url = endpoint
else:
p = urlparse.urlparse(endpoint)
# should not use `os.path.join` since it returns path string like "/foo\\bar"
request_path = path if p.path == "/" else "/".join([p.path, path])
url = urlparse.urlunparse(
urlparse.ParseResult(
p.scheme, p.netloc, request_path, p.params, p.query, p.fragment
)
)
# use default headers first
_headers = dict(self._headers)
# add default headers
_headers["authorization"] = "TD1 %s" % (self._apikey,)
_headers["date"] = email.utils.formatdate(time.time())
_headers["user-agent"] = self._user_agent
# override given headers
_headers.update({key.lower(): value for (key, value) in headers.items()})
return (url, _headers)
示例9: fetch_api_description
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def fetch_api_description(
url: typing.Union[str, ParseResult, SplitResult], insecure: bool = False
):
"""Fetch the API description from the remote MAAS instance."""
url_describe = urljoin(_ensure_url_string(url), "describe/")
connector = aiohttp.TCPConnector(verify_ssl=(not insecure))
session = aiohttp.ClientSession(connector=connector)
async with session, session.get(url_describe) as response:
if response.status != HTTPStatus.OK:
raise RemoteError("{0} -> {1.status} {1.reason}".format(url, response))
elif response.content_type != "application/json":
raise RemoteError(
"Expected application/json, got: %s" % response.content_type
)
else:
return await response.json()
示例10: __init__
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def __init__(self, urlobj: ParseResult):
self.kind = 'http'
host = urlobj.hostname
if urlobj.port:
host += ':{}'.format(urlobj.port)
self.url = '{}://{}{}'.format(urlobj.scheme, host, urlobj.path)
if urlobj.username or urlobj.password:
self.auth = (urlobj.username, urlobj.password)
self.nuclio_header = basic_auth_header(urlobj.username,
urlobj.password)
else:
self.auth = None
self.nuclio_header = None
self.path = urlobj.path
self.workdir = urlobj.fragment
示例11: dispatch
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def dispatch(self, request, *args, **kwargs):
if not self.test_func_tou_required(request.user):
messages.add_message(
request,
messages.INFO,
"You need to agree to the terms of use before you can do that.",
)
# Remember where they were trying to go, so we can redirect them
# back after they agree. There's logic in TermsView to pick up on
# this parameter.
next_path = request.path
next_param = urlencode({REDIRECT_FIELD_NAME: next_path})
path = reverse_lazy("terms")
new_url = ParseResult(
scheme="",
netloc="",
path=str(path),
params="",
query=str(next_param),
fragment="",
).geturl()
return HttpResponseRedirect(new_url)
return super(ToURequired, self).dispatch(request, *args, **kwargs)
示例12: get_payable_from_BIP21URI
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def get_payable_from_BIP21URI(uri: str, proto: str = "bitcoin") -> Tuple[str, Decimal]:
""" Computes a 'payable' tuple from a given BIP21 encoded URI.
:param uri: The BIP21 URI to decode
:param proto: The expected protocol/scheme (case insensitive)
:returns: A payable (address, amount) corresponding to the given URI
:raise: Raises s ValueError if there is no address given or if the
protocol/scheme doesn't match what is expected
"""
obj = parse.urlparse(uri) # type: parse.ParseResult
if not obj.path or obj.scheme.upper() != proto.upper():
raise ValueError("Malformed URI")
if not obj.query:
return obj.path, None
query = parse.parse_qs(obj.query) # type: Dict
return obj.path, Decimal(query["amount"][0])
示例13: get_direct_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def get_direct_url(url, headers):
"""Gets the zip direct download link from the project download page"""
direct_download_url = href_from_link_text(url,
headers,
'Problems Downloading')
parsed_download_url = urlparse(direct_download_url)
if parsed_download_url.scheme not in ['http', 'https']:
# url is relative, and is missing the scheme and netloc
parsed_parent_url = urlparse(url)
parsed_download_url = ParseResult(parsed_parent_url.scheme,
parsed_parent_url.netloc,
parsed_download_url.path,
parsed_download_url.params,
parsed_download_url.query,
parsed_download_url.fragment)
direct_download_url = parsed_download_url.geturl()
direct_download_url = href_from_link_text(direct_download_url,
headers,
'direct link')
return direct_download_url
示例14: __init__
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def __init__(self, system_ids, sources, proxy=None):
"""Create a new importer.
:param system_ids: A sequence of rack controller system_id's.
:param sources: A sequence of endpoints; see `ImportBootImages`.
:param proxy: The HTTP/HTTPS proxy to use, or `None`
:type proxy: :class:`urlparse.ParseResult` or string
"""
super().__init__()
self.system_ids = tuple(flatten(system_ids))
if isinstance(sources, Sequence):
self.sources = sources
else:
raise TypeError("expected sequence, got: %r" % (sources,))
if proxy is None or isinstance(proxy, ParseResult):
self.proxy = proxy
else:
self.proxy = urlparse(proxy)
示例15: base_url
# 需要導入模塊: from urllib import parse [as 別名]
# 或者: from urllib.parse import ParseResult [as 別名]
def base_url(self) -> str:
"""Returns the base url without query string or fragments."""
return urlunparse(ParseResult(self.scheme, self.host, self.path, "", "", ""))