From 4c1eab1e3ac68ea19839b007307248ab4ccd76f6 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 10 Sep 2025 13:39:16 +0100 Subject: [PATCH] httpx.run --- README.md | 6 ++---- src/ahttpx/__init__.py | 3 ++- src/ahttpx/_network.py | 7 +------ src/ahttpx/_server.py | 9 +++++++-- src/httpx/__init__.py | 3 ++- src/httpx/_server.py | 8 +++++++- tests/test_network.py | 3 ++- 7 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index cce4032..5a984c2 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,11 @@ $ pip install --pre httpx *Serving responses as the server...* ```python ->>> def hello_world(request): +>>> def app(request): ... content = httpx.HTML('hello, world.') ... return httpx.Response(200, content=content) ->>> with httpx.serve_http(hello_world) as server: -... print(f"Serving on {server.url} (Press CTRL+C to quit)") -... server.wait() +>>> httpx.run(app) Serving on http://127.0.0.1:8080/ (Press CTRL+C to quit) ``` diff --git a/src/ahttpx/__init__.py b/src/ahttpx/__init__.py index 1dc7f99..6f8ff68 100644 --- a/src/ahttpx/__init__.py +++ b/src/ahttpx/__init__.py @@ -8,7 +8,7 @@ from ._response import * # Response from ._request import * # Request from ._streams import * # ByteStream, FileStream, HTTPStream, Stream -from ._server import * # serve_http +from ._server import * # serve_http, run from ._urlencode import * # quote, unquote, urldecode, urlencode from ._urls import * # QueryParams, URL @@ -40,6 +40,7 @@ "patch", "Response", "Request", + "run", "serve_http", "Stream", "Text", diff --git a/src/ahttpx/_network.py b/src/ahttpx/_network.py index 6c9661e..c274bd4 100644 --- a/src/ahttpx/_network.py +++ b/src/ahttpx/_network.py @@ -14,7 +14,7 @@ def __init__( ) -> None: self._reader = reader self._writer = writer - self._ = address + self._address = address self._tls = False self._closed = False @@ -25,11 +25,6 @@ async def write(self, buffer: bytes) -> None: self._writer.write(buffer) await self._writer.drain() - async def start_tls(self, ctx: ssl.SSLContext, hostname: str | None = None) -> None: - # Requires Python >= 3.11... - await self._writer.start_tls(ctx, server_hostname=hostname) - self._tls = True - async def close(self) -> None: if not self._closed: self._writer.close() diff --git a/src/ahttpx/_server.py b/src/ahttpx/_server.py index 14cb7cd..e004eb2 100644 --- a/src/ahttpx/_server.py +++ b/src/ahttpx/_server.py @@ -11,7 +11,7 @@ from ._streams import HTTPStream __all__ = [ - "serve_http", + "serve_http", "run" ] logger = logging.getLogger("httpx.server") @@ -147,5 +147,10 @@ async def handler(stream): backend = NetworkBackend() async with await backend.serve("127.0.0.1", 8080, handler) as server: server = HTTPServer(server.host, server.port) - logger.info(f"Serving on {server.url}") + logger.info(f"Serving on {server.url} (Press CTRL+C to quit)") yield server + + +async def run(app): + async with await serve_http(app) as server: + server.wait() diff --git a/src/httpx/__init__.py b/src/httpx/__init__.py index 1dc7f99..6f8ff68 100644 --- a/src/httpx/__init__.py +++ b/src/httpx/__init__.py @@ -8,7 +8,7 @@ from ._response import * # Response from ._request import * # Request from ._streams import * # ByteStream, FileStream, HTTPStream, Stream -from ._server import * # serve_http +from ._server import * # serve_http, run from ._urlencode import * # quote, unquote, urldecode, urlencode from ._urls import * # QueryParams, URL @@ -40,6 +40,7 @@ "patch", "Response", "Request", + "run", "serve_http", "Stream", "Text", diff --git a/src/httpx/_server.py b/src/httpx/_server.py index b972962..871ffbd 100644 --- a/src/httpx/_server.py +++ b/src/httpx/_server.py @@ -11,7 +11,7 @@ from ._streams import HTTPStream __all__ = [ - "serve_http", + "serve_http", "run" ] logger = logging.getLogger("httpx.server") @@ -149,3 +149,9 @@ def handler(stream): server = HTTPServer(server.host, server.port) logger.info(f"Serving on {server.url}") yield server + + +def run(app): + with serve_http(app) as server: + logger.info(f"Serving on {server.url} (Press CTRL+C to quit)") + server.wait() diff --git a/tests/test_network.py b/tests/test_network.py index e8236c5..e6ce925 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -44,7 +44,8 @@ def test_network_backend_timeout(server): net = httpx.NetworkBackend() with httpx.timeout(0.0): with pytest.raises(TimeoutError): - net.connect(server.host, server.port) + with net.connect(server.host, server.port) as stream: + pass with httpx.timeout(10.0): with net.connect(server.host, server.port) as stream: