我从 swagger-codegen 生成了 python 服务器 stub ,地址为 swagger editor在线,我想在本地运行生成的 python 服务器 stub ,但它不起作用,并且端点给了我 Not Found
错误。我调查了 swagger 社区,没有找到与此相关的任何问题。基本上,我在 yaml 文件中使用 openapi 规范来生成 python 服务器 stub ,但生成的服务器 stub 无法在本地运行。我不知道发生了什么事。有没有人对这个问题有经验?从 swagger-codegen 生成可以在本地运行和测试的 python 服务器 stub 的正确方法是什么?有什么想法吗?
openapi规范文件
这是 yaml 文件的开始方式:
openapi: 3.0.2
info:
title: api
description: test api
contact:
name: python server
url: /api/v1/
email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9df4f3fbf2ddfaf0fcf4f1b3fef2f0" rel="noreferrer noopener nofollow">[email protected]</a>
version: 1.0.0
servers:
- url: /api/v1/
description: SwaggerHub API Auto Mocking
- url: /api/v1/
description: IMM AWS Server 1
我使用此 yaml 文件生成了 python 服务器 stub ,但生成的 python 服务器 stub 无法正常工作,并且无法从 python 控制台/cmd 在本地运行。有什么想法可以让这项工作成功吗?有什么可能的想法吗?
更新
这是我尝试过的:
venv) C:\Users\kim\photoalbum_api>python -m swagger_server
The swagger_ui directory could not be found.
Please install connexion with extra install: pip install connexion[swagger-ui]
or provide the path to your local installation by passing swagger_path=<your path>
The swagger_ui directory could not be found.
Please install connexion with extra install: pip install connexion[swagger-ui]
or provide the path to your local installation by passing swagger_path=<your path>
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
所以我做了pip install connexion[swagger-ui]
并尝试在本地运行它,但端点总是给我这个:
为了可重现的目的,我也测试了这个openapi spec file生成 python 服务器 stub ,但它不起作用。为什么?有什么想法可以让这项工作成功吗?谢谢
新更新:
即使我尝试使用http://localhost:8080/
,端点总是给我这个:
(venv) C:\Users\kim\photouser_api>python -m swagger_server
* Serving Flask app "__main__" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 198-543-983
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
127.0.0.1 - - [16/May/2020 12:13:51] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [16/May/2020 12:14:02] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [16/May/2020 12:14:05] "GET /api HTTP/1.1" 404 -
127.0.0.1 - - [16/May/2020 12:14:10] "GET /api/v1 HTTP/1.1" 404 -
127.0.0.1 - - [16/May/2020 12:14:14] "GET /api/v1/ HTTP/1.1" 404 -
127.0.0.1 - - [16/May/2020 12:15:14] "GET / HTTP/1.1" 404 -
最佳答案
为了让它在我的机器上工作,我克隆了你的存储库 openapi.yaml 。然后我将 openapi.yaml 中的 url 修改为/api/v1/,并将 requirements.txt 中的连接更改为 2.6.0 。我创建了 virtualenv,并按照 README.md 中的说明进行操作。安装requirements.txt并运行它。在浏览器中,我输入 http://localhost:8080/api/v1/ui这应该显示 swagger ui。我还尝试了 docker build 指令,这也有效。
关于从 swagger-codegen 生成的 python 服务器 stub 根本无法在本地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61839498/