niquests
Niquests is a simple, yet elegant, HTTP library. It is a drop-in replacement for Requests, which is under feature freeze.
Description
Niquests is a simple, yet elegant, HTTP library. It is a drop-in replacement for Requests, which is under feature freeze. β¨ Were you used to betamax, requests-mock, responses, ...? See how they still work! We got you covered.
Niquests, is the βSafest, Fastest[^10], Easiest, and Most advancedβ Python HTTP Client. Production Ready!
<div align="center"> <table> <tr> <td valign="middle"> <h3>Live Benchmark</h3> <b>Target:</b> <code>https://httpbingo.org/get</code><br/> <b>Conditions:</b> <i>All default parameters, one shared session, everything simultaneously.</i><br/> <b>HTTP/2</b> when supported. </td> <td> <img src="https://i.postimg.cc/CLxGykJL/niquests.gif" width="500" alt="Niquests Benchmark"/> </td> </tr> </table> </div> <details> <summary>π <b>Look at the feature table comparison</b> against <i>requests, httpx and aiohttp</i>!</summary>| Feature | niquests | requests | httpx | aiohttp |
|---|---|---|---|---|
HTTP/1.1 | β | β | β | β |
HTTP/2 | β | β | β [^7] | β |
HTTP/3 over QUIC | β | β | β | β |
Synchronous | β | β | β | N/A[^1] |
Asynchronous | β | β | β | β |
Thread Safe | β | β | β[^5] | N/A[^1] |
Task Safe | β | N/A[^2] | β | β |
OS Trust Store | β | β | β | β |
Multiplexing | β | β | Limited[^3] | β |
DNSSEC | β [^11] | β | β | β |
Customizable DNS Resolution | β | β | β | β |
DNS over HTTPS | β | β | β | β |
DNS over QUIC | β | β | β | β |
DNS over TLS | β | β | β | β |
Multiple DNS Resolver | β | β | β | β |
Network Fine Tuning & Inspect | β | β | Limited[^6] | Limited[^6] |
Certificate Revocation Protection | β | β | β | β |
Session Persistence | β | β | β | β |
In-memory Certificate CA & mTLS | β | β | Limited[^4] | Limited[^4] |
SOCKS 4/5 Proxies | β | β | β | β |
HTTP/HTTPS Proxies | β | β | β | β |
TLS-in-TLS Support | β | β | β | β |
Direct HTTP/3 Negotiation | β [^9] | N/A[^8] | N/A[^8] | N/A[^8] |
Happy Eyeballs | β | β | β | β |
Package / SLSA Signed | β | β | β | β |
HTTP/2 with prior knowledge (h2c) | β | β | β | β |
Post-Quantum Security | Limited[^12] | β | β | β |
HTTP Trailers | β | β | β | β |
Early Responses | β | β | β | β |
WebSocket over HTTP/1 | β | β[^14] | β[^14] | β |
WebSocket over HTTP/2 and HTTP/3 | β [^13] | β | β | β |
Automatic Ping for HTTP/2+ | β | N/A | β | N/A |
Automatic Connection Upgrade / Downgrade | β | N/A | β | N/A |
Server Side Event (SSE) | β | β | β | β |
Scenario: Fetch a thousand requests using 10 tasks or threads, each with a hundred requests using a single pool of connection.
High-Level APIs
| Client | Average Delay to Complete | Notes |
|---|---|---|
| requests | 987 ms or ~1013 req/s | ThreadPoolExecutor. HTTP/1.1 |
| httpx | 720 ms or ~1389 req/s | Asyncio. HTTP/2 |
| niquests | 340 ms or ~2941 req/s | Asyncio. HTTP/2 |
Simplified APIs
| Client | Average Delay to Complete | Notes |
|---|---|---|
| requests core | 643 ms or ~1555 req/s | ThreadPoolExecutor. HTTP/1.1 |
| httpx core | 490 ms or ~2000 req/s | Asyncio. HTTP/2 |
| aiohttp | 210 ms or ~4762 req/s | Asyncio. HTTP/1.1 |
| niquests core | 160 ms or ~6200 req/s | Asyncio. HTTP/2 |
Did you give up on HTTP/2 due to performance concerns? Think again! Do you realize that you can get 3 times faster with the same CPU if you ever switched to Niquests from Requests? Multiplexing and response lazyness open up a wide range of possibilities! Want to learn more about the tests? scripts? reasoning?
Take a deeper look at https://github.com/Ousret/niquests-stats
Update: The previous scenario is fairly complex to set up, and we were asked to provide a more "real life" benchmark scenario.
Given the script hosted at https://gist.github.com/Ousret/9e99b07e66eec48ccea5811775ec116d that simply execute 1k requests. We deliberately use two hosts, disabled Niquests manual multiplexing (for fairness), one known to be very slow (httpbin.org), and the other adequately fast (httpbingo.org) to showcase how the client behave.
Fast endpoint
| Client | Average Delay to Complete | Notes |
|---|---|---|
| httpx | 1.771s | HTTP/1.1 |
| aiohttp | 1.372s | HTTP/2 |
| niquests | 0.704s | HTTP/2 |
Slow endpoint
| Client | Average Delay to Complete | Notes |
|---|---|---|
| httpx | 8.19s | HTTP/1.1 |
| aiohttp | 7.041s | HTTP/2 |
| niquests | 4.201s | HTTP/2 |
:tada: Niquests can easily bring you twice the throughput if you migrated today. Join us today!
</details>>>> import niquests
>>> r = niquests.get('https://one.one.one.one')
>>> r.status_code
200
>>> r.headers['content-type']
'application/json; charset=utf-8'
>>> r.oheaders.content_type.charset
'utf-8'
>>> r.encoding
'utf-8'
>>> r.text
'{"authenticated": true, ...'
>>> r.json()
{'authenticated': True, ...}
>>> r
<Response HTTP/2 [200]>
>>> r.ocsp_verified
True
>>> r.conn_info.established_latency
datetime.timedelta(microseconds=38)
or using async/await!
import niquests
import asyncio
async def main() -> None:
r = await niquests.aget('https://one.one.one.one', stream=True)
print(r) # Output: <Response HTTP/2 [200]>
payload = await r.text # we await text because we set `stream=True`!
print(payload) # Output: <html>...
# or... without stream=True
r = await niquests.aget('https://one.one.one.one')
print(r) # Output: <Response HTTP/3 [200]>
payload = r.text # we don't need to away anything, it's already loaded!
print(payload) # Output: <html>...
asyncio.run(main())
Niquests allows you to send HTTP requests extremely easily. Thereβs no need to manually add query strings to your URLs, or to form-encode your PUT & POST data β just use the json method!
This project does not require any compilation toolchain. The HTTP/3 support is not enforced and installed if your platform can support it natively (e.g. pre-built wheel available).
β¨ Installing Niquests and Supported Versions
Niquests is available on PyPI:
$ python -m pip install niquests
Niquests officially supports Python or PyPy 3.7+.
π Supported Features & BestβPractices
Niquests is ready for the demands of building scalable, robust and reliable HTTPβspeaking applications.
- DNS over HTTPS, DNS ov