Skip to content
Myr

Advanced transport docs (#1165)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent 5b6d33e · Browse files · 1 file+43 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Diff stats

1 file+43 added−21 removed

  • other:+43 added−21 removed

1 file+43 added−21 removed

modified:docs/advanced.md+43 added-21 removed
611 unmodified lines
612```613614!!! tip615 It is safe to upload large files this way. File uploads are streaming by default, meaning that only one chunk will be loaded into memory at a time.616 616617 Non-file data fields can be included in the multipart form using by passing them to `data=...`.618 618619You can also send multiple files in one go with a multiple file field form.620To do that, pass a list of `(field, <file>)` items instead of a dictionary, allowing you to pass multiple items with the same `field`.621For instance this request sends 2 files, `foo.png` and `bar.png` in one request on the `images` form field: 621For instance this request sends 2 files, `foo.png` and `bar.png` in one request on the `images` form field:622623```python624>>> files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),625 ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
130 unmodified lines
756```python757>>> context = httpx.create_ssl_context()758```759760The `create_ssl_context` function accepts the same set of SSL configuration arguments 761(`trust_env`, `verify`, `cert` and `http2` arguments) 760The `create_ssl_context` function accepts the same set of SSL configuration arguments761(`trust_env`, `verify`, `cert` and `http2` arguments)762as `httpx.Client` or `httpx.AsyncClient`763764```python765>>> import httpx
42 unmodified lines
808HTTPX's `Client` also accepts a `transport` argument. This argument allows you809to provide a custom Transport object that will be used to perform the actual810sending of the requests.811812A transport instance must implement the Transport API defined by813[`httpcore`](https://www.encode.io/httpcore/api/). You814should either subclass `httpcore.AsyncHTTPTransport` to implement a transport to815use with `AsyncClient`, or subclass `httpcore.SyncHTTPTransport` to implement a816transport to use with `Client`.812For some advanced configuration you might need to instantiate a transport813class directly, and pass it to the client instance. The `httpcore` package814provides a `local_address` configuration that is only available via this815low-level API.816817```python818>>> import httpx, httpcore819>>> ssl_context = httpx.create_ssl_context()820>>> transport = httpcore.SyncConnectionPool(821... ssl_context=ssl_context,822... max_connections=100,823... max_keepalive_connections=20,824... keepalive_expiry=5.0,825... local_address="0.0.0.0"826... ) # Use the standard HTTPX defaults, but with an IPv4 only 'local_address'.827>>> client = httpx.Client(transport=transport)828```829830Unlike the `httpx.Client()`, the lower-level `httpcore` transport instances831do not include any default values for configuring aspects such as the832connection pooling details, so you'll need to provide more explicit833configuration when using this API.834818For example, HTTPX ships with a transport that uses the excellent835HTTPX also currently ships with a transport that uses the excellent836[`urllib3` library](https://urllib3.readthedocs.io/en/latest/), which can be837used with the sync `Client`...838839```python
4 unmodified lines
844```845846Note that you'll need to install the `urllib3` package to use `URLLib3Transport`.847848A transport instance must implement the Transport API defined by849[`httpcore`](https://www.encode.io/httpcore/api/). You850should either subclass `httpcore.AsyncHTTPTransport` to implement a transport to851use with `AsyncClient`, or subclass `httpcore.SyncHTTPTransport` to implement a852transport to use with `Client`.853854A complete example of a custom transport implementation would be:855856```python857import json835858import httpcore837import httpx859860840class JSONEchoTransport(httpcore.SyncHTTPTransport):861class HelloWorldTransport(httpcore.SyncHTTPTransport):862 """842 A mock transport that returns a JSON response containing the request body.863 A mock transport that always returns a JSON "Hello, world!" response.864 """865866 def request(self, method, url, headers=None, stream=None, timeout=None):846 body = b"".join(stream).decode("utf-8")847 content = json.dumps({"body": body}).encode("utf-8")848 stream = httpcore.SyncByteStream([content])867 message = {"text": "Hello, world!"}868 content = json.dumps(message).encode("utf-8")869 stream = httpcore.PlainByteStream(content)870 headers = [(b"content-type", b"application/json")]871 return b"HTTP/1.1", 200, b"OK", headers, stream872```873874Which we can use in the same way:875876```python877>>> import httpx856>>> client = httpx.Client(transport=JSONEchoTransport())857>>> response = client.post("https://httpbin.org/post", data="Hello, world!")878>>> client = httpx.Client(transport=HelloWorldTransport())879>>> response = client.get("https://example.org/")880>>> response.json()859{'body': 'Hello, world!'}881{"text": "Hello, world!"}882```