Skip to main content
 首页 » 编程设计

python之使用 connexion+Flask+Swagger 的 python 微服务出错

2025年05月04日15kenshinobiy

我正在尝试用 python 制作微服务,我正在关注 this tutorial

但是我收到了这个错误:

"flask_app.py", line 115, in run 
    raise Exception('Server {} not recognized'.format(self.server)) 
Exception: Server 9090 not recognized 

项目结构:

App.py文件代码

from connexion.resolver import RestyResolver 
import connexion 
 
if __name__ == '__main__': 
    app = connexion.App(__name__, 9090, specification_dir='swagger/') 
    app.add_api('my_super_app.yaml', resolver=RestyResolver('api')) 
    app.run() 

my_super_app.yaml文件代码

swagger: "2.0" 
 
info: 
  title: "My first API" 
  version: "1.0" 
 
basePath: /v1.0 
 
paths: 
  /items/: 
    get: 
      responses: 
        '200': 
          description: 'Fetch a list of items' 
          schema: 
            type: array 
            items: 
              $ref: '#/definitions/Item' 
 
definitions: 
  Item: 
    type: object 
    properties: 
      id: 
        type: integer 
        format: int64 
      name: { type: string } 

items.py文件代码

items = { 
    0: {"name": "First item"} 
} 
 
 
def search() -> list: 
    return items 

请您参考如下方法:

好的...我能够解决这个问题...问题出在 app.py 中,您必须指定变量端口:

不正确

app = connexion.App(__name__, 9090, specification_dir='swagger/') 

正确

app = connexion.App(__name__, port=9090, specification_dir='swagger/')