码迷,mamicode.com
首页 > 其他好文 > 详细

Django REST Framework限速

时间:2017-10-19 14:06:54      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:settings   eth   day   target   限速   ott   ros   es2017   anon   

官方文档:http://www.django-rest-framework.org/api-guide/throttling/#throttling

settings.py配置

REST_FRAMEWORK = {
    ‘DEFAULT_THROTTLE_CLASSES‘: (
        ‘rest_framework.throttling.AnonRateThrottle‘,
        ‘rest_framework.throttling.UserRateThrottle‘
    ),
    ‘DEFAULT_THROTTLE_RATES‘: {
        ‘anon‘: ‘100/day‘,
        ‘user‘: ‘1000/day‘
    }
}

AnonRateThrottle:用户未登录请求限速,通过IP地址判断

UserRateThrottle:用户登陆后请求限速,通过token判断

DEFAULT_THROTTLE_RATES 包括 second, minute, hour, day

 

引用样例:

from rest_framework.response import Response
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView

class ExampleView(APIView):
    throttle_classes = (UserRateThrottle,)

    def get(self, request, format=None):
        content = {
            ‘status‘: ‘request was permitted‘
        }
        return Response(content)

技术分享

 

Django REST Framework限速

标签:settings   eth   day   target   限速   ott   ros   es2017   anon   

原文地址:http://www.cnblogs.com/shhnwangjian/p/7691950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!