Python urllib.parse 模块,ParseResult() 实例源码

我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用urllib.parse.ParseResult()

项目:mugen    作者:PeterDing    | 项目源码 | 文件源码
def prepare(self):
        parser = urlparse(self.url)

        scheme = parser.scheme
        host = parser.netloc
        path = parser.path
        _params = parser.params
        query = parser.query
        fragment = parser.fragment

        if self.params:
            enc_params = url_params_encode(self.params)
            query = '{}&{}'.format(query, enc_params)

        self.url_parse_result = ParseResult(
            scheme = scheme or 'http',
            netloc = host,
            path = path,
            params = _params,
            query = query,
            fragment = fragment)

        self.ssl = scheme.lower() == 'https'
项目:USTC-Software-2017    作者:igemsoftware2017    | 项目源码 | 文件源码
def add_params(url, **params):
    """
    Append or replace query params in `url` using `params`.
    """
    url = parse.unquote(url)
    parsed = parse.urlparse(url)
    existing = dict(parse.parse_qsl(parsed.query))
    existing.update(params)
    return parse.urlunparse(
        parse.ParseResult(
            scheme=parsed.scheme,
            netloc=parsed.netloc,
            path=parsed.path,
            params=parsed.params,
            query=parse.urlencode(existing),
            fragment=parsed.fragment
        )
    )
项目:USTC-Software-2017    作者:igemsoftware2017    | 项目源码 | 文件源码
def add_params(url, **params):
    """
    Append or replace query params in `url` using `params`.
    """
    url = parse.unquote(url)
    parsed = parse.urlparse(url)
    existing = dict(parse.parse_qsl(parsed.query))
    existing.update(params)
    return parse.urlunparse(
        parse.ParseResult(
            scheme=parsed.scheme,
            netloc=parsed.netloc,
            path=parsed.path,
            params=parsed.params,
            query=parse.urlencode(existing),
            fragment=parsed.fragment
        )
    )
项目:phredutils    作者:doctaphred    | 项目源码 | 文件源码
def alter_url(url, **components):
    """
    >>> alter_url('http://placekitten.com', path='200/300')
    'http://placekitten.com/200/300'
    """
    original = urlparse(url)._asdict()
    original.update(components)
    return ParseResult(**original).geturl()
项目:asyncqlio    作者:SunDwarf    | 项目源码 | 文件源码
def __init__(self, dsn: ParseResult, *, loop: asyncio.AbstractEventLoop = None):
        """
        :param dsn: The :class:`urllib.parse.ParseResult` created from parsing a DSN.
        """
        self.loop = loop or asyncio.get_event_loop()

        self._parse_result = dsn
        self.dsn = dsn.geturl()
        self.host = dsn.hostname
        self.port = dsn.port
        self.username = dsn.username
        self.password = dsn.password
        self.db = dsn.path[1:]
        self.params = {k: v[0] for k, v in parse_qs(dsn.query).items()}
项目:git-repo    作者:guyzmo    | 项目源码 | 文件源码
def build_url(obj):
        scheme = getattr(obj, 'scheme', 'https')
        port = getattr(obj, 'port', None)
        # skip useless port specification
        if (not port
            or scheme == 'https' and str(port) == '443'
            or scheme == 'http' and str(port) == '80'):
            netloc = obj.fqdn
        else:
            netloc = '{}:{}'.format(obj.fqdn, port)
        return ParseResult(scheme, netloc, *['']*4).geturl()
项目:DLink_Harvester    作者:MikimotoH    | 项目源码 | 文件源码
def safeUrl(url:str)->str:
    from urllib import parse
    pr = parse.urlparse(url)
    pr2 = parse.ParseResult(scheme=pr.scheme, netloc=pr.netloc,
                            path=parse.quote(pr.path,'/%'), params=pr.params,
                            query=pr.query, fragment=pr.fragment)
    return parse.urlunparse(pr2)
项目:upnpclient    作者:flyte    | 项目源码 | 文件源码
def test_marshal_uri(self):
        """
        Should parse a 'uri' value into a `ParseResult`.
        """
        uri = 'https://media.giphy.com/media/22kxQ12cxyEww/giphy.gif?something=variable'
        marshalled, val = upnp.marshal.marshal_value('uri', uri)
        self.assertTrue(marshalled)
        self.assertIsInstance(val, ParseResult)
项目:python-phoenixdb    作者:lalinsky    | 项目源码 | 文件源码
def parse_url(url):
    url = urlparse.urlparse(url)
    if not url.scheme and not url.netloc and url.path:
        netloc = url.path
        if ':' not in netloc:
            netloc = '{}:8765'.format(netloc)
        return urlparse.ParseResult('http', netloc, '/', '', '', '')
    return url


# Defined in phoenix-core/src/main/java/org/apache/phoenix/exception/SQLExceptionCode.java
项目:data-hub-backend    作者:uktrade-attic    | 项目源码 | 文件源码
def __to_url(url):
    if isinstance(url, __urlparse.ParseResult):
        return url
    return __urlparse.urlparse(url)
项目:deb-python-jingo    作者:openstack    | 项目源码 | 文件源码
def urlparams(url_, fragment=None, query_dict=None, **query):
    """
Add a fragment and/or query parameters to a URL.

New query params will be appended to exising parameters, except duplicate
names, which will be replaced.
"""
    url_ = urlparse.urlparse(url_)
    fragment = fragment if fragment is not None else url_.fragment

    q = url_.query
    new_query_dict = (QueryDict(smart_str(q), mutable=True) if
                      q else QueryDict('', mutable=True))
    if query_dict:
        for k, l in query_dict.lists():
            new_query_dict[k] = None  # Replace, don't append.
            for v in l:
                new_query_dict.appendlist(k, v)

    for k, v in query.items():
        # Replace, don't append.
        if isinstance(v, list):
            new_query_dict.setlist(k, v)
        else:
            new_query_dict[k] = v

    query_string = urlencode([(k, v) for k, l in new_query_dict.lists() for
                              v in l if v is not None])
    new = urlparse.ParseResult(url_.scheme, url_.netloc, url_.path,
                               url_.params, query_string, fragment)
    return new.geturl()
项目:slim    作者:avalentino    | 项目源码 | 文件源码
def make_support_link(email, appname='SLiM'):
    support_link_parts = ParseResult(
        scheme='mailto',
        netloc='',
        path=email,
        params='',
        query='subject=' + quote('[%s] Support request' % appname),
        # query=urlencode({'subject': '[%s] Support request' % appname},
        #                 quote_via=quote),
        fragment='',
    )
    return urlunparse(support_link_parts)
项目:slim    作者:avalentino    | 项目源码 | 文件源码
def sqlite_uri_for(path):
    uri_parts = ParseResult(
        scheme='sqlite',
        netloc='/',
        path=path,
        params='',
        query='',
        fragment='')

    return urlunparse(uri_parts)
项目:caldav    作者:python-caldav    | 项目源码 | 文件源码
def __init__(self, url):
        if isinstance(url, ParseResult) or isinstance(url, SplitResult):
            self.url_parsed = url
            self.url_raw = None
        else:
            self.url_raw = url
            self.url_parsed = None
项目:caldav    作者:python-caldav    | 项目源码 | 文件源码
def objectify(self, url):
        if url is None:
            return None
        if isinstance(url, URL):
            return url
        else:
            return URL(url)

    # To deal with all kind of methods/properties in the ParseResult
    # class
项目:caldav    作者:python-caldav    | 项目源码 | 文件源码
def unauth(self):
        if not self.is_auth():
            return self
        return URL.objectify(ParseResult(
            self.scheme,
            '%s:%s' % (self.hostname,
                       self.port or {'https': 443, 'http': 80}[self.scheme]),
            self.path.replace('//', '/'), self.params, self.query,
            self.fragment))
项目:caldav    作者:python-caldav    | 项目源码 | 文件源码
def join(self, path):
        """
        assumes this object is the base URL or base path.  If the path
        is relative, it should be appended to the base.  If the path
        is absolute, it should be added to the connection details of
        self.  If the path already contains connection details and the
        connection details differ from self, raise an error.
        """
        pathAsString = str(path)
        if not path or not pathAsString:
            return self
        path = URL.objectify(path)
        if (
            (path.scheme and self.scheme and path.scheme != self.scheme) or
            (path.hostname and self.hostname and
             path.hostname != self.hostname) or
            (path.port and self.port and path.port != self.port)
        ):
            raise ValueError("%s can't be joined with %s" % (self, path))

        if path.path[0] == '/':
            ret_path = uc2utf8(path.path)
        else:
            sep = "/"
            if self.path.endswith("/"):
                sep = ""
            ret_path = "%s%s%s" % (self.path, sep, uc2utf8(path.path))
        return URL(ParseResult(
            self.scheme or path.scheme, self.netloc or path.netloc, ret_path,
            path.params, path.query, path.fragment))
项目:environs    作者:sloria    | 项目源码 | 文件源码
def test_url_cast(self, set_env, env):
        set_env({'URL': 'http://stevenloria.com/projects/?foo=42'})
        res = env.url('URL')
        assert isinstance(res, urlparse.ParseResult)