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
Provenance
- Commit
477824a- Tree
47d7862- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
5b6d33e
1 file changed+43 added−21 removed
modified:docs/advanced.md+43 added-21 removed
611 unmodified lines612 ```613 614 !!! 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- 616+617 Non-file data fields can be included in the multipart form using by passing them to `data=...`.618- 618+619 You can also send multiple files in one go with a multiple file field form.620 To do that, pass a list of `(field, <file>)` items instead of a dictionary, allowing you to pass multiple items with the same `field`.621-For instance this request sends 2 files, `foo.png` and `bar.png` in one request on the `images` form field: 621+For instance this request sends 2 files, `foo.png` and `bar.png` in one request on the `images` form field:622 623 ```python624 >>> files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),625 ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]130 unmodified lines756 ```python757 >>> context = httpx.create_ssl_context()758 ```759 760-The `create_ssl_context` function accepts the same set of SSL configuration arguments 761-(`trust_env`, `verify`, `cert` and `http2` arguments) 760+The `create_ssl_context` function accepts the same set of SSL configuration arguments761+(`trust_env`, `verify`, `cert` and `http2` arguments)762 as `httpx.Client` or `httpx.AsyncClient`763 764 ```python765 >>> import httpx42 unmodified lines808 HTTPX's `Client` also accepts a `transport` argument. This argument allows you809 to provide a custom Transport object that will be used to perform the actual810 sending of the requests.811 812-A transport instance must implement the Transport API defined by813-[`httpcore`](https://www.encode.io/httpcore/api/). You814-should either subclass `httpcore.AsyncHTTPTransport` to implement a transport to815-use with `AsyncClient`, or subclass `httpcore.SyncHTTPTransport` to implement a816-transport to use with `Client`.812+For some advanced configuration you might need to instantiate a transport813+class directly, and pass it to the client instance. The `httpcore` package814+provides a `local_address` configuration that is only available via this815+low-level API.816 817+```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+```829+830+Unlike the `httpx.Client()`, the lower-level `httpcore` transport instances831+do not include any default values for configuring aspects such as the832+connection pooling details, so you'll need to provide more explicit833+configuration when using this API.834+818-For example, HTTPX ships with a transport that uses the excellent835+HTTPX also currently ships with a transport that uses the excellent836 [`urllib3` library](https://urllib3.readthedocs.io/en/latest/), which can be837 used with the sync `Client`...838 839 ```python4 unmodified lines844 ```845 846 Note that you'll need to install the `urllib3` package to use `URLLib3Transport`.847 848+A transport instance must implement the Transport API defined by849+[`httpcore`](https://www.encode.io/httpcore/api/). You850+should either subclass `httpcore.AsyncHTTPTransport` to implement a transport to851+use with `AsyncClient`, or subclass `httpcore.SyncHTTPTransport` to implement a852+transport to use with `Client`.853+854 A complete example of a custom transport implementation would be:855 856 ```python857 import json835-858 import httpcore837-import httpx859 860 840-class JSONEchoTransport(httpcore.SyncHTTPTransport):861+class 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 """865 866 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 ```873 874 Which we can use in the same way:875 876 ```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 ```