Skip to content Skip to sidebar Skip to footer

How To Filter Ip Addresses By Route In Bottle?

My bottle webserver provides several services (routes). Some of them must be restricted to private (RFC1918) IPs, while others serve both private and public ones. Right now I check

Solution 1:

I'm not aware of anything built in, but you could use a decorator to do that. Return a wrapper from the decorator, which performs the check for the IP type and returns the regular view if the check passes or an error-message (or similar) otherwise. The following should work:

defprivate_only(route):
    defwrapper(*args, **kwargs):
        if IPy.IP(bottle.request.remote_addr).iptype() == 'PRIVATE':
            return route(*args, **kwargs)
        else:
            return"Not allowed!"return wrapper

Then decorate your private views with it:

@route('/my/internal/route')
@private_only
def internal_view():
    return some_data()

Post a Comment for "How To Filter Ip Addresses By Route In Bottle?"