Skip to main content
 首页 » 编程设计

python之Django REST 如何设置 throttle 时间以允许 10 分钟内发出一个请求

2024年12月31日33kerrycode

文档说该期间应该是以下之一:('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')。我很好奇是否可以将周期设置为类似 1/10min 的值?

请您参考如下方法:

查看 code和 documentation你不能“开箱即用”。但我确实看到了制造你自己的可能性 custom throttle基于现有的一个:

from rest_framework.throttling import AnonRateThrottle 
 
 
class AnonTenPerTenMinutesThrottle(AnonRateThrottle): 
    def parse_rate(self, rate): 
        """ 
        Given the request rate string, return a two tuple of: 
        <allowed number of requests>, <period of time in seconds> 
 
        So we always return a rate for 10 request per 10 minutes. 
 
        Args: 
            string: rate to be parsed, which we ignore. 
 
        Returns: 
            tuple:  <allowed number of requests>, <period of time in seconds> 
        """ 
        return (10, 600)