If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!
# 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'))

 

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere