Skip to content
Myr

README.md

This file

Path

README.md

89 lines · 2.1 KiB · Markdown

Last changed by

Added proxy exampleTom Christiebefore adoption87b21b0

History of this file

Take it

StuffTom Christiebefore adoption30530d4

lines 1–10why, one step back
1# HTTPCore
23```python4import httpcore56response = await httpcore.request('GET', 'http://example.com')7assert response.status_code == 2008assert response.body == b'Hello, world'9```10

Update READMETom Christiebefore adoption68b80dc

line 11why, one step back
11Top-level API...

StuffTom Christiebefore adoption30530d4

lines 12–21why, one step back
12
13```python14response = await httpcore.request(method, url, [headers], [body], [stream])15```1617Explicit PoolManager...1819```python20async with httpcore.PoolManager([ssl], [timeout], [limits]) as pool:21 response = await pool.request(method, url, [headers], [body], [stream])

Update READMETom Christiebefore adoption68b80dc

lines 22–29why, one step back
22```
2324Streaming...2526```python27response = await httpcore.request(method, url, stream=True)28async for part in response.stream():29 ...

Added proxy exampleTom Christiebefore adoption87b21b0

lines 30–88why, one step back
30```
3132The level of abstraction fits in really well if you're just writing at33the raw ASGI level. Eg. Here's an how an ASGI gateway server looks against the34API, including streaming uploads and downloads...3536```python37import httpcore383940class GatewayServer:41 def __init__(self, base_url):42 self.base_url = base_url43 self.pool = httpcore.PoolManager()4445 async def __call__(self, scope, receive, send):46 path = scope['path']47 query = scope['query_string']48 method = scope['method']49 headers = scope['headers']5051 url = self.base_url + path52 if query:53 url += '?' + query.decode()5455 async def body():56 nonlocal receive5758 while True:59 message = await receive()60 yield message.get('body', b'')61 if not message.get('more_body', False):62 break6364 response = await self.pool.request(65 method, url, headers=headers, body=body, stream=True66 )6768 await send({69 'type': 'http.response.start',70 'status': response.status_code,71 'headers': response.headers72 })73 async for data in response.stream():74 await send({75 'type': 'http.response.body',76 'body': data,77 'more_body': True78 })79 await send({'type': 'http.response.body'})808182app = GatewayServer('http://example.org')83```8485Run with...8687```shell88uvicorn example:app

StuffTom Christiebefore adoption30530d4

line 89why, one step back
89```