Home >Backend Development >Python Tutorial >Introducing and using Python's HTTPX web client

Introducing and using Python's HTTPX web client

WBOY
WBOYforward
2023-04-23 20:07:051902browse

httpx:一个 Python Web 客户端

Python’s ​ ​httpx​​ package 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 to use the ​​pip​​ tool:

$ python -m pip install httpx --user

To use it, import it into a Python script and then use ​ The .get​​ function gets data from a web address:

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

Here is the output of this simple script:

{'hello': 'world'}

HTTP response

By default, ​​httpx​​ Will not throw an error in non-200 status.

Try this code:

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

The result is:

<Response [404 NOT FOUND]>

A response can be returned explicitly. Add this exception handling:

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

Custom client

Besides the simplest script, there are many ways to use a custom client meaningful. In addition to nice performance improvements like connection pooling, this is also a great place to configure the client.

For example, you can set a custom base URL:

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

Output example:

{'source': 'custom-client'}

This is typical for a client talking to a specific server it works. For example, using ​​base_url​​ and ​​auth​​, you can build a nice abstraction for authenticated clients:

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'}

A nicer thing you can do with this is to build the client in a top-level "main" function and then pass it to other functions. This allows other functions to use the client and have them unit tested with the client 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'

Try httpx

Please visit python-httpx.org for more information, documentation and tutorials. I find it to be an excellent and flexible module for interacting with HTTP. Give it a try and see what it does for you.

The above is the detailed content of Introducing and using Python's HTTPX web client. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete