Python urllib.parse.urlunparse() Examples

The following are 30 code examples of urllib.parse.urlunparse(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib.parse , or try the search function .
Example #1
Source File: gs_model.py    From model_server with Apache License 2.0 6 votes vote down vote up
def get_version_files(cls, version):
        parsed_version_path = urlparse(version)
        content_list = cls.gs_list_content(version)
        xml_pattern = re.compile(
            parsed_version_path.path[1:-1] + r'/[^/]+\.xml$')
        bin_pattern = re.compile(
            parsed_version_path.path[1:-1] + r'/[^/]+\.bin$')
        xml_file = list(filter(xml_pattern.match, content_list))
        bin_file = list(filter(bin_pattern.match, content_list))
        if len(xml_file) != 0 and len(bin_file) != 0:
            if os.path.splitext(xml_file[0])[0] == \
                    os.path.splitext(bin_file[0])[0]:
                xml_file[0] = urlunparse(
                    (parsed_version_path.scheme, parsed_version_path.netloc,
                     xml_file[0], parsed_version_path.params,
                     parsed_version_path.query, parsed_version_path.fragment))
                bin_file[0] = urlunparse(
                    (parsed_version_path.scheme, parsed_version_path.netloc,
                     bin_file[0], parsed_version_path.params,
                     parsed_version_path.query, parsed_version_path.fragment))
                mapping_config = cls._get_mapping_config(version)
                return xml_file[0], bin_file[0], mapping_config
        return None, None, None 
Example #2
Source File: base.py    From zun with Apache License 2.0 6 votes vote down vote up
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 
Example #3
Source File: tiny_proxy.py    From sslyze with GNU Affero General Public License v3.0 6 votes vote down vote up
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() 
Example #4
Source File: utils.py    From normandy with Mozilla Public License 2.0 6 votes vote down vote up
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,
        )
    ) 
Example #5
Source File: flask_login.py    From jbox with MIT License 6 votes vote down vote up
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 
Example #6
Source File: base.py    From omniduct with MIT License 6 votes vote down vote up
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)))) 
Example #7
Source File: utils.py    From django-cas-ng with MIT License 6 votes vote down vote up
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_ 
Example #8
Source File: utils.py    From django-cas-ng with MIT License 6 votes vote down vote up
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 
Example #9
Source File: crawler.py    From ITWSV with MIT License 6 votes vote down vote up
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 
Example #10
Source File: api.py    From StockRecommendSystem with MIT License 6 votes vote down vote up
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)) 
Example #11
Source File: views.py    From django-sudo with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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)) 
Example #12
Source File: utils.py    From cornerwise with MIT License 6 votes vote down vote up
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)) 
Example #13
Source File: __init__.py    From uoft-scrapers with MIT License 6 votes vote down vote up
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) 
Example #14
Source File: views.py    From bioforum with MIT License 6 votes vote down vote up
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 
Example #15
Source File: send_to_providers.py    From notifications-api with MIT License 6 votes vote down vote up
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) 
Example #16
Source File: s3_model.py    From model_server with Apache License 2.0 6 votes vote down vote up
def get_version_files(cls, version):
        parsed_version_path = urlparse(version)
        content_list = cls.s3_list_content(version)
        xml_pattern = re.compile(
            parsed_version_path.path[1:-1] + r'/[^/]+\.xml$')
        bin_pattern = re.compile(
            parsed_version_path.path[1:-1] + r'/[^/]+\.bin$')
        xml_file = list(filter(xml_pattern.match, content_list))
        bin_file = list(filter(bin_pattern.match, content_list))
        if len(xml_file) != 0 and len(bin_file) != 0:
            if os.path.splitext(xml_file[0])[0] == \
                    os.path.splitext(bin_file[0])[0]:
                xml_file[0] = urlunparse(
                    (parsed_version_path.scheme, parsed_version_path.netloc,
                     xml_file[0], parsed_version_path.params,
                     parsed_version_path.query, parsed_version_path.fragment))
                bin_file[0] = urlunparse(
                    (parsed_version_path.scheme, parsed_version_path.netloc,
                     bin_file[0], parsed_version_path.params,
                     parsed_version_path.query, parsed_version_path.fragment))
                mapping_config = cls._get_mapping_config(version)
                return xml_file[0], bin_file[0], mapping_config
        return None, None, None 
Example #17
Source File: crawler.py    From ITWSV with MIT License 6 votes vote down vote up
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)) 
Example #18
Source File: client.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def urlunparse(parts):
        result = _urlunparse(tuple([p.decode("charmap") for p in parts]))
        return result.encode("charmap") 
Example #19
Source File: base.py    From hadoop-yarn-api-python-client with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def to_url(self, api_path=None):
        path = api_path or ''
        if self.port:
            result_url = urlunparse((self.scheme, self.hostname + ":" + str(self.port), path, None, None, None))
        else:
            result_url = urlunparse((self.scheme, self.hostname, path, None, None, None))

        return result_url 
Example #20
Source File: urlib.py    From Maryam with GNU General Public License v3.0 5 votes vote down vote up
def unparse(self, urparse):
		return urlparse.urlunparse(urparse) 
Example #21
Source File: pkg_resources.py    From oss-ftp with MIT License 5 votes vote down vote up
def _remove_md5_fragment(location):
    if not location:
        return ''
    parsed = urlparse(location)
    if parsed[-1].startswith('md5='):
        return urlunparse(parsed[:-1] + ('',))
    return location 
Example #22
Source File: selenium_response.py    From fetchman with Apache License 2.0 5 votes vote down vote up
def nice_join(self, url):
        url1 = urljoin(self.request.url, url)
        arr = urlparse(url1)
        path = normpath(arr[2])
        return urlunparse((arr.scheme, arr.netloc, path, arr.params, arr.query, arr.fragment)) 
Example #23
Source File: spider_response.py    From fetchman with Apache License 2.0 5 votes vote down vote up
def nice_join(self, url):
        url1 = urljoin(self.request.url, url)
        arr = urlparse(url1)
        path = normpath(arr[2])
        return urlunparse((arr.scheme, arr.netloc, path, arr.params, arr.query, arr.fragment)) 
Example #24
Source File: pkg_resources.py    From oss-ftp with MIT License 5 votes vote down vote up
def _remove_md5_fragment(location):
    if not location:
        return ''
    parsed = urlparse(location)
    if parsed[-1].startswith('md5='):
        return urlunparse(parsed[:-1] + ('',))
    return location 
Example #25
Source File: config.py    From openconnect-sso with GNU General Public License v3.0 5 votes vote down vote up
def vpn_url(self):
        parts = urlparse(self.address)
        group = self.user_group or parts.path
        if parts.path == self.address and not self.user_group:
            group = ""
        return urlunparse(
            (parts.scheme or "https", parts.netloc or self.address, group, "", "", "")
        ) 
Example #26
Source File: httputil.py    From teleport with Apache License 2.0 5 votes vote down vote up
def url_concat(url, args):
    """Concatenate url and arguments regardless of whether
    url has existing query parameters.

    ``args`` may be either a dictionary or a list of key-value pairs
    (the latter allows for multiple values with the same key.

    >>> url_concat("http://example.com/foo", dict(c="d"))
    'http://example.com/foo?c=d'
    >>> url_concat("http://example.com/foo?a=b", dict(c="d"))
    'http://example.com/foo?a=b&c=d'
    >>> url_concat("http://example.com/foo?a=b", [("c", "d"), ("c", "d2")])
    'http://example.com/foo?a=b&c=d&c=d2'
    """
    if args is None:
        return url
    parsed_url = urlparse(url)
    if isinstance(args, dict):
        parsed_query = parse_qsl(parsed_url.query, keep_blank_values=True)
        parsed_query.extend(args.items())
    elif isinstance(args, list) or isinstance(args, tuple):
        parsed_query = parse_qsl(parsed_url.query, keep_blank_values=True)
        parsed_query.extend(args)
    else:
        err = "'args' parameter should be dict, list or tuple. Not {0}".format(
            type(args))
        raise TypeError(err)
    final_query = urlencode(parsed_query)
    url = urlunparse((
        parsed_url[0],
        parsed_url[1],
        parsed_url[2],
        parsed_url[3],
        final_query,
        parsed_url[5]))
    return url 
Example #27
Source File: Implicit_auth_server.py    From auth-server-sample with Apache License 2.0 5 votes vote down vote up
def process_redirect_url(redirect_url, new_entries):
  # Prepare the redirect URL
  url_parts = list(urlparse.urlparse(redirect_url))
  queries = dict(urlparse.parse_qsl(url_parts[4]))
  queries.update(new_entries)
  url_parts[4] = urlencode(queries)
  url = urlparse.urlunparse(url_parts)
  return url 
Example #28
Source File: vcr_python_wrapper.py    From foreman-ansible-modules with GNU General Public License v3.0 5 votes vote down vote up
def filter_request_uri(request):
    request.uri = urlunparse(urlparse(request.uri)._replace(netloc="foreman.example.org"))
    return request 
Example #29
Source File: request.py    From sanic with MIT License 5 votes vote down vote up
def url(self):
        return urlunparse(
            (self.scheme, self.host, self.path, None, self.query_string, None)
        ) 
Example #30
Source File: tus.py    From tus.py with MIT License 5 votes vote down vote up
def _absolute_file_location(tus_endpoint, file_endpoint):
    parsed_file_endpoint = urlparse(file_endpoint)
    if parsed_file_endpoint.netloc:
        return file_endpoint

    parsed_tus_endpoint = urlparse(tus_endpoint)
    return urlunparse((
        parsed_tus_endpoint.scheme,
        parsed_tus_endpoint.netloc,
    ) + parsed_file_endpoint[2:])