简体   繁体   中英

How do I globally redirect an ip address in Pyramid?

Is there a global way to redirect users w/ certain ip's in pyramid? Right now, we have to redirect on a view by view basis.

For instance, each view will return 'my custom message' for every page someone w/ ip address of 'xyx.xxx.xx' visits. If you don't have that ip address, then the page will load as normal.

@view_config(renderer="json", route_name="myview")
def myview(request):
    redirector(request)
    ......
def redirector(request):
    if request.remote_addr.startswith('66.'): return viewA(request)
    else: return 'my custom message'

Is there a way we could redirect globally in pyramid (other than through .htaccess)?

Thanks!

You can use Pyramid 's events system :

@subscriber('pyramid.events.NewRequest')
def newrequest(event):
    request = event.request
    if request.remote_addr.startswith('66.'):
        raise SomeException('my custom message')

@view_config(context=SomeException, renderer='string')
def exc_view(exc, request):
    return exc.message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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