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

自定义频率组件,django自带的组件,解析器

时间:2018-12-14 21:09:31      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:ica   class   api   定义   href   调用   视图   scope   int   

自定义频率组件

from rest_framework.throttling import BaseThrottle, SimpleRateThrottle
        import time

        # 设置一分钟只能访问三次
        class MyThrottle(BaseThrottle):
            visitor_dic = {}

            def __init__(self):
                self.history = None

            def allow_request(self, request, view):
                # META:请求所有的东西的字典
                # 拿出ip地址
                ip = request.META.get(REMOTE_ADDR)
                ctime = time.time()

                # 判断ip在不在字典里,不再说明是第一次访问,往字典里添加时间
                if ip not in self.visitor_dic:
                    self.visitor_dic[ip] = [ctime, ]
                    return True

                # ip在字典里,取出ip对应的访问时间列表
                history = self.visitor_dic[ip]
                self.history = history
                # 当字典里有值和列表里当前时间减去第一次的时间差大于60s,超过了一分钟就不让访问,
                # 则把最后一次时间删掉
                while history and ctime - history[-1] > 60:
                    history.pop()
                # 判断列表里时间元素,是不是大于3,,大于3直接False
                if len(history) < 3:
                    # 把当前时间放在列表第0位
                    history.insert(0, ctime)
                    return True
                # 大于3直接False
                return False

            -设置等待时间
            def wait(self):
                ctime = time.time()
                # ctime - self.history[-1] 表示当前时间超了多长时间
                print(self.history[-1])
                # 当前时间减去第一次访问时间
                return 60 - (ctime - self.history[-1])

            -错误信息显示成中文
            def throttled(self, request, wait):
                class MyThrottled(exceptions.Throttled):
                    default_detail = 傻逼
                    extra_detail_singular = 还剩 {wait} 秒.
                    extra_detail_plural = 还剩 {wait} 秒
                raise MyThrottled(wait)

视图函数中

-频率组件局部使用
  # 调用上面的频率组件
  throttle_classes = [ MyThrottle,]

频率组件全局配置

settings里
REST_FRAMEWORK = {
  # 自定义频率组件全局使用
  DEFAULT_THROTTLE_CLASSES: [app01.MyAuth.MyThrottle, ],
}

 

 

django自带的频率组建

class MyThrottle(SimpleRateThrottle):
  scope = aaa
  def get_cache_key(self, request, view):
    return self.get_ident(request)

视图里引用上面自带的频率组件

class Test(APIView):
    # 调用频率组件
    throttle_classes = [MyThrottle,]

    def get(self, request):
        print(type(request._request))
        print(request._request)
        print(request.data.get(name))
        return HttpResponse(ok)

    def post(self, request):
        print(request.data)
        return HttpResponse(post)

    # 错误信息显示成中文
    def throttled(self, request, wait):
        class MyThrottled(exceptions.Throttled):
            default_detail = 傻逼
            extra_detail_singular = 还剩 {wait} 秒.
            extra_detail_plural = 还剩 {wait} 秒

        raise MyThrottled(wait)

自带的频率组建全局配置

REST_FRAMEWORK = {
    # django自带频率组件使用
    DEFAULT_THROTTLE_RATES: {
        aaa: 10/m
},

 

RestFramework之解析器

解析器:

对请求的数据进行解析-请求体进行解析。解析器在你不拿请求体数据时,不会被调用。

 

from rest_framework.parsers import JSONParser,FormParser

解析器局部使用

在视图类中
  # 两种格式都能解析,不写全部解析
  parser_classes = [FormParser,JSONParser]

解析器全局配置

REST_FRAMEWORK = {
# 解析器全局使用
‘DEFAULT_PARSER_CLASSES‘: [
‘rest_framework.parsers.JSONParser‘, # 客户端发送 application/json 的形式的值
‘rest_framework.parsers.FormParser‘ # 客户端发送 application/x-www-form-urlencoded 的形式的值
]
}

 

自定义频率组件,django自带的组件,解析器

标签:ica   class   api   定义   href   调用   视图   scope   int   

原文地址:https://www.cnblogs.com/liu--huan/p/10121498.html

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