• 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

Using httpx as a Python Web browser

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

Instructions to receive Claude Fable 5 for free for 30 days
Tips

Instructions to receive Claude Fable 5 for free for 30 days

June 15, 2026
Delete all Event Logs on Windows to remove traces of activity
Tips

Delete all Event Logs on Windows to remove traces of activity

June 15, 2026
Instructions for creating Minecraft-style food photos
Tips

Instructions for creating Minecraft-style food photos

June 9, 2026
Get free Cambridge courses to prepare for IELTS, Starters, Movers
Tips

Get free Cambridge courses to prepare for IELTS, Starters, Movers

June 4, 2026
How to record reaction videos with Android phones, no app needed
Tips

How to record reaction videos with Android phones, no app needed

June 1, 2026
Instructions on how to get Google AI Pro 1 year for free for new accounts
Tips

Instructions on how to get Google AI Pro 1 year for free for new accounts

June 1, 2026
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

Instructions to receive Claude Fable 5 for free for 30 days

Instructions to receive Claude Fable 5 for free for 30 days

June 15, 2026
Instructions on how to activate the new Siri on iOS 27

Instructions on how to activate the new Siri on iOS 27

June 15, 2026
Delete all Event Logs on Windows to remove traces of activity

Delete all Event Logs on Windows to remove traces of activity

June 15, 2026
Instructions on how to get the latest 200GB MultCloud for free in 2026

Instructions on how to get the latest 200GB MultCloud for free in 2026

June 14, 2026
Instructions to receive Claude Fable 5 for free for 30 days

Instructions to receive Claude Fable 5 for free for 30 days

June 15, 2026
Instructions on how to activate the new Siri on iOS 27

Instructions on how to activate the new Siri on iOS 27

June 15, 2026
Delete all Event Logs on Windows to remove traces of activity

Delete all Event Logs on Windows to remove traces of activity

June 15, 2026
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

Instructions to receive Claude Fable 5 for free for 30 days

Instructions to receive Claude Fable 5 for free for 30 days

June 15, 2026
Instructions on how to activate the new Siri on iOS 27

Instructions on how to activate the new Siri on iOS 27

June 15, 2026
  • Home
  • Home 2
  • Home 3
  • Home 4
  • Home 5
  • Home 6
  • Next Dest Page
  • Sample Page

6789 kv999

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

6789 kv999

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