How to Fail silently if ip is blacklisted

56 views
Skip to first unread message

Alfonso Serra

unread,
Dec 5, 2015, 7:41:09 PM12/5/15
to web2py-users
Hi, im working on a blacklist and i was wondering if can make web2py to fail silently on an early stage if the client is blacklisted.

I have something like this in my models.

#blacklist an ip
def blacklist(ip):
   
if ip:
        db
.blacklist.insert(ip=ip)

#hidden ips are considered black
def isblack(ip):
   
if ip:
       
if not db(db.blacklist.ip==ip).select().first():
           
return False
   
return True

if isblack(request.client):
   
raise
 


Ive tested this on localhost and i get a ticket with a socket error, which is ok, but will i get a ticket if i deploy the app?.

I know could use "raise HTTP(404)" but i dont want the server to go any further than that, waste any resource or even throw a ticket. Is it any way?

Thanks.

Anthony

unread,
Dec 5, 2015, 8:46:07 PM12/5/15
to web...@googlegroups.com
I don't think you can avoid making a response from within your app code, as everything is wrapped in a try/except, so whatever happens, web2py will eventually complete the response. You could always return an empty HTML page.

If you really want to minimize server resources, you should instead configure your web server or maybe iptables to do the blocking before anything gets to web2py). If necessary, you could potentially even use Python/web2py to update the list of blocked addresses dynamically.

Anthony

Alfonso Serra

unread,
Dec 5, 2015, 11:01:31 PM12/5/15
to web2py-users
Thanks Anthony. Ill read more about how the ticket system behaves if the request is not local.
I guess that an empty response is acceptable by now. Ill post if i make any progress.

By the way i was able to fix the problem at my other post with what you said. ill make a tutorial about it,  was lots of fun hacking form.custom.end and such.

Best Regards.

Anthony

unread,
Dec 6, 2015, 12:27:51 AM12/6/15
to web2py-users
On Saturday, December 5, 2015 at 11:01:31 PM UTC-5, Alfonso Serra wrote:
Thanks Anthony. Ill read more about how the ticket system behaves if the request is not local.

Tickets are the same regardless of whether the request is local or not (no distinction between development and production). Don't raise an error -- return a response.

Anthony
Reply all
Reply to author
Forward
0 new messages