本文整理汇总了Python中urllib.parse.urlunparse方法的典型用法代码示例。如果您正苦于以下问题:Python parse.urlunparse方法的具体用法?Python parse.urlunparse怎么用?Python parse.urlunparse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.parse
的用法示例。
在下文中一共展示了parse.urlunparse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_link
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def validate_link(self, link, bookmark=False):
"""Checks if the given link can get correct data."""
# removes the scheme and net location parts of the link
url_parts = list(urlparse.urlparse(link))
url_parts[0] = url_parts[1] = ''
# bookmark link should not have the version in the URL
if bookmark and url_parts[2].startswith(PATH_PREFIX):
return False
full_path = urlparse.urlunparse(url_parts)
try:
self.get_json(full_path, path_prefix='')
return True
except Exception:
return False
示例2: do_GET
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def do_GET(self):
(scm, netloc, path, params, query, fragment) = urlparse(self.path, "http")
if scm != "http" or fragment or not netloc:
self.send_error(400, "bad url %s" % self.path)
return
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
if self._connect_to(netloc, soc):
self.log_request()
soc.send(
"%s %s %s\r\n" % (self.command, urlunparse(("", "", path, params, query, "")), self.request_version)
)
self.headers["Connection"] = "close"
del self.headers["Proxy-Connection"]
for key_val in self.headers.items():
soc.send("%s: %s\r\n" % key_val)
soc.send("\r\n")
self._read_write(soc)
finally:
logging.warning("Finished do_GET()")
soc.close()
self.connection.close()
示例3: urlparams
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def urlparams(url, fragment=None, **kwargs):
"""
Add a fragment and/or query parameters to a URL.
Existing query string parameters are preserved, unless they conflict
with the new parameters, in which case they're overridden.
"""
parsed = urlparse(url)
query = dict(parse_qs(parsed.query), **kwargs)
return urlunparse(
(
parsed.scheme,
parsed.netloc,
parsed.path,
parsed.params,
urlencode(query, doseq=True),
fragment if fragment is not None else parsed.fragment,
)
)
示例4: make_next_param
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def make_next_param(login_url, current_url):
'''
Reduces the scheme and host from a given URL so it can be passed to
the given `login` URL more efficiently.
:param login_url: The login URL being redirected to.
:type login_url: str
:param current_url: The URL to reduce.
:type current_url: str
'''
l = urlparse(login_url)
c = urlparse(current_url)
if (not l.scheme or l.scheme == c.scheme) and \
(not l.netloc or l.netloc == c.netloc):
return urlunparse(('', '', c.path, c.params, c.query, ''))
return current_url
示例5: get_local_uri
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def get_local_uri(self, uri):
"""
Convert a remote uri to a local one.
This method takes a remote service uri accessible to the remote host and
returns a local uri accessible directly on the local host, establishing
any necessary port forwarding in the process.
Args:
uri (str): The remote uri to be made local.
Returns:
str: A local uri that tunnels all traffic to the remote host.
"""
parsed_uri = urlparse(uri)
return urlunparse(parsed_uri._replace(netloc='localhost:{}'.format(self.port_forward(parsed_uri.netloc))))
示例6: get_redirect_url
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def get_redirect_url(request):
"""Redirects to referring page, or CAS_REDIRECT_URL if no referrer is
set.
"""
next_ = request.GET.get(REDIRECT_FIELD_NAME)
if not next_:
redirect_url = resolve_url(django_settings.CAS_REDIRECT_URL)
if django_settings.CAS_IGNORE_REFERER:
next_ = redirect_url
else:
next_ = request.META.get('HTTP_REFERER', redirect_url)
prefix = urllib_parse.urlunparse(
(get_protocol(request), request.get_host(), '', '', '', ''),
)
if next_.startswith(prefix):
next_ = next_[len(prefix):]
return next_
示例7: get_service_url
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def get_service_url(request, redirect_to=None):
"""Generates application django service URL for CAS"""
if hasattr(django_settings, 'CAS_ROOT_PROXIED_AS'):
service = django_settings.CAS_ROOT_PROXIED_AS + request.path
else:
if django_settings.CAS_FORCE_SSL_SERVICE_URL:
protocol = 'https'
else:
protocol = get_protocol(request)
host = request.get_host()
service = urllib_parse.urlunparse(
(protocol, host, request.path, '', '', ''),
)
if not django_settings.CAS_STORE_NEXT:
if '?' in service:
service += '&'
else:
service += '?'
service += urllib_parse.urlencode({
REDIRECT_FIELD_NAME: redirect_to or get_redirect_url(request)
})
return service
示例8: _scripts
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def _scripts(self):
url_parts = urlparse(self._base or self._url)
scheme = url_parts.scheme
for tag in self.soup.find_all("script", src=True):
parts = urlparse(tag["src"])
if parts.scheme:
if parts.netloc:
# Full absolute URL
script_url = urlunparse((parts.scheme, parts.netloc, parts.path, parts.params, parts.query, ''))
else:
# Malformed absolute URL (no host)
continue
elif parts.netloc:
# Protocol relative URL
script_url = urlunparse((scheme, parts.netloc, parts.path, parts.params, parts.query, ''))
else:
# Internal relative URL
script_url = urlunparse(('', '', parts.path, parts.params, parts.query, ''))
yield script_url
示例9: set_proxy
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def set_proxy(self, proxy=""):
"""Set a proxy to use for HTTP requests."""
url_parts = urlparse(proxy)
protocol = url_parts.scheme.lower()
if protocol in ("http", "https", "socks"):
if protocol == "socks":
# socks5h proxy type won't leak DNS requests
proxy = urlunparse(("socks5h", url_parts.netloc, '/', '', '', ''))
else:
proxy = urlunparse((url_parts.scheme, url_parts.netloc, '/', '', '', ''))
# attach the proxy for http and https URLs
self._session.proxies["http"] = proxy
self._session.proxies["https"] = proxy
else:
raise ValueError("Unknown proxy type '{}'".format(protocol))
示例10: redirect_to_login
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
"""
Redirect the user to the login page, passing the given 'next' page.
"""
resolved_url = resolve_url(login_url or settings.LOGIN_URL)
login_url_parts = list(urlparse(resolved_url))
if redirect_field_name:
querystring = QueryDict(login_url_parts[4], mutable=True)
querystring[redirect_field_name] = next
login_url_parts[4] = querystring.urlencode(safe='/')
return HttpResponseRedirect(urlunparse(login_url_parts))
# 4 views for password reset:
# - password_reset sends the mail
# - password_reset_done shows a success message for the above
# - password_reset_confirm checks the link the user clicked and
# prompts for a new password
# - password_reset_complete shows a success message for the above
示例11: _BuildUrl
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def _BuildUrl(self, url, path_elements=None, extra_params=None):
# Break url into constituent parts
(scheme, netloc, path, params, query, fragment) = urlparse(url)
# Add any additional path elements to the path
if path_elements:
# Filter out the path elements that have a value of None
p = [i for i in path_elements if i]
if not path.endswith('/'):
path += '/'
path += '/'.join(p)
# Add any additional query parameters to the query string
if extra_params and len(extra_params) > 0:
extra_query = self._EncodeParameters(extra_params)
# Add it to the existing query
if query:
query += '&' + extra_query
else:
query = extra_query
# Return the rebuilt URL
return urlunparse((scheme, netloc, path, params, query, fragment))
示例12: redirect_to_sudo
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def redirect_to_sudo(next_url, sudo_url=None):
"""
Redirects the user to the login page, passing the given 'next' page
"""
if sudo_url is None:
sudo_url = URL
try:
# django 1.10 and greater can't resolve the string 'sudo.views.sudo' to a URL
# https://docs.djangoproject.com/en/1.10/releases/1.10/#removed-features-1-10
sudo_url = import_string(sudo_url)
except (ImportError, ImproperlyConfigured):
pass # wasn't a dotted path
sudo_url_parts = list(urlparse(resolve_url(sudo_url)))
querystring = QueryDict(sudo_url_parts[4], mutable=True)
querystring[REDIRECT_FIELD_NAME] = next_url
sudo_url_parts[4] = querystring.urlencode(safe="/")
return HttpResponseRedirect(urlunparse(sudo_url_parts))
示例13: add_params
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def add_params(url, extra=None, remove=None):
"""Given a URL, add new query parameters by merging in the contents of the
`extra` dictionary.
:param url: (str)
:param extra: (dict)
:param remove: (list or set)
:returns: (str) URL including new parameters
"""
if not (extra or remove):
return url
parsed = parse.urlparse(url)._asdict()
params = parse.parse_qsl(parsed["query"])
if extra:
params += list(extra.items())
if remove:
params = [pair for pair in params if pair[0] not in remove]
parsed["query"] = parse.urlencode(params, doseq=True)
return parse.urlunparse(parse.ParseResult(**parsed))
示例14: get_events_list
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [as 别名]
def get_events_list():
page_index_url = Events.host + 'index.php'
url_parts = list(urlparse.urlparse(page_index_url))
events_links, events_dates = [], []
paging_index = 1
events_count = 10
while events_count == 10:
params = {
'p': paging_index
}
url_parts[4] = urlencode(params)
paging_index += 1
html = Scraper.get(urlparse.urlunparse(url_parts))
soup = BeautifulSoup(html, 'html.parser')
events_dom_arr = soup.select('#results')[0].find_all('li')
events_count = len(events_dom_arr)
events_links += list(map(lambda e: e.a['href'], events_dom_arr))
events_dates += list(map(lambda e: e.find('p').text.split(' : ')[1].split(', ')[0], events_dom_arr))
return zip(events_links, events_dates)
示例15: get_logo_url
# 需要导入模块: from urllib import parse [as 别名]
# 或者: from urllib.parse import urlunparse [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)