使用Connexion+Flask+Swagger开发Python微服务遇服务器识别错误
Hey there, let's fix that annoying error you're seeing. The problem comes from how you're passing the port number when initializing your Connexion app.
Looking at your App.py code:
app = connexion.App(__name__, 9090, specification_dir='swagger/')
You're passing 9090 as the second positional argument, but the second parameter for connexion.App is actually specification_dir (which expects a string, not an integer). This misalignment is causing the app to misinterpret the port value as an unrecognized server identifier when you call app.run().
修复方案
There are two simple ways to fix this:
方法1:在connexion.App构造时使用port关键字参数
from connexion.resolver import RestyResolver import connexion if __name__ == '__main__': # 把port作为关键字参数传递,避免参数位置混淆 app = connexion.App(__name__, specification_dir='swagger/', port=9090) app.add_api('my_super_app.yaml', resolver=RestyResolver('api')) app.run()
方法2:在app.run()中直接指定端口
from connexion.resolver import RestyResolver import connexion if __name__ == '__main__': app = connexion.App(__name__, specification_dir='swagger/') app.add_api('my_super_app.yaml', resolver=RestyResolver('api')) # 在run方法里明确指定端口 app.run(port=9090)
Either approach will properly tell Connexion to run your service on port 9090 without that "not recognized" error. Your Swagger spec and items.py code look fine, so this should get things up and running smoothly.
内容的提问来源于stack exchange,提问作者Fernando Muñoz

