在
https://editor.swagger.io/
上您可以粘贴一些json/yaml。 我正在将此作为 JSON 进行测试(不要转换为 YAML):
{
"swagger": "2.0",
"info": {
"version": "1.0",
"title": "Our first generated REST API",
"description": "<h2>This is a sample server code the is generated from Swagger Documenation with Swagger Editor</h2>"
},
"paths": {
"/greetings": {
"get": {
"summary": "Returns a list of Greetings",
"tags": ["Hello in Different Languages"],
"description": "Returns greetings in different languages",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
}
}
在顶部面板上,您可以选择生成服务器。 我需要选择 python-flask。 这将导出为 .zip,在本地提取,然后我参考 README.md 文件。
README.md 中的这些命令会导致错误:
pip3 install -r requirements.txt
python3 -m swagger_server
err0r 位于底部:
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-serve
r$ docker rmi -f mynewserver
Untagged: mynewserver:latest
Deleted: sha256:c6af85f05b4058fb96aedf06d5c957bbe1df71c21bcb76e3ec420afd4fbbaa2c
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-serve
r$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-serve
r$ pip3 install -r requirements.txt
Defaulting to user installation because normal site-packages is not writeable
Collecting connexion>=2.6.0
Downloading connexion-3.1.0-py3-none-any.whl (113 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 113.1/113.1 KB 5.2 MB/s eta 0:00:00
Collecting python_dateutil==2.6.0
Downloading python_dateutil-2.6.0-py2.py3-none-any.whl (194 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 194.4/194.4 KB 26.1 MB/s eta 0:00:00
Requirement already satisfied: setuptools>=21.0.0 in /usr/lib/python3/dist-packages (from -r requirements.txt (line 4)) (59.6.0)
Collecting swagger-ui-bundle>=0.0.2
Downloading swagger_ui_bundle-1.1.0-py3-none-any.whl (2.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.6/2.6 MB 70.6 MB/s eta 0:00:00
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python_dateutil==2.6.0->-r requirements.txt (line 3)) (1.16.0)
Collecting starlette>=0.35
Downloading starlette-0.38.2-py3-none-any.whl (72 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 72.0/72.0 KB 15.7 MB/s eta 0:00:00
Collecting Jinja2>=3.0.0
Downloading jinja2-3.1.4-py3-none-any.whl (133 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.3/133.3 KB 22.1 MB/s eta 0:00:00
Collecting python-multipart>=0.0.5
Downloading python_multipart-0.0.9-py3-none-any.whl (22 kB)
Collecting inflection>=0.3.1
Downloading inflection-0.5.1-py2.py3-none-any.whl (9.5 kB)
Requirement already satisfied: PyYAML>=5.1 in /usr/lib/python3/dist-packages (from connexion>=2.6.0->-r requirements.txt (line 1)) (5.4.1)
Collecting jsonschema>=4.17.3
Downloading jsonschema-4.23.0-py3-none-any.whl (88 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 88.5/88.5 KB 20.0 MB/s eta 0:00:00
Collecting werkzeug>=2.2.1
Downloading werkzeug-3.0.3-py3-none-any.whl (227 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 227.3/227.3 KB 43.6 MB/s eta 0:00:00
Collecting asgiref>=3.4
Downloading asgiref-3.8.1-py3-none-any.whl (23 kB)
Collecting httpx>=0.23
Downloading httpx-0.27.0-py3-none-any.whl (75 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 75.6/75.6 KB 14.1 MB/s eta 0:00:00
Collecting typing-extensions>=4.6.1
Downloading typing_extensions-4.12.2-py3-none-any.whl (37 kB)
Collecting requests>=2.27
Downloading requests-2.32.3-py3-none-any.whl (64 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.9/64.9 KB 13.4 MB/s eta 0:00:00
Collecting anyio
Downloading anyio-4.4.0-py3-none-any.whl (86 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 86.8/86.8 KB 16.0 MB/s eta 0:00:00
Collecting httpcore==1.*
Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 77.9/77.9 KB 12.8 MB/s eta 0:00:00
Collecting sniffio
Downloading sniffio-1.3.1-py3-none-any.whl (10 kB)
Collecting certifi
Downloading certifi-2024.7.4-py3-none-any.whl (162 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 163.0/163.0 KB 25.9 MB/s eta 0:00:00
Collecting idna
Downloading idna-3.7-py3-none-any.whl (66 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 66.8/66.8 KB 13.3 MB/s eta 0:00:00
Collecting h11<0.15,>=0.13
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.3/58.3 KB 11.4 MB/s eta 0:00:00
Collecting MarkupSafe>=2.0
Downloading MarkupSafe-2.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Collecting referencing>=0.28.4
Downloading referencing-0.35.1-py3-none-any.whl (26 kB)
Collecting rpds-py>=0.7.1
Downloading rpds_py-0.19.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (355 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 355.6/355.6 KB 50.9 MB/s eta 0:00:00
Collecting attrs>=22.2.0
Downloading attrs-23.2.0-py3-none-any.whl (60 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 60.8/60.8 KB 14.6 MB/s eta 0:00:00
Collecting jsonschema-specifications>=2023.03.6
Downloading jsonschema_specifications-2023.12.1-py3-none-any.whl (18 kB)
Collecting urllib3<3,>=1.21.1
Downloading urllib3-2.2.2-py3-none-any.whl (121 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.4/121.4 KB 21.5 MB/s eta 0:00:00
Collecting charset-normalizer<4,>=2
Downloading charset_normalizer-3.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (142 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 142.1/142.1 KB 21.0 MB/s eta 0:00:00
Collecting exceptiongroup>=1.0.2
Downloading exceptiongroup-1.2.2-py3-none-any.whl (16 kB)
Installing collected packages: urllib3, typing-extensions, sniffio, rpds-py, python-multipart, python_dateutil, MarkupSafe, inflection, idna, h11, exceptiongroup, charset-normalizer, certifi, attrs, werkzeug, requests, referencing, Jinja2, httpcore, asgiref, anyio, swagger-ui-bundle, starlette, jsonschema-specifications, httpx, jsonschema, connexion
Successfully installed Jinja2-3.1.4 MarkupSafe-2.1.5 anyio-4.4.0 asgiref-3.8.1 attrs-23.2.0 certifi-2024.7.4 charset-normalizer-3.3.2 connexion-3.1.0 exceptiongroup-1.2.2 h11-0.14.0 httpcore-1.0.5 httpx-0.27.0 idna-3.7 inflection-0.5.1 jsonschema-4.23.0 jsonschema-specifications-2023.12.1 python-multipart-0.0.9 python_dateutil-2.6.0 referencing-0.35.1 requests-2.32.3 rpds-py-0.19.1 sniffio-1.3.1 starlette-0.38.2 swagger-ui-bundle-1.1.0 typing-extensions-4.12.2 urllib3-2.2.2 werkzeug-3.0.3
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-serve
r$ python3 -m swagger_server
Traceback (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/home/project/python-flask-server-generated/python-flask-server/swagger_server/__main__.py", line 5, in <module>
from swagger_server import encoder
File "/home/project/python-flask-server-generated/python-flask-server/swagger_server/encoder.py", line 1, in <module>
from connexion.apps.flask_app import FlaskJSONEncoder
ModuleNotFoundError: No module named 'connexion.apps.flask_app'
此外。 .. 使用 docker 运行,按照 README.md 文件如下所示:
# building the image
docker build -t swagger_server .
# starting up a container
docker run -p 8080:8080 swagger_server`
错误:
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-serve
r$ docker build -t swagger_server .
[+] Building 0.6s (12/12) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 35B 0.0s
=> [internal] load metadata for docker.io/library/python:3-alpine 0.1s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [1/6] FROM docker.io/library/python:3-alpine@sha256:7f15e22f496c65cffbbac5e30e7e98 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 3.15kB 0.0s
=> CACHED [2/6] RUN mkdir -p /usr/src/app 0.0s
=> CACHED [3/6] WORKDIR /usr/src/app 0.0s
=> CACHED [4/6] COPY requirements.txt /usr/src/app/ 0.0s
=> CACHED [5/6] RUN pip3 install --no-cache-dir -r requirements.txt 0.0s
=> [6/6] COPY . /usr/src/app 0.0s
=> exporting to image 0.4s
=> => exporting layers 0.4s
=> => writing image sha256:abade704fc9c4525b2cf89efeecff328d69d78c51c3705d186c7d7dbf3 0.0s
=> => naming to docker.io/library/swagger_server 0.0s
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-serve
r$ docker run -dp 8080:8080 swagger_server
7ba8497e3fd0f239b51ce540167c3ab120311ab5b520cb2434cc10bd65c0d83e
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-serve
r$ curl localhost:8080/greetings
curl: (7) Failed to connect to localhost port 8080 after 0 ms: Connection refused
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-serve
r$ curl localhost:8080/
curl: (7) Failed to connect to localhost port 8080 after 0 ms: Connection refused
theia@theiadocker-peterburger:/home/project$ ls python-flask-server-generated.zipflask-serve
ls: ca
我真的不知道要尝试什么。
docker images
docker rmi -f <IMAGE ID>
然后再试一次..?
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-server$ docker container ls --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ba8497e3fd0 swagger_server "python3 -m swagger_…" 10 minutes ago Exited (1) 10 minutes ago reverent_napier
ec7e894421fc c6af85f05b40 "python3 -m swagger_…" 21 minutes ago Exited (1) 21 minutes ago elastic_goldstine
theia@theiadocker-peterburger:/home/project/python-flask-server-generated/python-flask-server$ docker logs 7ba8497e3fd0
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/usr/src/app/swagger_server/__main__.py", line 5, in <module>
from swagger_server import encoder
File "/usr/src/app/swagger_server/encoder.py", line 1, in <module>
from connexion.apps.flask_app import FlaskJSONEncoder
ModuleNotFoundError: No module named 'connexion.apps.flask_app'
The issue you're encountering stems from an incompatibility between the version of Connexion used in the generated Flask server code and the structure of the Connexion library. Here's a breakdown and how to fix it:
Problem:
The generated code expects
FlaskJSONEncoder
to be located at
connexion.apps.flask_app
. However, in more recent Connexion versions (likely the one you have installed), this has been moved to
connexion.apps.flask.flask_app
.
Solution:
-
Modify
encoder.py
: -
Open the file
swagger_server/encoder.py
located within your generated project folder. -
Change the import statement from:
python from connexion.apps.flask_app import FlaskJSONEncoder
to:
python from connexion.apps.flask.flask_app import FlaskJSONEncoder
-
Rebuild and Run (Docker):
-
If you're using Docker, rebuild your image to incorporate this change:
bash docker build -t swagger_server .
-
Then, run the container:
bash docker run -p 8080:8080 swagger_server
-
Run Directly (Without Docker):
-
If you're running the server directly, simply start it again:
bash python3 -m swagger_server
Explanation:
- Connexion Update: The structure of the Connexion library likely changed in an update, leading to this import issue.
-
Path Correction:
By modifying the import path in
encoder.py
, you're directing your code to the correct location of theFlaskJSONEncoder
class.
After making these adjustments, your server should start correctly, and you should be able to access your API endpoints.
标签:python,docker,flask,swagger From: 78804801