端口 XXXX 正被另一个程序使用。识别并停止该程序,或者使用不同的端口启动服务器

Bri*_* P. 7 google-app-engine

我想运行一个 python Flask hello world。我部署到 App Engine,但它显示端口正在使用中,并且看起来它正在多个实例/线程/克隆上同时运行。

这是我的 main.py

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def helloIndex():
    print("Hello world log console")
    return 'Hello World from Python Flask!'

app.run(host='0.0.0.0', port=4444)
Run Code Online (Sandbox Code Playgroud)

这是我的 app.yaml

runtime: python38
env: standard
instance_class: B2
handlers:
  - url: /
    script: auto
  - url: .*
    script: auto
manual_scaling:
  instances: 1
Run Code Online (Sandbox Code Playgroud)

这是我的要求.txt

gunicorn==20.1.0
flask==2.2.2
Run Code Online (Sandbox Code Playgroud)

这是我得到的日志:

* Serving Flask app 'main'
* Debug mode: off
Address already in use
Port 4444 is in use by another program. Either identify and stop that program, or start the server with a different port.
[2022-08-10 15:57:28 +0000] [1058] [INFO] Worker exiting (pid: 1058)
[2022-08-10 15:57:29 +0000] [1059] [INFO] Booting worker with pid: 1059
[2022-08-10 15:57:29 +0000] [1060] [INFO] Booting worker with pid: 1060
[2022-08-10 15:57:29 +0000] [1061] [INFO] Booting worker with pid: 1061
Run Code Online (Sandbox Code Playgroud)

它说端口 4444 正在使用中。最初我尝试了 5000(flask 的默认端口),但它说它正在使用中。我也尝试删除port=4444但现在它说Port 5000 is in use by another program,我猜烧瓶默认分配端口= 5000。我怀疑是因为 GAE 在多个实例中运行导致了此错误。如果没有,那么请帮助解决这个问题。

Was*_*tab 16

我解决了使用lsofkill命令。lsof -i:5000就我而言,错误消息提到了端口 5000,因此我首先通过调用终端来检查哪些进程正在使用端口 5000 。一旦我找到了PID已经运行的Flask应用程序,我就使用以下命令杀死了它:kill -9 <PID>voil\xc3\xa0!

\n

这种方法应该适用于 Mac 和 Linux 用户。lsof如果 Windows 用户可以找到与 和Linux 命令等效的kill命令,那么它也可以在那里工作。

\n


Fer*_*ona 1

App Engine 应用程序应侦听端口 8080,而不是任何其他端口。

所以你可能需要这样设置

app.run(host='0.0.0.0', port=8080)
Run Code Online (Sandbox Code Playgroud)