首页 > 编程语言 >学习python-Day70

学习python-Day70

时间:2022-10-10 23:11:57浏览次数:41  
标签:return python ip self 学习 Day70 classes class history

今天学习内容

一、自定义频率类

from rest_framework.throttling import BaseThrottle
class MyThrottle(BaseThrottle):
    VISIT_RECORD = {}  # 存放用户访问记录{ip1:[时间1,时间2],ip2:[时间1,时间2],'192.168.1.101':[当前时间,]}

    def __init__(self):
        self.history = None
    def allow_request(self, request, view):
        # 在这里写逻辑:根据ip地址判断用户是不是超过了频率限制
        # (1)取出访问者ip
        ip = request.META.get('REMOTE_ADDR')
        import time
        ctime = time.time() # 取出当前时间
        # (2)判断当前ip不在访问字典里,添加进去,并且直接返回True,表示第一次访问
        if ip not in self.VISIT_RECORD:
            self.VISIT_RECORD[ip] = [ctime, ]
            return True
        self.history = self.VISIT_RECORD.get(ip) # 当前访问者的时间列表 [时间2,]
        # (3)循环判断当前ip的时间列表,有值,并且当前时间减去列表的最后一个时间大于60s,把这种数据pop掉,这样列表中只有60s以内的访问时间,
        while self.history and -ctime + self.history[-1] < 60: #循环结束后,剩下的都是1分钟以后访问的时间
            self.history.pop()
        # (4)判断,当列表小于3,说明一分钟以内访问不足三次,把当前时间插入到列表第一个位置,返回True,顺利通过
        # (5)当大于等于3,说明一分钟内访问超过三次,返回False验证失败
        if len(self.history) < 3:
            self.history.insert(0, ctime)
            return True
        else:
            return False

    def wait(self):
        import time
        ctime = time.time()
        return 60 - (ctime - self.history[-1])

二、频率功能源码剖析

SimpleRateThrottle

* 源码里执行的频率类的allow_request, 读SimpleRateThrottle的allow_request

class SimpleRateThrottle(BaseThrottle):
    cache = default_cache
    timer = time.time
    cache_format = 'throttle_%(scope)s_%(ident)s'
    scope = None
    THROTTLE_RATES = api_settings.DEFAULT_THROTTLE_RATES
    def __init__(self):  # 只要类实例化得到对象就会执行,一执行,self.rate就有值了,而且self.num_requests和self.duration
        if not getattr(self, 'rate', None): # 去频率类中反射rate属性或方法,发现没有,返回了None,这个if判断就符合,执行下面的代码
            self.rate = self.get_rate()  #返回了  '3/m'
        #  self.num_requests=3
        #  self.duration=60
        self.num_requests, self.duration = self.parse_rate(self.rate)

    def get_rate(self):
         return self.THROTTLE_RATES[self.scope] # 字典取值,配置文件中咱们配置的字典{'ss': '3/m',},根据ss取到了 '3/m'

    def parse_rate(self, rate):
        if rate is None:
            return (None, None)
        # rate:字符串'3/m'  根据 / 切分,切成了 ['3','m']
        # num=3,period=m
        num, period = rate.split('/')
        # num_requests=3  数字3
        num_requests = int(num)
        # period='m'  ---->period[0]--->'m'
        # {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
        # duration=60
        duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
        # 3     60
        return (num_requests, duration)

    def allow_request(self, request, view):
        if self.rate is None:
            return True
        # 咱们自己写的,返回什么就以什么做限制  咱们返回的是ip地址
        # self.key=当前访问者的ip地址
        self.key = self.get_cache_key(request, view)
        if self.key is None:
            return True
        # self.history 访问者的时间列表,从缓存中拿到,如果拿不到就是空列表,如果之前有 [时间2,时间1]
        self.history = self.cache.get(self.key, [])
        # 当前时间
        self.now = self.timer()
        while self.history and self.history[-1] <= self.now - self.duration:
            self.history.pop()
        if len(self.history) >= self.num_requests:
            return self.throttle_failure()
        return self.throttle_success()

总结:

写频率类,只需要继承SimpleRateThrottle,重写get_cache_key,配置类属性scope,配置文件中配置一下就可以了。

三、分页功能

查询所有的接口才需要分页,分页后端写法是固定的,前端展示形式是不一样的。

pc端  下一页是点击
app   下一页是下拉更多

drf中分页的使用

  1. 写一个类,继承drf提供的三个分页类之一

  2. 重写某几个类的属性

    from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
    
    
    class XPageNum(PageNumberPagination):  # 使用最多, 写4个类属性即可
        page_size = 2  # 每页显示的条数
        page_query_param = 'page'  # 查询第几页的参数 /books/?page=3
        page_size_query_param = 'size'  # 查询第三页,每页显示4条  /books/?page=3&size=4
        max_page_size = 5  # 限制通过size查询,最大的条数
    
    
    class CLimitOffset(LimitOffsetPagination):
        default_limit = 2  # 每页显示的条数
        limit_query_param = 'limit'  # /books/?limit=4   这一页显示4条数据
        offset_query_param = 'offset'  # /books/?offset=3&limit=4  # 从第3条开始,取4条数据
        max_limit = 5  # 限制每页最多显示条数
    
    
    class CCursor(CursorPagination):  # 只能上一页和下一页,它的分页效果是最高的,高于上面所有的分页的方式,大数据量的分页,建议使用这种。
        cursor_query_param = 'cursor'
        page_size = 3  # 每页显示条数
        ordering = 'id'  # 排序,必须是表中得字段
    

    .

    image

    .

    image

    .

    image

  3. 把它配置在继承自GenericAPIView + ListModelMixin的视图子类上

    class AuthorView(ViewSetMixin, ListAPIView, CreateModelMixin):
        # 必须继承GenericAPIView
        pagination_class = XPageNum
        # pagination_class = CLimitOffset
        # pagination_class = CCursor
    
        authentication_classes = []
        permission_classes = []
        throttle_classes = []
        queryset = Author.objects.all()
        serializer_class = AuthorSerializer
    
  4. 如果继承的是APIView,需要自己写

    class BookDView(APIView):
        authentication_classes = [LoginAuth, ]
        permission_classes = []
        throttle_classes = [ZThrottle, ]
    
        def get(self, request):
            qs = Book.objects.all()
            page = XPageNum()
            res = page.paginate_queryset(qs, request)
            ser = BookSerializer(instance=res, many=True)
            return Response(ser.data)
    

四、排序功能

  • 查询所有才涉及到排序,其他接口都不需要

  • 必须是继承GenericAPIView + ListModelMixin的视图子类上

    -配置排序类:
        filter_backends=[OrderingFilter,]
    -配置排序的字段
        ordering_fields=['id','price']
    -支持前端的访问形式
        http://127.0.0.1:8000/books/?ordering=-price,id # 先按价格的降序排,如果价格一样再按id的升序排
     
    自己写,继承APIView,需要从请求地址中取出排序规则,自己排序。
    * 'price','id'=reqeust.query_params.get('ordering').split(',')
    * qs = Book.objects.all().order_by('price','-id')
    
  • 分页和排序能一起用,但是是先排序后分页的

  • class BookView(ViewSetMixin,
                   ListCreateAPIView,
                   RetrieveUpdateDestroyAPIView):
        authentication_classes = [LoginAuth, ]
        permission_classes = []
        throttle_classes = [ZThrottle, ]
        queryset = Book.objects.all()
        serializer_class = BookSerializer
        
        
        # # 排序类的配置 # 指定过滤类
        filter_backends = [OrderingFilter, SearchFilter, ]
        # 指定按哪个字段排序
        ordering_fields = ['id', 'price']
        # 指定过滤字段
        search_fields = ['name', 'price']
    

五、过滤功能

内置过滤

  • 查询所有才涉及到过滤,其他接口都不需要

  • restful规范中有一条,请求地址带过滤条件:分页,排序,过滤统称为过滤

  • 使用内置过滤类使用步骤:

    必须是继承GenericAPIView+ListModelMixin的子视图类上
      -配置过滤类:
            filter_backends=[SearchFilter,]
    -配置过滤的字段
            ordering_fields=['name','publish']
    -支持前端的访问形式
            http://127.0.0.1:8000/books/?search=三
     # 只要name中有该字符就能查找出来。
    
  • 内置过滤类只能通过search写条件,如果配置了多个过滤字段,是'或者'的条件。

  • 第三方:过滤类, 自己写:自己写过滤类。

image

作业

1 自定义频率类,写一遍
import time


class ZThrottle(BaseThrottle):
    user_ip = {}

    def __init__(self):
        self.history = None

    def allow_request(self, request, view):

        t = time.time()
        ip = request.META.get('REMOTE_ADDER')
        if ip not in self.user_ip:
            self.user_ip[ip] = [t]
            return True
        self.history = self.user_ip.get(ip)
        while self.history and t - self.history[-1] > 60:
            self.history.pop()
        if len(self.history) < 3:
            self.history.insert(0, t)
            return True
        else:
            return False

    def wait(self):
        t1 = time.time()
        wait_time = 60 - (t1 - self.history[-1])
        return wait_time

2 使用3种分页方式,实现对查询所有数据接口的分页
class AuthorView(ViewSetMixin, ListAPIView, CreateModelMixin):
    # 必须继承GenericAPIView
    pagination_class = XPageNum
    # pagination_class = CLimitOffset
    # pagination_class = CCursor

    authentication_classes = []
    permission_classes = []
    throttle_classes = []
    queryset = Author.objects.all()
    serializer_class = AuthorSerializer

3 带排序,带按名字过滤
class BookView(ViewSetMixin,
               ListCreateAPIView,
               RetrieveUpdateDestroyAPIView):
    authentication_classes = [LoginAuth, ]
    permission_classes = []
    throttle_classes = [ZThrottle, ]
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    # # 排序类的配置 # 指定过滤类
    filter_backends = [OrderingFilter, SearchFilter, ]
    # 指定按哪个字段排序
    ordering_fields = ['id', 'price']
    # 指定过滤字段
    search_fields = ['name', 'price']
#----部分人写----
4 继承APIView,实现分页,返回格式跟之前一样
class BookDView(APIView):
    authentication_classes = [LoginAuth, ]
    permission_classes = []
    throttle_classes = [ZThrottle, ]

    def get(self, request):
        qs = Book.objects.all()
        page = XPageNum()
        res = page.paginate_queryset(qs, request)
        ser = BookSerializer(instance=res, many=True)
        from collections import OrderedDict
        return Response(OrderedDict([
            ('count', page.page.paginator.count),
            ('next', page.get_next_link()),
            ('previous', page.get_previous_link()),
            ('results', ser.data)
        ]))

urls.py

image

.

image

标签:return,python,ip,self,学习,Day70,classes,class,history
From: https://www.cnblogs.com/bjyxxc/p/16777773.html

相关文章