moodh moodh - 1 year ago 103
Python Question

Get IP Address when testing flask application through nosetests

My application depends on

which is
when i run tests through nosetests which uses
app.test_client().post('/users/login', ....)

How can I emulate an IP ( works fine) when I run tests?
I've tried setting environment variables, sent in headers with the post() method and I've digged through nosetests, werkzeugs and flasks documentation but nothing I've tried has worked.

Answer Source

A friend gave me this solution, which works across all requests:

class myProxyHack(object):

    def __init__(self, app): = app

    def __call__(self, environ, start_response):
        environ['REMOTE_ADDR'] = environ.get('REMOTE_ADDR', '')
        return, start_response)

app.wsgi_app = myProxyHack(app.wsgi_app)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download