试图运行Flask应用程序给出“地址已经在使用” [英] Trying to run Flask app gives "Address already in use"

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

问题描述

我最近更新了我的应用程序,并尝试运行它,并得到了有关地址已被使用的以下错误。这是什么意思,我该如何解决它?

 追溯(最近的最后一次调用):
文件/ home / ubuntu / workspace / app.py,第11行,在< module>
app.run(host = os.getenv('IP','0.0.0.0'),port = int(os.getenv('PORT',8080)))
文件/ usr / local / lib / python2.7 / dist-packages / flask / app.py,第772行,运行
run_simple(host,port,self,** options)
文件/ usr / local /lib/python2.7/dist-packages/werkzeug/serving.py,第687行,在run_simple中
inner()
文件/usr/local/lib/python2.7/dist-packages /werkzeug/serving.py,第653行,内部
fd = fd).serve_forever()
文件/usr/local/lib/python2.7/dist-packages/werkzeug/serving。 py,第557行,在make_server
passthrough_errors,ssl_context,fd = fd)
文件/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py,第467行,在__init__
HTTPServer .__ init __(self,(host,int(port)),handler)
在__init__ $文件/usr/lib/python2.7/SocketServer.py,行419 b $ b self.server_bind()
文件/usr/lib/python2.7/BaseHTTPServer.py,第108行,在server_bind中
SocketServer.TCPServer.server_bind(s elf)
文件/usr/lib/python2.7/SocketServer.py,第430行,在server_bind中
self.socket.bind(self.server_address)
文件/ usr / (self._sock,name)(* args)
socket.error:[Errno 98]地址已经在使用了$ lib $ python2.7 / socket.py,第224行,在meth
返回getattr b $ b


解决方案

这意味着有另一个服务使用该端口( 8080 在这种情况下)。也许是因为你忘了关闭另一个正在运行的Flask应用程序,它使用 8080 port。



但是,你正在使用,例如把它改为 4444 就像这样:

  if __name __ ==__ main__:
app.run(host = os.getenv('IP','0.0.0.0'),
port = int(os.getenv('PORT', 4444)))

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



以下是 netcat 方法(

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



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

您也可以杀死它通过命令 kill

  kill< pid> 

您也可以通过 killall pkill ,它使用进程名称而不是它的pid:

$ $ p $ killall / pkill<进程名称>


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

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.

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)))

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.

Here's the netcat way (from here):

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

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)

You can also kill it via command kill:

kill <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天全站免登陆