django-drf 控制ip访问频率

# 对应视图文件 views.py
#***频率组件****
from rest_framework.throttling import BaseThrottle
VISIT_RECORD={}
class VisitThrottle(BaseThrottle):

    def __init__(self):
        self.history=None
    def allow_request(self,request,view):
        remote_addr = request.META.get('REMOTE_ADDR')
        print('请求的IP:',remote_addr)
        import time
        ctime=time.time()
        if remote_addr not in VISIT_RECORD:
            VISIT_RECORD[remote_addr]=[ctime,]
            return True
        history=VISIT_RECORD.get(remote_addr)
        self.history=history
        while history and history[-1]

你可能感兴趣的:(Python,Django)