The httpx package for Python is an excellent and flexible module for
interacting with HTTP.
![Digital creative of a browser on the internet][1]
The `httpx`package for Python is a sophisticated web client. Once you install it, you can use it to get data from websites. As usual, the easiest way to install it is with the `pip`utility:
```
`$ python -m pip install httpx --user`
```
To use it, import it into a Python script, and then use the `.get`function to fetch data from a web address:
```
import httpx
result = httpx.get("<https://httpbin.org/get?hello=world>")
result.json()["args"]
```
Here's the output from that simple script:
```
`{'hello': 'world'}`
```
### HTTP response
By default, `httpx`will not raise errors on a non-200 status.
Try this code:
```
result = httpx.get("<https://httpbin.org/status/404>")
result
```
The result:
```
` <Response [404 NOT FOUND]>`
```
It's possible to raise a response explicitly. Add this exception handler:
```
try:
result.raise_for_status()
except Exception as exc:
print("woops", exc)
```
Here's the result:
```
woops Client error '404 NOT FOUND' for url '<https://httpbin.org/status/404>'
For more information check: <https://httpstatuses.com/404>
```
### Custom client
It is worthwhile to use a custom client for anything but the simplest script. Aside from nice performance improvements, such as connection pooling, this is a good place to configure the client.
This is useful for a typical scenario where you use the client to talk to a specific server. For example, using both `base_url`and `auth`, you can build a nice abstraction for an authenticated client:
```
client = httpx.Client(
base_url="<https://httpbin.org>",
auth=("good_person", "secret_password"),
)
result = client.get("/basic-auth/good_person/secret_password")
result.json()
```
Output:
```
`{'authenticated': True, 'user': 'good_person'}`
```
One of the nicer things you can use this for is constructing the client at a top-level "main" function and then passing it around. This lets other functions use the client and lets them get unit-tested with a client connected to a local WSGI app.
```
def get_user_name(client):
result = client.get("/basic-auth/good_person/secret_password")
Visit [python-httpx.org][2]for more information, documentation, and tutorials. I've found it to be an excellent and flexible module for interacting with HTTP. Give it a try and see what it can do for you.
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/browser_web_internet_website.png?itok=g5B_Bw62 (Digital creative of a browser on the internet)