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 aiohttp import web
from aiohttplimiter import default_keyfunc, Limiter
app = web.Application()
routes = web.RouteTableDef()
limiter = Limiter(keyfunc=default_keyfunc)
@routes.get("/")
# This endpoint can only be requested 1 time per second per IP address
@limiter.limit("1/1")
async def home(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 Limiter, 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.
limiter = Limiter(keyfunc=default_keyfunc, exempt_ips={"192.168.1.245"})
@routes.get("/")
@limiter.limit("1/1")
async def test(request):
return web.Response(text="test")
app.add_routes(routes)
web.run_app(app)
You can limit how much memory aiohttp-ratelimiter can use to store ratelimiting info with the max_memory kwargs. This kwarg limits the max amount of gigabytes aiohttp-ratelimiter can use to store ratelimiting info. The default is None, which means aiohttp-ratelimiter can use as much as it can before throwing a MemoryError.
from aiohttp import web
from aiohttplimiter import default_keyfunc, Limiter
app = web.Application()
routes = web.RouteTableDef()
# aiohttp-ratelimiter can only store 0.5 gigabytes of ratelimiting data.
# When the limit is reached the data resets.
# Please note that the number is not exact. It might be a little over 0.5.
limiter = Limiter(keyfunc=default_keyfunc, max_memory=.5)
@routes.get("/")
@limiter.limit("1/1")
def home(request):
return web.Response(text="test")
app.add_routes(routes)
web.run_app(app)
If you have any middlewares, just specify the amount 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-3.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e738b198a258019573670925ba18ed866ac269d7fa8a555ef5929e08a29c996 |
|
MD5 | 41bc41df90dc51274a84c29aa714b436 |
|
BLAKE2b-256 | 6c710af52c7fcfd73893990fae96715b28187193ac8441b228df574ede54851e |