soyokaze 0.6.0

HTTP/1/2/3 Library Crate
Documentation
"""A server and a client in one process, talking over loopback TCP.

The Python half of the crate's ``examples/loopback.rs``. It needs no network
access and no certificate, so it is the fastest way to see a request cross the
whole stack:

    uv run examples/loopback.py
"""

import asyncio

from soyokaze import Client, Message, Method, Port, Server, URL

async def greet(request):
    """Answers every request with a greeting taken from its target.

    A handler is an ordinary coroutine function: it takes the request and
    returns the response, and the bindings frame it in whichever version the
    connection negotiated. It runs on this program's event loop, so it may
    await anything the loop can — the rest of these bindings included.
    """
    name = (request.target or "/").lstrip("/") or "World"
    return Message.text(f"Hello, {name}!", request.version)

async def main():
    # A port of zero lets the kernel choose one, so the example names none.
    server = Server()
    handle = await server.serve(greet, [Port.TCP(0)])

    client = Client()
    connection = await client.open(URL(f"http://127.0.0.1:{handle.port}/"))

    for target in ["/", "/soyokaze"]:
        response = await client.request(connection, Message.request(Method.GET, target, connection.version))
        print(f"{target} -> {response.status_code} {(await response.body()).decode()}")

    await connection.close()
    await handle.close(5)

if __name__ == "__main__":
    asyncio.run(main())