如何在Flask/Connexion中获取呼叫者的IP地址? [英] How to obtain the caller's IP address in Flask/Connexion?

查看:91
本文介绍了如何在Flask/Connexion中获取呼叫者的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenAPI 3.0规范(swagger.yml),并使用Swagger Codegen创建相应的Python Flask应用程序存根.这是我运行应用程序以公开Swagger API的方式:

I'm using an OpenAPI 3.0 specification (swagger.yml) and use Swagger Codegen to create the corresponding Python Flask application stubs. This is how I run the application to expose my Swagger API:

app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('swagger.yaml', arguments={'title': 'My Test API'})
# add CORS support to send Access-Control-Allow-Origin header
CORS(app.app)

到目前为止,一切都很好.应用程序逻辑在生成的Python存根中处理,这些存根由 x-openapi-router-controller:swagger_server.controllers.user_controller 链接.

So far so good. The application logic is handled within the generated Python stubs which are linked by the x-openapi-router-controller: swagger_server.controllers.user_controller.

但是,我现在需要访问应用程序内的 HTTP请求特定信息本身,例如根据 HTTP_CLIENT_IP地址

I now however need to access HTTP Request specific information within the application itself to for example react differently based on the HTTP_CLIENT_IP address

如何在控制器端点内获取该信息?

How can I obtain that information within my controller endpoint?

推荐答案

请求标头参数,以及连接不如何将它们转发到自定义控制器.我最终通过

Attached two related links addressing the same issue on request header parameters and how connexion does not forward them to custom controllers. I ended up manually accessing them via

access_token = connexion.request.headers['access_token']

这篇关于如何在Flask/Connexion中获取呼叫者的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆