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

Introducing and using Python's HTTPX web client

WBOY
Release: 2023-04-23 20:07:05
forward
1941 people have browsed it

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
Copy after login

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"]

Copy after login

Here is the output of this simple script:

{'hello': 'world'}
Copy after login

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

Copy after login

The result is:

<Response [404 NOT FOUND]>
Copy after login

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

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

Copy after login

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

Copy after login

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"]

Copy after login

Output example:

{'source': 'custom-client'}
Copy after login

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()

Copy after login

Output:

{'authenticated': True, 'user': 'good_person'}
Copy after login

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)

Copy after login

Output:

'pretty_good_person'
Copy after login

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template