You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/advanced.md
+67-37
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,36 @@ Using a Client instance to make requests will give you HTTP connection pooling,
6
6
will provide cookie persistence, and allows you to apply configuration across
7
7
all outgoing requests.
8
8
9
-
A Client instance is equivalent to a Session instance in `requests`.
9
+
!!! hint
10
+
A Client instance is equivalent to a Session instance in `requests`.
11
+
12
+
### Usage
13
+
14
+
The recommended way to use a `Client` is as a context manager. This will ensure that connections are properly cleaned up when leaving the `with` block:
15
+
16
+
```python
17
+
>>>with httpx.Client() as client:
18
+
... r = client.get('https://example.com')
19
+
...
20
+
>>> r
21
+
<Response [200OK]>
22
+
```
23
+
24
+
Alternatively, you can explicitly close the connection pool without block-usage using `.close()`:
10
25
11
26
```python
12
27
>>> client = httpx.Client()
13
-
>>> r = client.get('https://example.org/')
28
+
>>>try:
29
+
... r = client.get('https://example.com')
30
+
...finally:
31
+
... client.close()
32
+
...
14
33
>>> r
15
34
<Response [200OK]>
16
35
```
17
36
37
+
Once you have a `Client`, you can use all the features documented in the [Quickstart](/quickstart) guide.
38
+
18
39
## Calling into Python Web Apps
19
40
20
41
You can configure an `httpx` client to call directly into a Python web
@@ -38,10 +59,10 @@ app = Flask(__name__)
38
59
defhello():
39
60
return"Hello World!"
40
61
41
-
client =httpx.Client(app=app)
42
-
r = client.get('http://example/')
43
-
assert r.status_code ==200
44
-
assert r.text =="Hello World!"
62
+
withhttpx.Client(app=app)as client:
63
+
r = client.get('http://example/')
64
+
assert r.status_code ==200
65
+
assert r.text =="Hello World!"
45
66
```
46
67
47
68
For some more complex cases you might need to customize the WSGI or ASGI
@@ -56,7 +77,8 @@ For example:
56
77
```python
57
78
# Instantiate a client that makes WSGI requests with a client IP of "1.2.3.4".
0 commit comments