尝试运行 Flask 应用程序会显示“地址已在使用中" [英] Trying to run Flask app gives "Address already in use"

查看:21
本文介绍了尝试运行 Flask 应用程序会显示“地址已在使用中"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新了我的应用程序并尝试运行它,但收到以下有关地址已在使用中"的错误消息.这是什么意思,我该如何解决?

I recently updated my app and tried to run it, and got the following error about "Address already in use". What does this mean and how do I fix it?

Traceback (most recent call last):
  File "/home/ubuntu/workspace/app.py", line 11, in <module>
    app.run(host = os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT',8080)))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 687, in run_simple
    inner()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 653, in inner
    fd=fd).serve_forever()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 557, in make_server
    passthrough_errors, ssl_context, fd=fd)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 467, in __init__
    HTTPServer.__init__(self, (host, int(port)), handler)
  File "/usr/lib/python2.7/SocketServer.py", line 419, in __init__
    self.server_bind()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
    SocketServer.TCPServer.server_bind(self)
  File "/usr/lib/python2.7/SocketServer.py", line 430, in server_bind
    self.socket.bind(self.server_address)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

推荐答案

这意味着有另一个服务正在使用该端口(在本例中为 8080).也许是因为您忘记关闭另一个正在运行的 Flask 应用程序,而它正在使用 8080 端口.

It means there's another service's using that port (8080 in this case). Maybe because you forgot close another running Flask app and it's using 8080 port.

但是,您可以更改正在使用的端口,例如将其更改为 4444,如下所示:

However, you could change the port you're using, for example change it to 4444 like this:

if __name__=="__main__":
    app.run(host=os.getenv('IP', '0.0.0.0'), 
            port=int(os.getenv('PORT', 4444)))

但无论如何,如果不是您的程序,我想您想知道哪个程序正在使用该部分.您可以使用 nmapnetcat GNU 程序来检查它.

But anyways, I think you'd like to know which program is using that part if it's not your program. You could use nmap or netcat GNU program to check it.

这是 netcat 方式(从这里):

$ sudo netstat -nlp | grep 8080
tcp  0  0  0.0.0.0:8080  0.0.0.0:*  LISTEN  125004/nginx

当你得到它时,我建议手动停止它(例如如果它是 nginx 或其他 HTTP 服务器,然后通过 service 命令或 停止它systemctl(如果您使用的是 systemd Linux)

When you got it, I'd suggest stop it manually (for example if it's nginx or other HTTP servers, then stop it via service command or systemctl if you're using systemd Linux)

你也可以通过命令kill杀死它:

You can also kill it via command kill:

kill <pid>

您也可以通过 killallpkill 杀死它,它使用进程名称而不是 pid:

You can also kill it via killall or pkill, it use a process name instead of it's pid:

killall/pkill <process name>

这篇关于尝试运行 Flask 应用程序会显示“地址已在使用中"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆