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 ofrequest.GET['X']
-
'post:X'
- Use the value ofrequest.POST['X']
-
'header:x-x'
- Use the value ofrequest.META['HTTP_X_X']
冒号的右值将被转换为全大写,任何破折号都将被下划线替换,例如:x-client-ip => X_CLIENT_IP -
'user'
- Use an appropriate value fromrequest.user
. Do not use with unauthenticated users -
'user_or_ip'
- Use an appropriate value fromrequest.user
if the user is authenticated, otherwise userequest.META['REMOTE_ADDR']
频率rate参数
s - second
m - minute
h - hour
d - day
100/5m
100/300s
100/300
注意:block要设为True,否则限制不起作用
网友评论