Skip to content

Commit 682ddca

Browse files
stephengruppettaStephen Gruppettaclauderealpython-bot
authored
Sample code for: Python HTTPX: Making HTTP Requests With HTTPX2 (#820)
* Sample code for: Python HTTPX: Making HTTP Requests With HTTPX2 * Sync python-httpx with the updated tutorial - custom_timeouts.py: write=5.0 instead of None - timeout_fires.py: catch TimeoutException - Add timeout_fires_async.py, stream_download_async.py, slow_endpoint.py - Remove handle_errors_async.py (replaced in the tutorial) - requirements.txt: httpx2 2.13.1 Every .py file now matches the tutorial's code blocks, formatted with the repo's ruff config. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Stephen Gruppetta <stephen@realpython.com> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Real Python Bot <42617967+realpython-bot@users.noreply.github.com>
1 parent 2a732e2 commit 682ddca

19 files changed

Lines changed: 169 additions & 0 deletions

‎python-httpx/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python HTTPX: Making HTTP Requests With HTTPX2
2+
3+
This folder provides the code examples for the Real Python tutorial [Python HTTPX: Making HTTP Requests With HTTPX2](https://realpython.com/python-httpx/)

‎python-httpx/async_request.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import asyncio
2+
3+
import httpx2
4+
5+
6+
async def main():
7+
async with httpx2.AsyncClient(base_url="https://api.github.com") as client:
8+
response = await client.get("/events")
9+
print(response.status_code)
10+
11+
12+
asyncio.run(main())

‎python-httpx/auth_header.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import httpx2
2+
3+
with httpx2.Client(
4+
base_url="https://api.github.com",
5+
headers={
6+
"Authorization": "Bearer your-token",
7+
"Accept": "application/vnd.github+json",
8+
},
9+
) as client:
10+
first = client.get("/events")
11+
second = client.get("/repos/python/cpython")
12+
13+
print(first.status_code, second.status_code)
14+
print(second.request.headers["Accept"])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import httpx2
2+
3+
with httpx2.Client() as client:
4+
pass
5+
6+
client.get("https://api.github.com/events")

‎python-httpx/custom_timeouts.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import httpx2
2+
3+
timeout = httpx2.Timeout(connect=5.0, read=10.0, write=5.0, pool=5.0)
4+
5+
with httpx2.Client(timeout=timeout) as client:
6+
response = client.get("https://api.github.com/events")
7+
print(response.status_code)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import httpx2
2+
3+
httpx2.get("https://httpbin.org/delay/6")

‎python-httpx/fetch_all.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import asyncio
2+
3+
import httpx2
4+
5+
6+
async def fetch_all(paths):
7+
async with httpx2.AsyncClient(base_url="https://api.github.com") as client:
8+
coroutines = [client.get(path) for path in paths]
9+
responses = await asyncio.gather(*coroutines)
10+
return [response.status_code for response in responses]
11+
12+
13+
paths = ["/events", "/repos/python/cpython", "/repos/psf/requests"]
14+
print(asyncio.run(fetch_all(paths)))

‎python-httpx/first_request.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import httpx2
2+
3+
response = httpx2.get("https://api.github.com/events")
4+
print(response.status_code)
5+
print(response.json())

‎python-httpx/handle_errors.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import httpx2
2+
3+
timeout = httpx2.Timeout(5.0)
4+
5+
with httpx2.Client(timeout=timeout) as client:
6+
try:
7+
response = client.get("https://api.github.com/nonexistent")
8+
response.raise_for_status()
9+
except httpx2.ConnectTimeout:
10+
print("Timed out while connecting to the host.")
11+
except httpx2.ReadTimeout:
12+
print("Timed out while receiving data from the host.")
13+
except httpx2.WriteTimeout:
14+
print("Timed out while sending data to the host.")
15+
except httpx2.PoolTimeout:
16+
print("Timed out waiting for a pool connection.")
17+
except httpx2.HTTPStatusError as error:
18+
print(f"Bad response: {error.response.status_code}")
19+
except httpx2.RequestError as error:
20+
print(f"A network problem occurred: {error}")

‎python-httpx/http2_client.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import httpx2
2+
3+
with httpx2.Client(http2=True) as client:
4+
response = client.get("https://api.github.com/events")
5+
print(response.http_version)

0 commit comments

Comments
 (0)