How to use the httpx.backends.base.lookup_backend function in httpx

To help you get started, weā€™ve selected a few httpx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / httpx / tests / test_concurrency.py View on Github external
def test_lookup_backend():
    assert isinstance(lookup_backend("asyncio"), AsyncioBackend)
    assert isinstance(lookup_backend("trio"), TrioBackend)
    assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)

    async def get_backend_from_auto():
        auto_backend = lookup_backend("auto")
        return auto_backend.backend

    loop = asyncio.get_event_loop()
    backend = loop.run_until_complete(get_backend_from_auto())
    assert isinstance(backend, AsyncioBackend)

    backend = trio.run(get_backend_from_auto)
    assert isinstance(backend, TrioBackend)

    with pytest.raises(Exception, match="unknownio"):
        lookup_backend("unknownio")
github encode / httpx / tests / test_concurrency.py View on Github external
async def test_start_tls_on_tcp_socket_stream(https_server):
    backend = lookup_backend()
    ctx = SSLConfig().load_ssl_context_no_verify()
    timeout = Timeout(5)

    stream = await backend.open_tcp_stream(
        https_server.url.host, https_server.url.port, None, timeout
    )

    try:
        assert stream.is_connection_dropped() is False
        assert get_cipher(backend, stream) is None

        stream = await stream.start_tls(https_server.url.host, ctx, timeout)
        assert stream.is_connection_dropped() is False
        assert get_cipher(backend, stream) is not None

        await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)
github encode / httpx / tests / test_concurrency.py View on Github external
async def test_start_tls_on_uds_socket_stream(https_uds_server):
    backend = lookup_backend()
    ctx = SSLConfig().load_ssl_context_no_verify()
    timeout = Timeout(5)

    stream = await backend.open_uds_stream(
        https_uds_server.config.uds, https_uds_server.url.host, None, timeout
    )

    try:
        assert stream.is_connection_dropped() is False
        assert get_cipher(backend, stream) is None

        stream = await stream.start_tls(https_uds_server.url.host, ctx, timeout)
        assert stream.is_connection_dropped() is False
        assert get_cipher(backend, stream) is not None

        await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)
github encode / httpx / tests / test_concurrency.py View on Github external
async def test_concurrent_read(server):
    """
    Regression test for: https://github.com/encode/httpx/issues/527
    """
    backend = lookup_backend()
    stream = await backend.open_tcp_stream(
        server.url.host, server.url.port, ssl_context=None, timeout=Timeout(5)
    )
    timeout = Timeout(5)
    try:
        await stream.write(b"GET / HTTP/1.1\r\n\r\n", timeout)
        await run_concurrently(
            backend, lambda: stream.read(10, timeout), lambda: stream.read(10, timeout)
        )
    finally:
        await stream.close()
github encode / httpx / tests / test_concurrency.py View on Github external
def test_lookup_backend():
    assert isinstance(lookup_backend("asyncio"), AsyncioBackend)
    assert isinstance(lookup_backend("trio"), TrioBackend)
    assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)

    async def get_backend_from_auto():
        auto_backend = lookup_backend("auto")
        return auto_backend.backend

    loop = asyncio.get_event_loop()
    backend = loop.run_until_complete(get_backend_from_auto())
    assert isinstance(backend, AsyncioBackend)

    backend = trio.run(get_backend_from_auto)
    assert isinstance(backend, TrioBackend)

    with pytest.raises(Exception, match="unknownio"):
        lookup_backend("unknownio")
github encode / httpx / tests / test_concurrency.py View on Github external
def test_lookup_backend():
    assert isinstance(lookup_backend("asyncio"), AsyncioBackend)
    assert isinstance(lookup_backend("trio"), TrioBackend)
    assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)

    async def get_backend_from_auto():
        auto_backend = lookup_backend("auto")
        return auto_backend.backend

    loop = asyncio.get_event_loop()
    backend = loop.run_until_complete(get_backend_from_auto())
    assert isinstance(backend, AsyncioBackend)

    backend = trio.run(get_backend_from_auto)
    assert isinstance(backend, TrioBackend)

    with pytest.raises(Exception, match="unknownio"):
        lookup_backend("unknownio")
github encode / httpx / tests / test_concurrency.py View on Github external
assert isinstance(lookup_backend("trio"), TrioBackend)
    assert isinstance(lookup_backend(AsyncioBackend()), AsyncioBackend)

    async def get_backend_from_auto():
        auto_backend = lookup_backend("auto")
        return auto_backend.backend

    loop = asyncio.get_event_loop()
    backend = loop.run_until_complete(get_backend_from_auto())
    assert isinstance(backend, AsyncioBackend)

    backend = trio.run(get_backend_from_auto)
    assert isinstance(backend, TrioBackend)

    with pytest.raises(Exception, match="unknownio"):
        lookup_backend("unknownio")
github encode / httpx / tests / test_concurrency.py View on Github external
async def get_backend_from_auto():
        auto_backend = lookup_backend("auto")
        return auto_backend.backend
github encode / httpx / httpx / dispatch / http2.py View on Github external
def __init__(
        self,
        socket: BaseSocketStream,
        backend: typing.Union[str, ConcurrencyBackend] = "auto",
        on_release: typing.Callable = None,
    ):
        self.socket = socket
        self.backend = lookup_backend(backend)
        self.on_release = on_release
        self.state = h2.connection.H2Connection(config=self.CONFIG)

        self.streams = {}  # type: typing.Dict[int, HTTP2Stream]
        self.events = {}  # type: typing.Dict[int, typing.List[h2.events.Event]]

        self.init_started = False
github encode / httpx / httpx / dispatch / connection.py View on Github external
def __init__(
        self,
        origin: typing.Union[str, Origin],
        verify: VerifyTypes = True,
        cert: CertTypes = None,
        trust_env: bool = None,
        http2: bool = False,
        backend: typing.Union[str, ConcurrencyBackend] = "auto",
        release_func: typing.Optional[ReleaseCallback] = None,
        uds: typing.Optional[str] = None,
    ):
        self.origin = Origin(origin) if isinstance(origin, str) else origin
        self.ssl = SSLConfig(cert=cert, verify=verify, trust_env=trust_env)
        self.http2 = http2
        self.backend = lookup_backend(backend)
        self.release_func = release_func
        self.uds = uds
        self.open_connection: typing.Optional[OpenConnection] = None
        self.expires_at: typing.Optional[float] = None