# put this in your model or controller
# to block bots that download too fast
BAN_IP_TIME = 60 * 60 * 24 # 1 day
ban_key = request.client + 'ban'
if cache.ram(ban_key, lambda: False, BAN_IP_TIME):
raise HTTP(429, 'IP blocked')
# maximum number of fast requests allowed before banned
MAX_REQUESTS = 10
request_key = request.client + 'requests'
cache.ram(request_key, lambda: 0, 1)
if cache.ram.increment(request_key) > MAX_REQUESTS:
cache.ram(ban_key, lambda: True, BAN_IP_TIME)
redirect(URL(f='index'))



Comments (0)