当前位置: 首页>>代码示例>>Python>>正文


Python URL.build方法代码示例

本文整理汇总了Python中yarl.URL.build方法的典型用法代码示例。如果您正苦于以下问题:Python URL.build方法的具体用法?Python URL.build怎么用?Python URL.build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在yarl.URL的用法示例。


在下文中一共展示了URL.build方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_build_with_query_and_query_string

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_with_query_and_query_string():
    with pytest.raises(ValueError):
        URL.build(
            scheme="http",
            host="127.0.0.1",
            user="foo",
            password="bar",
            port=8000,
            path="/index.html",
            query=dict(arg="value1"),
            query_string="arg=value1",
            fragment="top",
        )
开发者ID:asvetlov,项目名称:yarl,代码行数:15,代码来源:test_url_build.py

示例2: test_build_with_query_and_query_string

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_with_query_and_query_string():
    with pytest.raises(ValueError):
        URL.build(
            scheme='http',
            host='127.0.0.1',
            user='foo',
            password='bar',
            port=8000,
            path='/index.html',
            query=dict(arg="value1"),
            query_string="arg=value1",
            fragment="top"
        )
开发者ID:stasfilin,项目名称:yarl,代码行数:15,代码来源:test_url_build.py

示例3: __init__

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
    def __init__(self, path: str, *, name: Optional[str]=None) -> None:
        super().__init__(name=name)
        pattern = ''
        formatter = ''
        for part in ROUTE_RE.split(path):
            match = self.DYN.fullmatch(part)
            if match:
                pattern += '(?P<{}>{})'.format(match.group('var'), self.GOOD)
                formatter += '{' + match.group('var') + '}'
                continue

            match = self.DYN_WITH_RE.fullmatch(part)
            if match:
                pattern += '(?P<{var}>{re})'.format(**match.groupdict())
                formatter += '{' + match.group('var') + '}'
                continue

            if '{' in part or '}' in part:
                raise ValueError("Invalid path '{}'['{}']".format(path, part))

            path = URL.build(path=part).raw_path
            formatter += path
            pattern += re.escape(path)

        try:
            compiled = re.compile(pattern)
        except re.error as exc:
            raise ValueError(
                "Bad pattern '{}': {}".format(pattern, exc)) from None
        assert compiled.pattern.startswith(PATH_SEP)
        assert formatter.startswith('/')
        self._pattern = compiled
        self._formatter = formatter
开发者ID:KeepSafe,项目名称:aiohttp,代码行数:35,代码来源:web_urldispatcher.py

示例4: _match

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
 def _match(self, path: str) -> Optional[Dict[str, str]]:
     match = self._pattern.fullmatch(path)
     if match is None:
         return None
     else:
         return {key: URL.build(path=value, encoded=True).path
                 for key, value in match.groupdict().items()}
开发者ID:KeepSafe,项目名称:aiohttp,代码行数:9,代码来源:web_urldispatcher.py

示例5: test_build_drop_dots

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_drop_dots():
    u = URL.build(
        scheme='http',
        host='example.com',
        path='/path/../to',
    )
    assert str(u) == 'http://example.com/to'
开发者ID:stasfilin,项目名称:yarl,代码行数:9,代码来源:test_url_build.py

示例6: url_for

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
    def url_for(self, *, filename: Union[str, Path],  # type: ignore
                append_version: Optional[bool]=None) -> URL:
        if append_version is None:
            append_version = self._append_version
        if isinstance(filename, Path):
            filename = str(filename)
        while filename.startswith('/'):
            filename = filename[1:]
        filename = '/' + filename

        # filename is not encoded
        url = URL.build(path=self._prefix + filename)

        if append_version:
            try:
                if filename.startswith('/'):
                    filename = filename[1:]
                filepath = self._directory.joinpath(filename).resolve()
                if not self._follow_symlinks:
                    filepath.relative_to(self._directory)
            except (ValueError, FileNotFoundError):
                # ValueError for case when path point to symlink
                # with follow_symlinks is False
                return url  # relatively safe
            if filepath.is_file():
                # TODO cache file content
                # with file watcher for cache invalidation
                with open(str(filepath), mode='rb') as f:
                    file_bytes = f.read()
                h = self._get_file_hash(file_bytes)
                url = url.with_query({self.VERSION_KEY: h})
                return url
        return url
开发者ID:KeepSafe,项目名称:aiohttp,代码行数:35,代码来源:web_urldispatcher.py

示例7: test_query_str

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_query_str():
    u = URL.build(
        scheme='http',
        host='127.0.0.1',
        path='/',
        query_string="arg=value1"
    )
    assert str(u) == 'http://127.0.0.1/?arg=value1'
开发者ID:stasfilin,项目名称:yarl,代码行数:10,代码来源:test_url_build.py

示例8: test_build_query_quoting

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_query_quoting():
    u = URL.build(scheme="http", host="127.0.0.1", path="/файл.jpg", query="arg=Привет")

    assert u == URL("http://127.0.0.1/файл.jpg?arg=Привет")
    assert str(u) == (
        "http://127.0.0.1/%D1%84%D0%B0%D0%B9%D0%BB.jpg?"
        "arg=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82"
    )
开发者ID:asvetlov,项目名称:yarl,代码行数:10,代码来源:test_url_build.py

示例9: test_build_path_quoting

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_path_quoting():
    u = URL.build(
        scheme='http',
        host='127.0.0.1',
        path='/файл.jpg',
        query=dict(arg="Привет")
    )

    assert u == URL('http://127.0.0.1/файл.jpg?arg=Привет')
    assert str(u) == ('http://127.0.0.1/%D1%84%D0%B0%D0%B9%D0%BB.jpg?'
                      'arg=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82')
开发者ID:stasfilin,项目名称:yarl,代码行数:13,代码来源:test_url_build.py

示例10: test_build_already_encoded

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_already_encoded():
    # resulting URL is invalid but not encoded
    u = URL.build(
        scheme="http",
        host="историк.рф",
        path="/путь/файл",
        query_string="ключ=знач",
        fragment="фраг",
        encoded=True,
    )
    assert str(u) == "http://историк.рф/путь/файл?ключ=знач#фраг"
开发者ID:asvetlov,项目名称:yarl,代码行数:13,代码来源:test_url_build.py

示例11: test_build_with_all

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_with_all():
    u = URL.build(
        scheme="http",
        host="127.0.0.1",
        user="foo",
        password="bar",
        port=8000,
        path="/index.html",
        query_string="arg=value1",
        fragment="top",
    )
    assert str(u) == "http://foo:[email protected]:8000/index.html?arg=value1#top"
开发者ID:asvetlov,项目名称:yarl,代码行数:14,代码来源:test_url_build.py

示例12: test_build_with_all

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_with_all():
    u = URL.build(
        scheme='http',
        host='127.0.0.1',
        user='foo',
        password='bar',
        port=8000,
        path='/index.html',
        query_string="arg=value1",
        fragment="top"
    )
    assert str(u) == 'http://foo:[email protected]:8000/index.html?arg=value1#top'
开发者ID:stasfilin,项目名称:yarl,代码行数:14,代码来源:test_url_build.py

示例13: __init__

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
 def __init__(self, runner, sock, *,
              shutdown_timeout=60.0, ssl_context=None,
              backlog=128):
     super().__init__(runner, shutdown_timeout=shutdown_timeout,
                      ssl_context=ssl_context, backlog=backlog)
     self._sock = sock
     scheme = 'https' if self._ssl_context else 'http'
     if hasattr(socket, 'AF_UNIX') and sock.family == socket.AF_UNIX:
         name = '{}://unix:{}:'.format(scheme, sock.getsockname())
     else:
         host, port = sock.getsockname()[:2]
         name = str(URL.build(scheme=scheme, host=host, port=port))
     self._name = name
开发者ID:3lnc,项目名称:aiohttp,代码行数:15,代码来源:web_runner.py

示例14: resolve

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
    async def resolve(self, request: Request) -> _Resolve:
        path = request.rel_url.raw_path
        method = request.method
        allowed_methods = set(self._routes)
        if not path.startswith(self._prefix):
            return None, set()

        if method not in allowed_methods:
            return None, allowed_methods

        match_dict = {'filename': URL.build(path=path[len(self._prefix)+1:],
                                            encoded=True).path}
        return (UrlMappingMatchInfo(match_dict, self._routes[method]),
                allowed_methods)
开发者ID:KeepSafe,项目名称:aiohttp,代码行数:16,代码来源:web_urldispatcher.py

示例15: test_build_encode

# 需要导入模块: from yarl import URL [as 别名]
# 或者: from yarl.URL import build [as 别名]
def test_build_encode():
    u = URL.build(
        scheme="http",
        host="историк.рф",
        path="/путь/файл",
        query_string="ключ=знач",
        fragment="фраг",
    )
    expected = (
        "http://xn--h1aagokeh.xn--p1ai"
        "/%D0%BF%D1%83%D1%82%D1%8C/%D1%84%D0%B0%D0%B9%D0%BB"
        "?%D0%BA%D0%BB%D1%8E%D1%87=%D0%B7%D0%BD%D0%B0%D1%87"
        "#%D1%84%D1%80%D0%B0%D0%B3"
    )
    assert str(u) == expected
开发者ID:asvetlov,项目名称:yarl,代码行数:17,代码来源:test_url_build.py


注:本文中的yarl.URL.build方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。