A simple ratelimiter for aiohttp.web
Project description
aiohttp-ratelimiter
This library allows you to add a rate limit to your aiohttp.web app.
Install from git
python -m pip install git+https://github.com/Nebulizer1213/aiohttp-ratelimiter
Install from pypi
python -m pip install aiohttp-ratelimiter
Example
from aiohttplimiter import limit, default_keyfunc
from aiohttp import web
app = web.Application()
routes = web.RouteTableDef()
# This endpoint can only be requested one time per second per IP address.
@routes.get("/")
@limit(ratelimit="1/1", keyfunc=default_keyfunc)
async def test(request):
return web.Response(text="test")
app.add_routes(routes)
web.run_app(app)
You can exempt an IP from ratelimiting using the exempt_ips kwarg.
from aiohttplimiter import limit, default_keyfunc
from aiohttp import web
app = web.Application()
routes = web.RouteTableDef()
# 192.168.1.245 is exempt from ratelimiting.
# Keep in mind that exempt_ips takes a set not a list.
@routes.get("/")
@limit(ratelimit="1/1", keyfunc=default_keyfunc, exempt_ips={"192.168.1.245"})
async def test(request):
return web.Response(text="test")
app.add_routes(routes)
web.run_app(app)
If you plan on having the same keyfunc and exempt IPs for each endpoint using the Limiter class would be easier.
from aiohttp import web
from aiohttplimiter import default_keyfunc, Limiter
app = web.Application()
routes = web.RouteTableDef()
limiter = Limiter(keyfunc=default_keyfunc, exempt_ips={"192.168.1.235"})
@routes.get("/")
@limiter.limit("1/5")
def home(request):
return web.Response(text="test")
app.add_routes(routes)
web.run_app(app)
If you have any middlewares just specify the ammount in the middleware_count kwarg.
limiter = Limiter(keyfunc=default_keyfunc, exempt_ips={"192.168.1.235"}, middleware_count=1)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for aiohttp-ratelimiter-2.0.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed468fc406ab4825728e515f4cb7b665affc9929afbd8995b779a451675bec06 |
|
MD5 | 1a4f178d374f5e48a18faf20645bb974 |
|
BLAKE2b-256 | 285866241fb2420fa71fb042208fa1f6f989fe3e98726681d43f6cf46c4092f0 |