简体   繁体   English

在龙卷风服务器上请求控制

[英]requests control on Tornado server

I have a Tornado web server. 我有一个Tornado Web服务器。 I wonder if there is anyway I can control the number of incoming requests? 我想知道是否仍然可以控制传入请求的数量? I want to only accept x number of requests from a single client in a given timeframe. 我只想在给定的时间范围内接受来自单个客户端的x个请求。

Set a cookie with the expiry as the timeframe you want to be and use this cookie to keep count of requests. 设置一个带有到期时间的cookie作为您想要的时间范围,并使用该cookie保持请求计数。

code sample: 代码示例:

lets say you want the timeframe to be of one day, so here is how you would set the cookie. 假设您希望时间段为一天,因此这是设置Cookie的方法。 Do this when user logged in (or after any action you want): 用户登录时(或执行任何所需操作后)执行以下操作:

set_secure_cookie('requestscount', '0', expires_days=1)

and then check the count value before giving access to the resource: 然后在允许访问资源之前检查计数值:

user_requests = int(get_secure_cookie('requestscount'))
if user_requests < MAX_USER_REQUESTS:
    user_requests += 1
    set_secure_cookie('requestscount', str(user_requests), expires_days=1)
    # serve the resource to user
    ...

Of course there are other ways. 当然还有其他方法。 You could keep this count in a database instead of a cookie. 您可以将此计数保留在数据库中而不是cookie中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM