Using httpx as a Python Web Browser

The Python httpx package helps you create a command-line Web browser. Once installed, you can use it to view data from websites. As usual, the easiest way to install it is using pip:

python -m pip install httpx --user

How to use httpx, a Python web client

To use this library, import it into a Python script, then use the .get to fetch data from a web address:

import httpx
result = httpx.get("https://httpbin.org/get?hello=world")
result.json()["args"]

Here is the output from that simple script:

{'hello': 'world'}

HTTP response in httpx

By default, this library will not raise an error on status other than 200.

Try this code:

result = httpx.get("https://httpbin.org/status/404")
result

Result:
<Response [404 NOT FOUND]>

You can turn it into an explicit answer. Add this exception handler:

try:
    result.raise_for_status()
except Exception as exc:
    print("woops", exc)

Here are the results:

woops Client error '404 NOT FOUND' for url 'https://httpbin.org/status/404'
    For more information check: https://httpstatuses.com/404

Customize client

It would be better to use a custom client for anything but the simplest script. In addition to performance improvements, such as the connection area, this is a good place to configure the client.

For example, you can set a custom URL:

client = httpx.Client(base_url="https://httpbin.org")
result = client.get("/get?source=custom-client")
result.json()["args"]

Sample output:
{'source': 'custom-client'}

This is useful for a typical situation where you use the client to talk to a specific server. For example, using both base_url and auth, you can build a boilerplate 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 better ones you can use is to build the client application on a top-level “main” function and then deploy it around. This allows other functions to use the client application and allows them to be tested with the client application connected to the local WSGI application.

def get_user_name(client):
    result = client.get("/basic-auth/good_person/secret_password")
    return result.json()["user"]

get_user_name(client)
    'good_person'

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'application/json')])
    return [b'{"user": "pretty_good_person"}']
fake_client = httpx.Client(app=application, base_url="https://fake-server")
get_user_name(fake_client)

Output:
'pretty_good_person'

So you have learned how to use httpx already. In addition, you can also create a Keylogger using python in the simplest way here.

Leave a Reply