美文网首页
Django 插件实现请求次数限制

Django 插件实现请求次数限制

作者: 空山晚来秋 | 来源:发表于2018-03-26 18:32 被阅读0次

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

    此处使用的插件为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.

    相关文章

      网友评论

          本文标题:Django 插件实现请求次数限制

          本文链接:https://www.haomeiwen.com/subject/learcftx.html