Socket error: Address already in use
Socket error: Address already in use
我有一个经常 运行 启动服务器的 CherryPy 脚本。今天我不得不启动和停止它几次以修复配置文件中的一些错误,我猜套接字并没有完全关闭,因为当我再次尝试启动它时我遇到了这个问题:
[23/Mar/2015:14:08:00] ENGINE Listening for SIGHUP.
[23/Mar/2015:14:08:00] ENGINE Listening for SIGTERM.
[23/Mar/2015:14:08:00] ENGINE Listening for SIGUSR1.
[23/Mar/2015:14:08:00] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[23/Mar/2015:14:08:00] ENGINE Started monitor thread 'Autoreloader'.
[23/Mar/2015:14:08:00] ENGINE Started monitor thread '_TimeoutMonitor'.
[23/Mar/2015:14:08:00] ENGINE Error in HTTP server: shutting down
Traceback (most recent call last):
File "/home/andrew/virtualenvs/mikernels/lib/python2.7/site-packages/cherrypy/process/servers.py", line 188, in _start_http_thread
self.httpserver.start()
File "/home/andrew/virtualenvs/mikernels/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 1848, in start
raise socket.error(msg)
error: No socket could be created
我编辑了 CherryPy 的 wsgiserver2.py 以查看 socket.error 和 error.strerror
的详细信息
98 (98, 'Address already in use') Address already in use
同时我的套接字构造为:
af = 2
socktype = 1
proto = 6
canonname = ''
sa = ('0.0.0.0', 2112)
self.bind(af, socktype, proto)
(这不是确切的代码,但这是触发错误时的值)
我检查了 netstat 并没有看到任何在端口 2112 上侦听的东西,可能是什么导致了问题,我该如何诊断它?
谢谢!
您可以尝试以下方法
from socket import *
sock=socket()
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# then bind
来自文档:
The SO_REUSEADDR flag tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire.
完整的解释如下:
Running an example several times with too small delay between executions, could lead to this error:
socket.error: [Errno 98] Address already in use
This is because the previous execution has left the socket in a TIME_WAIT state, and can’t be immediately reused.
There is a socket flag to set, in order to prevent this, socket.SO_REUSEADDR:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
您可以通过以下操作找到进程并kill
它:
ps aux | grep python
,查找进程 ID,并通过执行以下操作手动停止它:
sudo kill -9 PID
用您的 PID 替换 PID。
在使用 Flask/CherryPy 进行测试时,我经常不得不这样做。有兴趣看看是否有更简单的方法(例如首先防止它)
通过以下方式更容易做到:
检查 PID(:5000 是主机,因为我在 127.0.0.1:5000 运行):
$ lsof -i :5000
然后杀掉它:
$ sudo kill -9 PID
我有一个经常 运行 启动服务器的 CherryPy 脚本。今天我不得不启动和停止它几次以修复配置文件中的一些错误,我猜套接字并没有完全关闭,因为当我再次尝试启动它时我遇到了这个问题:
[23/Mar/2015:14:08:00] ENGINE Listening for SIGHUP.
[23/Mar/2015:14:08:00] ENGINE Listening for SIGTERM.
[23/Mar/2015:14:08:00] ENGINE Listening for SIGUSR1.
[23/Mar/2015:14:08:00] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[23/Mar/2015:14:08:00] ENGINE Started monitor thread 'Autoreloader'.
[23/Mar/2015:14:08:00] ENGINE Started monitor thread '_TimeoutMonitor'.
[23/Mar/2015:14:08:00] ENGINE Error in HTTP server: shutting down
Traceback (most recent call last):
File "/home/andrew/virtualenvs/mikernels/lib/python2.7/site-packages/cherrypy/process/servers.py", line 188, in _start_http_thread
self.httpserver.start()
File "/home/andrew/virtualenvs/mikernels/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 1848, in start
raise socket.error(msg)
error: No socket could be created
我编辑了 CherryPy 的 wsgiserver2.py 以查看 socket.error 和 error.strerror
的详细信息
98 (98, 'Address already in use') Address already in use
同时我的套接字构造为:
af = 2
socktype = 1
proto = 6
canonname = ''
sa = ('0.0.0.0', 2112)
self.bind(af, socktype, proto)
(这不是确切的代码,但这是触发错误时的值)
我检查了 netstat 并没有看到任何在端口 2112 上侦听的东西,可能是什么导致了问题,我该如何诊断它?
谢谢!
您可以尝试以下方法
from socket import *
sock=socket()
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# then bind
来自文档:
The SO_REUSEADDR flag tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire.
完整的解释如下:
Running an example several times with too small delay between executions, could lead to this error:
socket.error: [Errno 98] Address already in use
This is because the previous execution has left the socket in a TIME_WAIT state, and can’t be immediately reused.
There is a socket flag to set, in order to prevent this, socket.SO_REUSEADDR:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((HOST, PORT))
您可以通过以下操作找到进程并kill
它:
ps aux | grep python
,查找进程 ID,并通过执行以下操作手动停止它:
sudo kill -9 PID
用您的 PID 替换 PID。
在使用 Flask/CherryPy 进行测试时,我经常不得不这样做。有兴趣看看是否有更简单的方法(例如首先防止它)
通过以下方式更容易做到:
检查 PID(:5000 是主机,因为我在 127.0.0.1:5000 运行):$ lsof -i :5000
然后杀掉它:$ sudo kill -9 PID