Django 插件实现请求次数限制

为了防止用户恶意访问接口(爬虫), 可以对一定时间内请求次数做限制. 若超出限制次数, 则返回错误页面

此处使用的插件为django-ratelimit, 它可以通过ip限制,也可以通过访问次数限制. 使用很灵活

安装

pip install django-ratelimit

官方文档

http://django-ratelimit.readthedocs.io/en/v1.0.0/

使用案例

from ratelimit.decorators import ratelimit


@ratelimit(group=None, key=, rate=None, method=ALL, block=False)
def myview(request):
    pass

大致用法: (翻译不通)
| Parameters: |

  • groupNone A group of rate limits to count together. Defaults to the dotted name of the view.

  • key – What key to use, see Keys.

  • rate
    请求次数限制, 5/m为5次每分钟, 单位可以是秒, 分, 时, 日
    ‘5/m’ The number of requests per unit time allowed. Valid units are:

    • s - seconds
    • m - minutes
    • h - hours
    • d - days

    Also accepts callables. See Rates.

  • methodALL Which HTTP method(s) to rate-limit. May be a string, a list/tuple of strings, or the special values for ALL or UNSAFE (which includes POST, PUT, DELETE and PATCH).

  • blockFalse Whether to block the request instead of annotating.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,178评论 0 10
  • 不知不觉做妈妈已经3个月17天,小宝贝胖嘟嘟的已是爱说爱笑,咿咿呀呀眉开眼笑的时候让人舍不得挪开视线,莫名其妙哭闹...
    小胖果妈咪阅读 3,863评论 4 8
  • 2017.4.27 星期四 累计89 一、目标: 新一轮目标仍然要实现财富收入50万元(7月底)。通过我的目标实现...
    鹊曾阅读 1,328评论 0 1

友情链接更多精彩内容