首页 > 编程问答 >Swagger、Docker、Python-Flask: : https://editor.swagger.io/ 生成服务器 python-flask erorr

Swagger、Docker、Python-Flask: : https://editor.swagger.io/ 生成服务器 python-flask erorr

时间:2024-07-29 04:58:39浏览次数:10  
标签:python docker flask swagger

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:

  1. Modify encoder.py :

  2. Open the file swagger_server/encoder.py located within your generated project folder.

  3. Change the import statement from:

    python from connexion.apps.flask_app import FlaskJSONEncoder

    to:

    python from connexion.apps.flask.flask_app import FlaskJSONEncoder

  4. Rebuild and Run (Docker):

  5. If you're using Docker, rebuild your image to incorporate this change:

    bash docker build -t swagger_server .

  6. Then, run the container:

    bash docker run -p 8080:8080 swagger_server

  7. Run Directly (Without Docker):

  8. 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 the FlaskJSONEncoder 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

相关文章

  • Flask 是否有使用 Sqlalchemy 的动态查询生成器?
    一个简单的查询如下所示User.query.filter(User.name=='admin')在我的代码中,我需要检查正在传递的参数,然后根据参数过滤数据库中的结果。例如,如果User表包含像username这样的列、位置和电子邮件,请求参数可以包含其中之一,也可以包含列的组合。我不想像下面这样检......
  • 使用 Matplotlib 的 Python 代码中出现意外的控制流
    Ubuntu22.04上的此Python3.12代码的行为符合预期,除非我按q或ESC键退出。代码如下:importnumpyasnp,matplotlib.pyplotaspltfrompathlibimportPathfromcollectionsimportnamedtuplefromskimage.ioimportimreadfrommatplotlib.widgets......
  • 参考 - Python 类型提示
    这是什么?这是与在Python中使用类型提示主题相关的问题和答案的集合。这个问题本身就是一个社区维基;欢迎大家参与维护。这是为什么?Python类型提示是一个不断增长的话题,因此许多(可能的)新问题已经被提出,其中许多甚至已经有了答案。该集合有助于查找现有内容。范......
  • 我的 Python 程序中解决 UVa 860 的运行时错误 - 熵文本分析器
    我正在尝试为UVa860编写一个解决方案,但是当我通过vJudge发送它时,它一直显示“运行时错误”。fromsysimportstdinimportmathdefmain():end_of_input=Falselambda_words=0dictionary={}text_entropy=0relative_entropy=0whilenotend_of_in......
  • Python进度条
    当我的脚本正在执行某些可能需要时间的任务时,如何使用进度条?例如,一个需要一些时间才能完成并在完成后返回True的函数。如何在函数执行期间显示进度条?请注意,我需要实时显示进度条,所以我不知道该怎么办。我需要thread为此吗?我不知道。现在在执行函数......
  • 此 Python 代码给出了超出时间限制的错误。由于其中使用的输入或输出方法而在其中传递
    N=int(input())L1=input()L=L1.split()s=set(L)d={}foreins:d[e]=L.count(e)print(d)max_value=max(d.values())print(max_value)L=list(d.values())print(L)res=L.count(max_value)print(res)/在提供正常输入时,它运行良好,但在提......
  • @staticmethod 在 Python 中意味着什么?
    我正在使用Python学习OOP。我想知道@staticmethod在OOP中到底做了什么。为什么我应该/不应该使用它?classCar:@staticmethoddefstart():print("carstarted")defstop():print("carstopped")当然,让我们来分解一下Pyth......
  • 如何在Anaconda基础环境中更新Python?
    如何在Anaconda基础环境中更新Python?Python是否打算在基础环境中进行升级,还是应该完全删除并重新安装Anaconda?任何版本最终都会失去支持,所以应该有一些解决方案。到目前为止我已经尝试过,并没有导致Python更新:condaupdate--allcondaupdate......
  • 为什么我的 HTTP 请求代码需要在 PowerShell 上运行一次才能在 Python 上正常工作?
    下面的代码只是一个演示,我有一个http函数来获取新的'log-uid'和'ASP.NET_SessionId',然后将它们放入cookie中,但是如果我直接运行python代码,响应是'{data:不登录}'cookies={'log-uid':'**newlog-uidexample**','ASP.NET_SessionId':......
  • 在 Windows 上的 python 中更改特定应用程序的音频
    我尝试制作一个可以分割音频的程序,我面临的问题之一是如何更改一个特定应用程序的音频输出。operafrompycaw.pycawimportAudioUtilities,IAudioEndpointVolumefromcomtypesimportCLSCTX_ALLimportpsutildefchange_audio_output(device_name):prog_name=i......