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,否则限制不起作用

你可能感兴趣的:(django设置接口请求频率限制)