Skip to main content

WebSockets support for HTTPX

Project description

httpx-ws

WebSockets support for HTTPX

build codecov PyPI version Downloads

Installation

Not released on PyPI yet 😅

pip install httpx-ws

Quickstart

httpx-ws provides connect_ws and aconnect_ws to help connecting and communication with WebSockets. The resulting WebSocketSession/AsyncWebSocketSession object provides helpers to send and receive messages in the WebSocket.

Sync

from httpx_ws import connect_ws

with connect_ws("http://localhost:8000/ws") as ws:
    message = ws.receive_text()
    print(message)
    ws.send_text("Hello!")

Async

from httpx_ws import aconnect_ws

async with aconnect_ws("http://localhost:8000/ws") as ws:
    message = await ws.receive_text()
    print(message)
    await ws.send_text("Hello!")

You can also pass an httpx.Client/httpx.AsyncClient if you want to customize parameters or benefit from connection pooling:

Sync

import httpx
from httpx_ws import connect_ws

with httpx.Client() as client:
    with connect_ws("http://localhost:8000/ws", client) as ws:
        message = ws.receive_text()
        print(message)
        ws.send_text("Hello!")

Async

import httpx
from httpx_ws import aconnect_ws

with httpx.AsyncClient() as client:
    async with aconnect_ws("http://localhost:8000/ws", client) as ws:
        message = await ws.receive_text()
        print(message)
        await ws.send_text("Hello!")

Testing ASGI apps

You can use httpx_ws to test WebSockets defined in an ASGI app, just like you do with HTTPX for HTTP endpoints.

For this, we've implemented a custom transport for HTTPX, ASGIWebSocketTransport. You need to instantiate a class of this transport and set it as parameter on your HTTPX client.

Let's say you have this Starlette app:

from starlette.applications import Starlette
from starlette.responses import HTMLResponse
from starlette.routing import Route, WebSocketRoute


async def http_hello(request):
    return HTMLResponse("Hello World!")

async def ws_hello(websocket):
    await websocket.accept()
    await websocket.send_text("Hello World!")
    await websocket.close()


app = Starlette(
    routes=[
        Route("/http", http_hello),
        WebSocketRoute("/ws", ws_hello),
    ],
)

You can call it directly like this:

import httpx
from httpx_ws import aconnect_ws
from httpx_ws.transport import ASGIWebSocketTransport

async with httpx.AsyncClient(transport=ASGIWebSocketTransport(app)) as client:
    http_response = await client.get("http://server/http")
    assert http_response.status_code == 200

    async with aconnect_ws("http://server/ws", client) as ws:
        message = await ws.receive_text()
        assert message == "Hello World!"

Notice that, in this case, you must pass the client instance to aconnect_ws. HTTP requests are handled normally.

Development

Setup environment

We use Hatch to manage the development environment and production build. Ensure it's installed on your system.

Run unit tests

You can run all the tests with:

hatch run test

Format the code

Execute the following command to apply isort and black formatting:

hatch run lint

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

httpx_ws-0.1.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

httpx_ws-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file httpx_ws-0.1.0.tar.gz.

File metadata

  • Download URL: httpx_ws-0.1.0.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.1

File hashes

Hashes for httpx_ws-0.1.0.tar.gz
Algorithm Hash digest
SHA256 79719efccd59afe81a2ca5c7295fd6a6ae0762d3597d4b21e1a8ed7fffd7e6ea
MD5 af5bdf1804c2b481ee852de076c8903a
BLAKE2b-256 86a98e70ce24b702c4c6e9d33f7f9ed639f9f3731c911a8d7905cafa72578313

See more details on using hashes here.

File details

Details for the file httpx_ws-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: httpx_ws-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.1

File hashes

Hashes for httpx_ws-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98c81c728a625a8b9c488ad1bd00a8d5a2967ce8204aad92f178f3f255159219
MD5 6ee9f372612786cbc658df3a8dbac31b
BLAKE2b-256 5d132f30d8cc17e6a65e76fc17607b147c00367d13d9bdde67e445800ff587ab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page