Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the HTTP interaction httpx package module of Pytho

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use Pytho's HTTP interactive httpx package module". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Python's httpx package is a complex Web client. When you install it, you can use it to get data from the website. As always, 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 to get data from a web address:

Import httpxresult = 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 does not raise an error in a state other than 200.

Try this code:

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

The result is:

You can explicitly return a response. 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

In addition to the simplest scripts, it makes sense to use a custom client. In addition to good performance improvements, such as connection pooling, this is also a good place to configure clients.

For example, you can set a custom basic 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 typical scenarios where the client is talking to a specific server. For example, with base_url and auth, you can create a beautiful 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 better thing you can do with it is to build the client in the top-level "master" function and pass it to other functions. This allows other functions to use the client and have them unit test 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'

This is the end of the content of "how to use Pytho's HTTP interactive httpx package module". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report