• Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
AnonyViet - English Version
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office
No Result
View All Result
AnonyViet - English Version
No Result
View All Result

Using httpx as a Python Web browser

AnonyViet by AnonyViet
January 25, 2023
in Tips
0

Package httpx of Python 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:

Join the channel Telegram of the AnonyViet 👉 Link 👈

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 in python in the simplest way here.

The article achieved: 5/5 – (100 votes)

Tags: browserhttpxPythonweb
Previous Post

How to Use Screaming Frog Seo Spider 16 to Check Website SEO

Next Post

Lesson 244: Curved lines in Excel

AnonyViet

AnonyViet

Related Posts

4 ways to fix bluetooth connectivity on Windows 11
Tips

4 ways to fix bluetooth connectivity on Windows 11

August 8, 2025
How to know the computer is tracked and processed by Keylogger
Tips

How to know the computer is tracked and processed by Keylogger

August 7, 2025
Opal: Create applications who do not need to write code
Tips

Opal: Create applications who do not need to write code

August 3, 2025
How to activate a new Start menu on Windows 11
Tips

How to activate a new Start menu on Windows 11

July 29, 2025
Intellgpt: AI tool for osint and data science
Tips

Intellgpt: AI tool for osint and data science

July 28, 2025
How to create Google Ai Pro 12 months free with Indian student account
Tips

How to create Google Ai Pro 12 months free with Indian student account

July 27, 2025
Next Post
Lesson 244: Curved lines in Excel

Lesson 244: Curved lines in Excel

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Recent News

How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 2025
Instructions on how to fix Screen Time Limited Reached on RoBlox

Instructions on how to fix Screen Time Limited Reached on RoBlox

August 13, 2025
How to install GPT-suns on who do not need the Internet

How to install GPT-suns on who do not need the Internet

August 12, 2025
How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 2025
Instructions on how to fix Screen Time Limited Reached on RoBlox

Instructions on how to fix Screen Time Limited Reached on RoBlox

August 13, 2025
AnonyViet - English Version

AnonyViet

AnonyViet is a website share knowledge that you have never learned in school!

We are ready to welcome your comments, as well as your articles sent to AnonyViet.

Follow Us

Contact:

Email: anonyviet.com[@]gmail.com

Main Website: https://anonyviet.com

Recent News

How to add application to your favorite bar

How to add application to your favorite bar

August 14, 2025
Wowhay.com – The door opens the world of modern knowledge and network culture

Wowhay.com – The door opens the world of modern knowledge and network culture

August 13, 2025
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

No Result
View All Result
  • Home
  • News
  • Software
  • Knowledge
  • MMO
  • Tips
  • Security
  • Network
  • Office

©2024 AnonyVietFor Knowledge kqxs hôm nay xem phim miễn phí mm88 8XBET mm88 trang chủ new88

wpDiscuz
0
0
Would love your thoughts, please comment.x
()
x
| Reply