django设置接口请求频率限制

pip3 install django-ratelimit
from ratelimit.decorators import ratelimit

@ratelimit(key='ip')
def myview(request):
    # ...

@ratelimit(key='ip', rate='100/h')
def secondview(request):
    # ...

@ratelimit(group=None, key=, rate=None, method=ALL, block=False)

key参数

  • 'ip' - Use the request IP address (i.e. request.META['REMOTE_ADDR'])
  • 'get:X' - Use the value of request.GET['X']
  • 'post:X' - Use the value of request.POST['X']
  • 'header:x-x' - Use the value of request.META['HTTP_X_X']
    冒号的右值将被转换为全大写,任何破折号都将被下划线替换,例如:x-client-ip => X_CLIENT_IP
  • 'user' - Use an appropriate value from request.user. Do not use with unauthenticated users
  • 'user_or_ip' - Use an appropriate value from request.user if the user is authenticated, otherwise use request.META['REMOTE_ADDR']

频率rate参数
s - second
m - minute
h - hour
d - day

100/5m
100/300s
100/300

注意:block要设为True,否则限制不起作用

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

相关阅读更多精彩内容

  • 英雄联盟. 精灵CP组合 孙苹表白刘妞 谢谢你,悄悄地私信我,给我的文章提修改意见; 谢谢你,那么忙还愿意接受我的...
    佳佳_c2e1阅读 1,247评论 0 0

友情链接更多精彩内容