aiohttp-ratelimiter


Nameaiohttp-ratelimiter JSON
Version 4.1.2 PyPI version JSON
download
home_pagehttps://jgltechnologies.com/aiohttplimiter
SummaryA simple rate limiter for aiohttp.web
upload_time2023-07-13 13:53:59
maintainer
docs_urlNone
authorGeorge Luca
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <a href="https://jgltechnologies.com/discord">
<img src="https://discord.com/api/guilds/844418702430175272/embed.png">
</a>

# aiohttp-ratelimiter

aiohttp-ratelimiter is a rate limiter for the aiohttp.web framework.
This is a new library, and we are always looking for people to contribute. If you see something wrong with the code or want to add a feature, please create a pull request 
on <a href="https://jgltechnologies.com/aiohttplimiter">our github</a>.


Install from git
```
python -m pip install git+https://github.com/JGLTechnologies/aiohttp-ratelimiter
```

Install from pypi
```
python -m pip install aiohttp-ratelimiter
// if redis is being used
python -m pip install aiohttp-ratelimiter[redis]
// if memcached is being used
python -m pip install aiohttp-ratelimiter[memcached]
```

<br>


Example

```python
from aiohttp import web
from aiohttplimiter import default_keyfunc, Limiter
from aiohttplimiter.redis_limiter import RedisLimiter
from aiohttplimiter.memcached_limiter import MemcachedLimiter

app = web.Application()
routes = web.RouteTableDef()

# In Memory
limiter = Limiter(keyfunc=default_keyfunc)
# Redis
limiter = RedisLimiter(keyfunc=default_keyfunc, uri="redis://localhost:6379")
# Memcached
limiter = MemcachedLimiter(keyfunc=default_keyfunc, uri="memcached://localhost:11211")

@routes.get("/")
# This endpoint can only be requested 1 time per second per IP address
@limiter.limit("1/second")
async def home(request):
    return web.Response(text="test")

app.add_routes(routes)
web.run_app(app)
```

<br>

You can exempt an IP from rate limiting using the exempt_ips kwarg.

```python
from aiohttplimiter import Limiter, default_keyfunc
from aiohttp import web

app = web.Application()
routes = web.RouteTableDef()

# 192.168.1.245 is exempt from rate limiting.
# 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("3/5minutes")
async def test(request):
    return web.Response(text="test")

app.add_routes(routes)
web.run_app(app)
```

<br>

You can create your own error handler by using the error_handler kwarg.

```python
from aiohttplimiter import Allow, RateLimitExceeded, Limiter, default_keyfunc
from aiohttp import web

def handler(request: web.Request, exc: RateLimitExceeded):
    # If for some reason you want to allow the request, return aiohttplimitertest.Allow().
    if some_condition:
        return Allow()
    return web.Response(text=f"Too many requests", status=429)

limiter = Limiter(keyfunc=default_keyfunc, error_handler=handler)
```

<br>

If multiple paths use one handler like this:
```python
@routes.get("/")
@routes.get("/home")
@limiter.limit("5/hour")
def home(request):
    return web.Response(text="Hello")
```

<br>

Then they will have separate rate limits. To prevent this use the path_id kwarg.

```python
@routes.get("/")
@routes.get("/home")
@limiter.limit("2/3days", path_id="home")
def home(request):
    return web.Response(text="Hello")
```

<br>

Views Example 

```python
@routes.view("/")
class Home(View):
    @limiter.limit("1/second")
    def get(self):
        return web.Response(text="hello")
```






            

Raw data

            {
    "_id": null,
    "home_page": "https://jgltechnologies.com/aiohttplimiter",
    "name": "aiohttp-ratelimiter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "George Luca",
    "author_email": "fixingg@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/16/bd/211eec5af5becb0f24fe142fefda25c0d1f9fc00a8eb08dccefd253416f5/aiohttp-ratelimiter-4.1.2.tar.gz",
    "platform": null,
    "description": "<a href=\"https://jgltechnologies.com/discord\">\r\n<img src=\"https://discord.com/api/guilds/844418702430175272/embed.png\">\r\n</a>\r\n\r\n# aiohttp-ratelimiter\r\n\r\naiohttp-ratelimiter is a rate limiter for the aiohttp.web framework.\r\nThis is a new library, and we are always looking for people to contribute. If you see something wrong with the code or want to add a feature, please create a pull request \r\non <a href=\"https://jgltechnologies.com/aiohttplimiter\">our github</a>.\r\n\r\n\r\nInstall from git\r\n```\r\npython -m pip install git+https://github.com/JGLTechnologies/aiohttp-ratelimiter\r\n```\r\n\r\nInstall from pypi\r\n```\r\npython -m pip install aiohttp-ratelimiter\r\n// if redis is being used\r\npython -m pip install aiohttp-ratelimiter[redis]\r\n// if memcached is being used\r\npython -m pip install aiohttp-ratelimiter[memcached]\r\n```\r\n\r\n<br>\r\n\r\n\r\nExample\r\n\r\n```python\r\nfrom aiohttp import web\r\nfrom aiohttplimiter import default_keyfunc, Limiter\r\nfrom aiohttplimiter.redis_limiter import RedisLimiter\r\nfrom aiohttplimiter.memcached_limiter import MemcachedLimiter\r\n\r\napp = web.Application()\r\nroutes = web.RouteTableDef()\r\n\r\n# In Memory\r\nlimiter = Limiter(keyfunc=default_keyfunc)\r\n# Redis\r\nlimiter = RedisLimiter(keyfunc=default_keyfunc, uri=\"redis://localhost:6379\")\r\n# Memcached\r\nlimiter = MemcachedLimiter(keyfunc=default_keyfunc, uri=\"memcached://localhost:11211\")\r\n\r\n@routes.get(\"/\")\r\n# This endpoint can only be requested 1 time per second per IP address\r\n@limiter.limit(\"1/second\")\r\nasync def home(request):\r\n    return web.Response(text=\"test\")\r\n\r\napp.add_routes(routes)\r\nweb.run_app(app)\r\n```\r\n\r\n<br>\r\n\r\nYou can exempt an IP from rate limiting using the exempt_ips kwarg.\r\n\r\n```python\r\nfrom aiohttplimiter import Limiter, default_keyfunc\r\nfrom aiohttp import web\r\n\r\napp = web.Application()\r\nroutes = web.RouteTableDef()\r\n\r\n# 192.168.1.245 is exempt from rate limiting.\r\n# Keep in mind that exempt_ips takes a set not a list.\r\nlimiter = Limiter(keyfunc=default_keyfunc, exempt_ips={\"192.168.1.245\"})\r\n\r\n@routes.get(\"/\")\r\n@limiter.limit(\"3/5minutes\")\r\nasync def test(request):\r\n    return web.Response(text=\"test\")\r\n\r\napp.add_routes(routes)\r\nweb.run_app(app)\r\n```\r\n\r\n<br>\r\n\r\nYou can create your own error handler by using the error_handler kwarg.\r\n\r\n```python\r\nfrom aiohttplimiter import Allow, RateLimitExceeded, Limiter, default_keyfunc\r\nfrom aiohttp import web\r\n\r\ndef handler(request: web.Request, exc: RateLimitExceeded):\r\n    # If for some reason you want to allow the request, return aiohttplimitertest.Allow().\r\n    if some_condition:\r\n        return Allow()\r\n    return web.Response(text=f\"Too many requests\", status=429)\r\n\r\nlimiter = Limiter(keyfunc=default_keyfunc, error_handler=handler)\r\n```\r\n\r\n<br>\r\n\r\nIf multiple paths use one handler like this:\r\n```python\r\n@routes.get(\"/\")\r\n@routes.get(\"/home\")\r\n@limiter.limit(\"5/hour\")\r\ndef home(request):\r\n    return web.Response(text=\"Hello\")\r\n```\r\n\r\n<br>\r\n\r\nThen they will have separate rate limits. To prevent this use the path_id kwarg.\r\n\r\n```python\r\n@routes.get(\"/\")\r\n@routes.get(\"/home\")\r\n@limiter.limit(\"2/3days\", path_id=\"home\")\r\ndef home(request):\r\n    return web.Response(text=\"Hello\")\r\n```\r\n\r\n<br>\r\n\r\nViews Example \r\n\r\n```python\r\n@routes.view(\"/\")\r\nclass Home(View):\r\n    @limiter.limit(\"1/second\")\r\n    def get(self):\r\n        return web.Response(text=\"hello\")\r\n```\r\n\r\n\r\n\r\n\r\n\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple rate limiter for aiohttp.web",
    "version": "4.1.2",
    "project_urls": {
        "Homepage": "https://jgltechnologies.com/aiohttplimiter"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "16bd211eec5af5becb0f24fe142fefda25c0d1f9fc00a8eb08dccefd253416f5",
                "md5": "5c074838f02322f6f9b53c97faba5f39",
                "sha256": "cc6cada2b8a91a013aca7a2468835f0cdc6b9ea2b7425bc7dbe1969a1b80b6e8"
            },
            "downloads": -1,
            "filename": "aiohttp-ratelimiter-4.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "5c074838f02322f6f9b53c97faba5f39",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5313,
            "upload_time": "2023-07-13T13:53:59",
            "upload_time_iso_8601": "2023-07-13T13:53:59.756949Z",
            "url": "https://files.pythonhosted.org/packages/16/bd/211eec5af5becb0f24fe142fefda25c0d1f9fc00a8eb08dccefd253416f5/aiohttp-ratelimiter-4.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-13 13:53:59",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "aiohttp-ratelimiter"
}
        
Elapsed time: 0.37729s