python web项目意外关闭,但占用端口的解决方法


问题描述

因为项目强制关闭,但是服务还在运行,导致重新运行项目时候 提示地址已经使用(端口被占用)

/usr/bin/python3.5 python-login-demo/index.py
http://0.0.0.0:8080/
Traceback (most recent call last):
  File "/var/www/git/mine/python-login-demo/index.py", line 64, in <module>
    application.run()
  File "/usr/local/lib/python3.5/dist-packages/web/application.py", line 341, in run
    return wsgi.runwsgi(self.wsgifunc(*middleware))
  File "/usr/local/lib/python3.5/dist-packages/web/wsgi.py", line 59, in runwsgi
    return httpserver.runsimple(func, server_addr)
  File "/usr/local/lib/python3.5/dist-packages/web/httpserver.py", line 177, in runsimple
    server.start()
  File "/usr/local/lib/python3.5/dist-packages/web/wsgiserver/wsgiserver3.py", line 1669, in start
    raise socket.error(msg)
OSError: No socket could be created -- (('0.0.0.0', 8080): [Errno 98] Address already in use)

解决方法

查找(lsof)该进程, 并结束(kill)该进程 即可.

$ lsof -i:8080                                                          130 ↵
COMMAND     PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3.5 31982 itaken    3u  IPv4 593966      0t0  TCP *:http-alt (LISTEN)

$ sudo kill 31982

Author: Itaken
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Itaken !
  TOC目录