zoukankan      html  css  js  c++  java
  • django限流全局和单个视图

    全局匿名和用户节流:

        # 限流
        'DEFAULT_THROTTLE_CLASSES': [
            'rest_framework.throttling.AnonRateThrottle',
            'rest_framework.throttling.UserRateThrottle'
        ],
        'DEFAULT_THROTTLE_RATES': {
            'anon': '300/day',  # 匿名用户
            'user': '5000/day'  # 用户
        }
    

      

    # 单个视图限流:

    ScopedRateThrottle

    ScopedRateThrottle类可用于限制访问API的特定部分。仅当所访问的视图包含.throttle_scope属性时,才会应用此限制然后,通过将请求的“范围”与唯一的用户ID或IP地址串联起来,即可形成唯一的限制键。

    允许的请求速率由DEFAULT_THROTTLE_RATES设置使用请求“范围”中的键确定

    例如,给定以下视图...

    class ContactListView(APIView):
        throttle_scope = 'contacts'
        ...
    
    class ContactDetailView(APIView):
        throttle_scope = 'contacts'
        ...
    
    class UploadView(APIView):
        throttle_scope = 'uploads'
        ...

    ...以及以下设置。

    REST_FRAMEWORK = {
        'DEFAULT_THROTTLE_CLASSES': [
            'rest_framework.throttling.ScopedRateThrottle',
        ],
        'DEFAULT_THROTTLE_RATES': {
            'contacts': '1000/day',
            'uploads': '20/day'
        }
    }

    用户每天对一个请求的请求ContactListViewContactDetailView将被限制为总共1000个请求。用户对的请求UploadView将被限制为每天20个请求。

    官方文档:

    https://www.django-rest-framework.org/api-guide/throttling/

  • 相关阅读:
    正则表达式
    HDU 2066 多源最短路
    UVA 11039 模拟
    Concrete Mathematics Chapter 1 Warmups
    List differences between JAVA and C++
    uva 11107Life Forms
    poj 1509 Glass Beads
    poj 3581
    网络流建图
    图论算法----网络流
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/14343990.html
Copyright © 2011-2022 走看看