Get IP Address when testing flask application through nosetests

前端 未结 3 1671
醉话见心
醉话见心 2020-12-31 03:01

My application depends on request.remote_addr which is None when i run tests through nosetests which uses app.test_client().post(\'/users/log

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 03:28

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

    class myProxyHack(object):
    
        def __init__(self, app):
            self.app = app
    
        def __call__(self, environ, start_response):
            environ['REMOTE_ADDR'] = environ.get('REMOTE_ADDR', '127.0.0.1')
            return self.app(environ, start_response)
    
    app.wsgi_app = myProxyHack(app.wsgi_app)
    
    app.test_client().post(...)
    

提交回复
热议问题