README.md
This file
Path
README.md
158 lines · 4.5 KiB · Markdown
Last changed by
README tweaksTom Christiebefore adoption8e1a263
History of this fileTake it
StuffTom Christiebefore adoption30530d4
lines 1–2why, one step back1# HTTPCore2Update README.mdTom Christiebefore adoption01a3c09
line 3why, one step back3A low-level async HTTP library.Update READMETom Christiebefore adoption665743a
line 4why, one step back4Update README.mdTom Christiebefore adoption01a3c09
line 5why, one step back5## Proposed functionalityUpdate READMETom Christiebefore adoption665743a
line 6why, one step back6Update README.mdTom Christiebefore adoption01a3c09
line 7why, one step back7* Support for streaming requests and responses. (Done)Update README.mdTom Christiebefore adoption9927dc8
line 8why, one step back8* Support for connection pooling. (Done)Update README.mdTom Christiebefore adoption6acdfb8
line 9why, one step back9* gzip, deflate, and brotli decoding. (Done)Update README.mdTom Christiebefore adoption01a3c09
lines 10–11why, one step back10* SSL verification. (Done)11* Proxy support. (Not done)Update README.mdTom Christiebefore adoption2ad8501
line 12why, one step back12* HTTP/2 support. (Not done)Update README.mdTom Christiebefore adoption01a3c09
line 13why, one step back13* Support *both* async and sync operations. (Sync will be lightweight shim on top of async. Not done.)Update READMETom Christiebefore adoption665743a
line 14why, one step back14Update README.mdTom Christiebefore adoption01a3c09
line 15why, one step back15## MotivationUpdate READMETom Christiebefore adoption665743a
line 16why, one step back16Update README.mdTom Christiebefore adoption01a3c09
line 17why, one step back17Some of the trickier remaining issues on `requests-async` such as request/response streaming, connection pooling, proxy support, would require a fully async variant of urllib3. I considered and started work on a straight port of `urllib3-async`, but having started to dive into it, my judgement is that a from-scratch implementation will be less overall work to achieve.Update READMETom Christiebefore adoption665743a
line 18why, one step back18Update README.mdTom Christiebefore adoption01a3c09
line 19why, one step back19The intent is that this library could be the low-level implementation, that `requests-async` would then wrap up.Update READMETom Christiebefore adoption665743a
line 20why, one step back20Update README.mdTom Christiebefore adoption01a3c09
line 21why, one step back21## CreditUpdate READMETom Christiebefore adoption665743a
line 22why, one step back22Update README.mdTom Christiebefore adoption01a3c09
lines 23–25why, one step back23* Some inspiration from the design-work of `urllib3`, but redone from scratch, and built as an async-first library.24* Dependant on the absolutely excellent `h11` package.25* Uses the `certifi` package for the default SSL verification.Update READMETom Christiebefore adoption665743a
line 26why, one step back26Update README.mdTom Christiebefore adoption0d3eb0e
line 27why, one step back27## UsageUpdate README.mdTom Christiebefore adoption01a3c09
line 28why, one step back28Update README.mdTom Christiebefore adoption337eb70
lines 29–30why, one step back29Making a request:30StuffTom Christiebefore adoption30530d4
lines 31–33why, one step back31```python32import httpcore33Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 34–35why, one step back34http = httpcore.ConnectionPool()35response = await http.request('GET', 'http://example.com')StuffTom Christiebefore adoption30530d4
lines 36–39why, one step back36assert response.status_code == 20037assert response.body == b'Hello, world'38```39Update README.mdTom Christiebefore adoption337eb70
line 40why, one step back40Top-level API:StuffTom Christiebefore adoption30530d4
lines 41–42why, one step back4142```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 43–44why, one step back43http = httpcore.ConnectionPool([ssl], [timeout], [limits])44response = await http.request(method, url, [headers], [body], [stream])StuffTom Christiebefore adoption30530d4
lines 45–46why, one step back45```46Update README.mdTom Christiebefore adoption337eb70
line 47why, one step back47ConnectionPool as a context-manager:StuffTom Christiebefore adoption30530d4
lines 48–49why, one step back4849```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 50–51why, one step back50async with httpcore.ConnectionPool([ssl], [timeout], [limits]) as http:51 response = await http.request(method, url, [headers], [body], [stream])Update READMETom Christiebefore adoption68b80dc
lines 52–53why, one step back52```53Update README.mdTom Christiebefore adoption337eb70
line 54why, one step back54Streaming responses:Update READMETom Christiebefore adoption68b80dc
lines 55–56why, one step back5556```pythonMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 57–58why, one step back57http = httpcore.ConnectionPool()58response = await http.request(method, url, stream=True)Update READMETom Christiebefore adoption68b80dc
lines 59–60why, one step back59async for part in response.stream():60 ...Added proxy exampleTom Christiebefore adoption87b21b0
line 61why, one step back61```Update README.mdTom Christiebefore adoption01a3c09
line 62why, one step back62README tweaksTom Christiebefore adoption8e1a263
lines 63–71why, one step back63Raw data without gzip/deflate/brotli decompression applied:6465```python66http = httpcore.ConnectionPool()67response = await http.request(method, url, stream=True)68async for part in response.raw():69 ...70```71Update README.mdTom Christiebefore adoption01a3c09
line 72why, one step back72## Building a Gateway ServerAdded proxy exampleTom Christiebefore adoption87b21b0
lines 73–84why, one step back7374The level of abstraction fits in really well if you're just writing at75the raw ASGI level. Eg. Here's an how an ASGI gateway server looks against the76API, including streaming uploads and downloads...7778```python79import httpcore808182class GatewayServer:83 def __init__(self, base_url):84 self.base_url = base_urlMerge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 85why, one step back85 self.http = httpcore.ConnectionPool()Added proxy exampleTom Christiebefore adoption87b21b0
lines 86–87why, one step back8687 async def __call__(self, scope, receive, send):Only serve http requests, ignore websocket and lifespan eventsTom Christiebefore adoption3d6aba6
line 88why, one step back88 assert scope['type'] == 'http'Added proxy exampleTom Christiebefore adoption87b21b0
lines 89–91why, one step back89 path = scope['path']90 query = scope['query_string']91 method = scope['method']Improve GatewayService exampleTom Christiebefore adoption9d59a5a
lines 92–93why, one step back92 headers = [93 (k, v) for (k, v) in scope['headers']README tweaksTom Christiebefore adoption8e1a263
line 94why, one step back94 if k not in (b'host', b'transfer-encoding')Improve GatewayService exampleTom Christiebefore adoption9d59a5a
line 95why, one step back95 ]Added proxy exampleTom Christiebefore adoption87b21b0
lines 96–100why, one step back9697 url = self.base_url + path98 if query:99 url += '?' + query.decode()100Improve GatewayService exampleTom Christiebefore adoption9d59a5a
lines 101–107why, one step back101 initial_body, more_body = await self.initial_body(receive)102 if more_body:103 # Streaming request.104 body = self.stream_body(receive, initial_body)105 else:106 # Standard request.107 body = initial_bodyAdded proxy exampleTom Christiebefore adoption87b21b0
line 108why, one step back108Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 109why, one step back109 response = await self.http.request(Added proxy exampleTom Christiebefore adoption87b21b0
lines 110–117why, one step back110 method, url, headers=headers, body=body, stream=True111 )112113 await send({114 'type': 'http.response.start',115 'status': response.status_code,116 'headers': response.headers117 })Improve GatewayService exampleTom Christiebefore adoption9d59a5a
line 118why, one step back118 data = b''README tweaksTom Christiebefore adoption8e1a263
line 119why, one step back119 async for next_data in response.raw():Improve GatewayService exampleTom Christiebefore adoption9d59a5a
lines 120–127why, one step back120 if data:121 await send({122 'type': 'http.response.body',123 'body': data,124 'more_body': True125 })126 data = next_data127 await send({'type': 'http.response.body', 'body': data})Added proxy exampleTom Christiebefore adoption87b21b0
line 128why, one step back128Improve GatewayService exampleTom Christiebefore adoption9d59a5a
lines 129–143why, one step back129 async def initial_body(self, receive):130 """131 Pull the first body message off the 'receive' channel.132 Allows us to determine if we should use a streaming request or not.133 """134 message = await receive()135 body = message.get('body', b'')136 more_body = message.get('more_body', False)137 return (body, more_body)138139 async def stream_body(self, receive, initial_body):140 """141 Async iterator returning bytes for the request body.142 """143 yield initial_bodyTweak GatewayServer exampleTom Christiebefore adoptiond572f4c
lines 144–148why, one step back144 while True:145 message = await receive()146 yield message.get('body', b'')147 if not message.get('more_body', False):148 breakImprove GatewayService exampleTom Christiebefore adoption9d59a5a
line 149why, one step back149Added proxy exampleTom Christiebefore adoption87b21b0
lines 150–157why, one step back150151app = GatewayServer('http://example.org')152```153154Run with...155156```shell157uvicorn example:appStuffTom Christiebefore adoption30530d4
line 158why, one step back158```