HTTPX Tutorial - A next-generation HTTP client for Python

  Рет қаралды 101,436

Patrick Loeber

Patrick Loeber

Күн бұрын

In this Python Tutorial we learn about HTTPX, a modern HTTP client for Python with async support.
Resources:
www.python-httpx.org/
www.twilio.com/blog/asynchron...
Get my Free NumPy Handbook:
www.python-engineer.com/numpy...
✅ Write cleaner code with Sourcery, instant refactoring suggestions in VS Code & PyCharm: sourcery.ai/?... *
⭐ Join Our Discord : / discord
📓 ML Notebooks available on Patreon:
/ patrickloeber
If you enjoyed this video, please subscribe to the channel:
▶️ : / @patloeber
~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
🖥️ Website: www.python-engineer.com
🐦 Twitter - / patloeber
✉️ Newsletter - www.python-engineer.com/newsl...
📸 Instagram - / patloeber
🦾 Discord: / discord
▶️ Subscribe: / @patloeber
~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
🅿 Patreon - / patrickloeber
#Python
Timeline:
00:00 Introduction
00:47 Installation & Features
02:25 Basic Usage
03:55 Advanced Usage with Client
05:27 Async API
06:20 Web App Testing
07:33 Speed Tests
----------------------------------------------------------------------------------------------------------
* This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏

Пікірлер: 117
@patloeber
@patloeber Жыл бұрын
What's your favorite HTTP library in Python?
@franciskatende1566
@franciskatende1566 Жыл бұрын
httpx
@abdultheseekerofknowledge4453
@abdultheseekerofknowledge4453 Жыл бұрын
requests
@thesupercoach
@thesupercoach Жыл бұрын
aiohttp
@0xN1nja
@0xN1nja Жыл бұрын
requests
@shanecaler1167
@shanecaler1167 Жыл бұрын
Aiohttp because you can change proxies per request in a client rather than having to set the proxy at the time of creating the client. Httpx would be the better option if they had that one feature
@TimL_
@TimL_ Жыл бұрын
This was brilliant, the more involved topic, the comparisons, discussing correct practices, everything! Keep it up!
@uvle-ts8bi
@uvle-ts8bi Жыл бұрын
Thankx for breaking it down.. great tutorial for beginners
@cexploreful
@cexploreful Жыл бұрын
Thx Python Engineer for making High Quality Videos!
@theeox
@theeox Жыл бұрын
It's funny I recently ran into domaintools switching from requests to httpx and didn't know why. Thanks for the run down!
@ermalgashimramori
@ermalgashimramori Жыл бұрын
Great content, looking forward for more.
@magno5157
@magno5157 Жыл бұрын
I love Python! ... the simplicity of Pythonic ethos reverberates through even 3rd party Python modules
@Strikeviolet
@Strikeviolet Жыл бұрын
I used this in our project… best library for async and sync requests
@prateeksarangi9187
@prateeksarangi9187 Жыл бұрын
Thanks mate !! Great one
@daynetran6697
@daynetran6697 Жыл бұрын
This is a great video that came at the right time! I was working with an API for the past week and the part of the pipeline that took the longest was the APi request through the requests library. Now I'm going to implement this HTTTPX client and also try to work in the Async feature! Thank you! You're awesome!
@vishalmatam
@vishalmatam Жыл бұрын
Brilliant video!!
@chizzlemo3094
@chizzlemo3094 Жыл бұрын
GREAT CONTENT, THANK YOU!
@JuanDuran85
@JuanDuran85 Жыл бұрын
Excellent. Thanks.
@anthonyaouad4190
@anthonyaouad4190 Жыл бұрын
Small nitpick, can you please use time.perf_counter() instead of time.time() since a lot of beginners watch your videos and it's the better function
@cobyiv
@cobyiv Жыл бұрын
Why is it better?
@bongjunjang5683
@bongjunjang5683 Жыл бұрын
@@cobyiv because perf_counter uses cpu clocks to measure the difference of two function calls, while time.time returns the system time, which is subject to changes by the operating system.
@bongjunjang5683
@bongjunjang5683 Жыл бұрын
@@cobyiv the system time, also called as ‘real world time’, is always subject to changes because your operating system periodically tries to calibrate the clock with clock synchronization protocol. You should not use system time to measure the difference of two time points because the time measured latter can be smaller than the time measured earlier due to these changes.
@anthonyaouad4190
@anthonyaouad4190 Жыл бұрын
@@cobyiv check out pep418 peps.python.org/pep-0418/#rationale time.perf_counter() has the highest resolution for time deltas and is the one that should be used.
@bytesizedfeed
@bytesizedfeed Жыл бұрын
Could you instead use time.process_time()?
@shaheerzaman620
@shaheerzaman620 Жыл бұрын
Very helpful!
@ChrisHalden007
@ChrisHalden007 Жыл бұрын
Interesting. Thanks
@JoshBecigneul
@JoshBecigneul Жыл бұрын
Any thoughts on whether additional code would be needed to handle a server's responses such as HTTP rate-limits?
@fredieeevlogs7077
@fredieeevlogs7077 Жыл бұрын
wow thanks for the tip
@kosmonautofficial296
@kosmonautofficial296 Жыл бұрын
Great video thanks! Now is this also able to handle well in a multiprocess async fashion? I am looking into aiomultuprocess and not sure if it would be better to run this over aiohttp.
@TheZekei
@TheZekei Жыл бұрын
I was trying to use this, but you can't pass context to event handlers. This means, for instance, that a response handler only knows the response, but it has no knowledge of what the request was. Handlers are only assigned at the client level (no per-request handlers). So to get different handling for each request, you need a new client. This breaks connection pooling. Ultimately, I went with aiohttp, as it allows passing context per request. Hopefully they add this feature to httpx.
@tusharsnn
@tusharsnn Жыл бұрын
have you tried http2 for making request ? what was the performance difference between http 1.1 vs 2 ? I think requests does not support h2 but httpx does.
@Diablerick
@Diablerick Жыл бұрын
Nice.
@michak.6325
@michak.6325 Жыл бұрын
Are there benefits compared to aiohttp? Performance should be comparable, but I'm curious if there is more... probably on testing flask / fastAPI as already mentioned?
@ivan2885
@ivan2885 Жыл бұрын
so i think, as a HTTP client the bast solution for now is httpx, because AIOHTTP provides httpserver and httpclient as well.
@Assxz
@Assxz 10 ай бұрын
aiohttp is faster than async httpx, but need more code to write and less intuitive (like requests library). and httpx supports http/2 which is faster
@AiF0
@AiF0 Жыл бұрын
Well what is the difference between it and the requests library?
@toosafelol
@toosafelol Жыл бұрын
httpx is very good library. amazing asyncio support. at my job we have replaced all httprequests in our code base with this. we used aiohttp and requests in the past.
@arthurdujardin2877
@arthurdujardin2877 Жыл бұрын
What is the VS code theme used ? I really like it !
@motaseamyousef7668
@motaseamyousef7668 Жыл бұрын
Hi, thanks for all useful info Could you please make video list for your shorts videos
@markjones9180
@markjones9180 Жыл бұрын
What theme do you use in VS code?
@techshareurdu3948
@techshareurdu3948 Жыл бұрын
I am new to your channel
@Caedin8
@Caedin8 Жыл бұрын
It would have been great if you verified that all the 150 Pokemon were successfully gathered after each time trial. We are just trusting that the outcome of the 0.57 second async gather is equivalent to the 7 second synchronous gathering of Pokemon details. I believe you, but I am skeptical and an extra 2 or 3 seconds in the video to print the contents of the lists after would have been appreciated! Thanks
@holthuizenoemoet591
@holthuizenoemoet591 Жыл бұрын
I always wondered if something like this would work to build backend webframework with
@Lucifer-xk9de
@Lucifer-xk9de Жыл бұрын
nice demo, the requests does not support asyncio, so there is aiohttp can help. now, we have another library which much convenient than aiohttp. the httpx have the same style as requests..thanks !
@stevanmeandzija
@stevanmeandzija Жыл бұрын
Do you know why memory leak is happening when using in fastapi on linux?
@abdoemad3952
@abdoemad3952 Жыл бұрын
can you build a desktop app. determining the area of any engineering shapes like rectangle ,..... .
@nicenaija9952
@nicenaija9952 Жыл бұрын
How well does this intergrate with Django?
@aminramazanifar9743
@aminramazanifar9743 Жыл бұрын
Can you please give a lecture about 'with' ?
@techshareurdu3948
@techshareurdu3948 Жыл бұрын
Can you please make a video on Aws Cloud Front How can I Use This http module with Python
@bigdaddy5303
@bigdaddy5303 Жыл бұрын
So they combined requests with aiohttp? Don't think I'm going to bother changing my code to just have a single import.
@JusticeNDOU
@JusticeNDOU Жыл бұрын
try using timeit to time method execution, python time library is not meant to time method execution
@ch4sethe5un
@ch4sethe5un Жыл бұрын
But can't you use requests in asyncio via to_thread?
@hicoop
@hicoop Жыл бұрын
Looks cool but not enough features to switch from requests
@Medx111
@Medx111 Жыл бұрын
Make a video about socket
@commerceclassesbygoyalinst8373
@commerceclassesbygoyalinst8373 Жыл бұрын
How Can i Use Place Method with Frames in Tkinter ?
@ehsankabiri8912
@ehsankabiri8912 10 ай бұрын
Hope some day, httpx supports (Async+Socks5 proxy)
@vraymond2048
@vraymond2048 Жыл бұрын
I'm pretty sure there is ways to archive similiar result with request module and threading.
@angeloj.willems4362
@angeloj.willems4362 Жыл бұрын
That's the method I currently use. But this looks to be faster.
@lucianofeder5422
@lucianofeder5422 Жыл бұрын
And using async you dont need more threads to just wait requests. You can take advantage of a single thread on those kinda of tasks.
@pritamsarkar3371
@pritamsarkar3371 Жыл бұрын
the last example sometimes is giving "RuntimeError: Event loop is closed" or "httpx.ConnectTimeout" but sometimes it is executing properly, I am using python 3.8. , windows 10,, can you please explain what is happening, and what is the solution?
@pritamsarkar3371
@pritamsarkar3371 Жыл бұрын
got the solution , just need to increase the timeout param
@yashdawani1909
@yashdawani1909 Жыл бұрын
import httpx as requests. Boom no refactoring required
@muhamedqasim5390
@muhamedqasim5390 Жыл бұрын
Which faster httpx or aiohttp
@maqarg
@maqarg 8 ай бұрын
🎯 Key Takeaways for quick navigation: 00:00 📖 Introduction to HTTPX 00:27 🚀 Getting Started with HTTPX 01:37 📜 HTTPX Features 02:58 🧪 Basic Functionality of HTTPX 04:05 🔧 Advanced Usage with HTTPX Client 05:27 ⚡ Asynchronous API in HTTPX 06:24 🧪 Testing Web Applications with HTTPX 07:33 🚀 Speed Comparisons 10:58 🎉 Conclusion
@CodingByAmp
@CodingByAmp Жыл бұрын
wow
@int0matar
@int0matar Жыл бұрын
Why does python use single-line quotes instead of double quotes when defining a string literal? Golang always uses doubles. What could be the reason for this choice?
@tolia5587
@tolia5587 Жыл бұрын
You can use double or single-line quotes. For example print("text"more text"text") won't work, but print("text'more text'text") will work
@int0matar
@int0matar Жыл бұрын
@@tolia5587 I didn't mean it, it's all clear. I'm talking about the fact that it would be more logical to always use double quotes.
@tolia5587
@tolia5587 Жыл бұрын
@@int0matar Yeah, I agree with you
@DenisRasulev
@DenisRasulev Жыл бұрын
Speed comparison starts at 7:33 - kzbin.info/www/bejne/p3LLZpd6hKl9a80
@Pelmenosaurus
@Pelmenosaurus Жыл бұрын
99% modern internet is adult content or advertisement. I remember the times when a page opened in a browser was tens of kilobytes in size, and not like modern monstrous pages in megabytes and tens of megabytes.
@lazyh0rse
@lazyh0rse Жыл бұрын
Whats the point? If request is working perfectly then i dont see the need for more bloat and dependencies.
@bayy4202
@bayy4202 Жыл бұрын
i think requests + asyncio.to_thread way simple and clean kkkk
@DecimasoN
@DecimasoN Жыл бұрын
I'd still choose aiohttp over httpx for pure async code
@reinduhr
@reinduhr 8 күн бұрын
Nice explanation Patrick, but 'Pokémon' is plural. Not 'pokemons'
@JokeryEU
@JokeryEU Жыл бұрын
its missing http/3
@Ihavetoreturnsomevideotapes
@Ihavetoreturnsomevideotapes Жыл бұрын
Hi Patrick ! Bateman
@blueapollo3982
@blueapollo3982 Жыл бұрын
Is this better than uvicorn?
@vraymond2048
@vraymond2048 Жыл бұрын
Uvicorn Gunicorn is for server side hosting. In contrast, Request and httpx module is client side to request result from server.
@TNeulaender
@TNeulaender Жыл бұрын
It's even better than Excel
@hansdietrich1496
@hansdietrich1496 Жыл бұрын
"There is no way to get async with requests" ... well there is, using greenlets and gevent.
@MrEloska
@MrEloska Жыл бұрын
In the last example there is no need for `asyncio.create_task`. You can do it just adding coroutines to list `tasks.append(get_pokemon(client, url))`. What's more - to make this code more "pythonic" you can do it like: `pokemons = await asyncio.gather(*[get_pokemon(client, f"{base_pokemon_url}/{i}") for i in range(1, 151)])` :D
@angeloj.willems4362
@angeloj.willems4362 Жыл бұрын
True. I tried it without asyncio.ensure_future and it worked prefectly.
@armandophilippe6203
@armandophilippe6203 Жыл бұрын
threading with request would have beeen as fast...
@ordinarygg
@ordinarygg Жыл бұрын
MEMORY LEAKS: httpx.Client and httpx.AsynClient is leaking memory when you are not passing custom ssl_context. So if you use it in production, be cautious behind tutorials in vacuum space != your real code.
@martinhotmann7868
@martinhotmann7868 Жыл бұрын
Nice, but honestly for a language that itself does not perform well and therefore always was the perfect wrapper and easy to handle, this async function (the right way) looks way to complicated. They should have made it WAY more simple.
@akzual50
@akzual50 Жыл бұрын
New gen? You mean someone just made helper methods deal with this for you
@Dan-vu3vt
@Dan-vu3vt Жыл бұрын
You're comparing parallelism to synchronous code. This is not really a fair comparison.
@philippelhaus
@philippelhaus Жыл бұрын
No http/3…
@jerrygeorge180
@jerrygeorge180 Жыл бұрын
.
@Skeptic_Von_Rahm
@Skeptic_Von_Rahm Жыл бұрын
hell no why every new framework say "next generation framework" just stop saying just GO and do it or you RUST
@Jkauppa
@Jkauppa Жыл бұрын
trash expansions upon trash
@Jkauppa
@Jkauppa Жыл бұрын
you do thrash just for money sake, nothing useful, therefore trash
@Jkauppa
@Jkauppa Жыл бұрын
trash bloats around everything
Requests vs HTTPX vs Aiohttp | Which One to Pick?
15:11
ArjanCodes
Рет қаралды 33 М.
Python Asyncio, Requests, Aiohttp | Make faster API Calls
17:56
Patrick Collins
Рет қаралды 122 М.
Зомби Апокалипсис  часть 1 🤯#shorts
00:29
INNA SERG
Рет қаралды 7 МЛН
Разбудила маму🙀@KOTVITSKY TG:👉🏼great_hustle
00:11
МишАня
Рет қаралды 3,6 МЛН
La final estuvo difícil
00:34
Juan De Dios Pantoja
Рет қаралды 16 МЛН
когда одна дома // EVA mash
00:51
EVA mash
Рет қаралды 13 МЛН
Python dataclasses will save you HOURS, also featuring attrs
8:50
Async for loops in Python
16:36
mCoding
Рет қаралды 55 М.
Best Web Scraping Combo? Use These In Your Projects
20:13
John Watson Rooney
Рет қаралды 41 М.
6 Tips to write BETTER For Loops in Python
9:19
Patrick Loeber
Рет қаралды 244 М.
Python Asynchronous Programming - AsyncIO & Async/Await
25:57
Tech With Tim
Рет қаралды 411 М.
Middleware Pattern For HttpClient With Delegating Handlers
17:07
Milan Jovanović
Рет қаралды 12 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 786 М.
Web Scraping with AIOHTTP and Python
13:44
John Watson Rooney
Рет қаралды 23 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 352 М.
How To Make ANY Function Asynchronous In Python 3.12
5:52
Indently
Рет қаралды 34 М.
Зомби Апокалипсис  часть 1 🤯#shorts
00:29
INNA SERG
Рет қаралды 7 МЛН