Httpx¶
Description:
- HTTPX is a fully featured HTTP client library for Python 3. It includes an integrated command line client, has support for both HTTP/1.1 and HTTP/2, and provides both sync and async APIs.
Resources:
- [GitHub] https://github.com/encode/httpx
- [Docs] https://www.python-httpx.org/
Example 1¶
Get http status codes, headers, and text for a url
In [ ]:
Copied!
import httpx
r = httpx.get('https://www.eugo.io/')
print(r.status_code)
print(r.headers['content-type'])
print(r.text)
import httpx
r = httpx.get('https://www.eugo.io/')
print(r.status_code)
print(r.headers['content-type'])
print(r.text)
Example 2¶
Monitor Download Progress
In [ ]:
Copied!
import tempfile
import httpx
from tqdm import tqdm
with tempfile.NamedTemporaryFile() as download_file:
url = "https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz"
with httpx.stream("GET", url) as response:
total = int(response.headers["Content-Length"])
with tqdm(total=total, unit_scale=True, unit_divisor=1024, unit="B") as progress:
num_bytes_downloaded = response.num_bytes_downloaded
for chunk in response.iter_bytes():
download_file.write(chunk)
progress.update(response.num_bytes_downloaded - num_bytes_downloaded)
num_bytes_downloaded = response.num_bytes_downloaded
import tempfile
import httpx
from tqdm import tqdm
with tempfile.NamedTemporaryFile() as download_file:
url = "https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz"
with httpx.stream("GET", url) as response:
total = int(response.headers["Content-Length"])
with tqdm(total=total, unit_scale=True, unit_divisor=1024, unit="B") as progress:
num_bytes_downloaded = response.num_bytes_downloaded
for chunk in response.iter_bytes():
download_file.write(chunk)
progress.update(response.num_bytes_downloaded - num_bytes_downloaded)
num_bytes_downloaded = response.num_bytes_downloaded
Example 3¶
Making an async request
In [ ]:
Copied!
import httpx
async def example_async():
async with httpx.AsyncClient() as client:
r = await client.get('https://www.eugo.io/')
print(r)
# The notebook already runs an event loop, so await the coroutine directly
# (in a script, use asyncio.run(example_async()) instead)
await example_async()
import httpx
async def example_async():
async with httpx.AsyncClient() as client:
r = await client.get('https://www.eugo.io/')
print(r)
# The notebook already runs an event loop, so await the coroutine directly
# (in a script, use asyncio.run(example_async()) instead)
await example_async()