套接字错误:地址已在使用中 [英] Socket error: Address already in use

查看:73
本文介绍了套接字错误:地址已在使用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CherryPy脚本,我经常运行该脚本来启动服务器.今天,我不得不启动和停止几次以修复配置文件中的一些错误,我想套接字并没有完全关闭,因为当我尝试再次启动它时,我遇到了这个问题:

I have a CherryPy script that I frequently run to start a server. Today I was having to start and stop it a few times to fix some bugs in a config file, and I guess the socket didn't close all the way because when I tried to start it up again I got this issue:

[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 的详细信息是

I edited CherryPy's wsgiserver2.py to see the details of the socket.error and error.strerror was

98  (98, 'Address already in use') Address already in use

同时我的套接字被构造为:

Meanwhile my socket is constructed as:

af = 2
socktype = 1
proto = 6
canonname = ''
sa = ('0.0.0.0', 2112)
self.bind(af, socktype, proto)

(这不是确切的代码,但是这是引发错误时的值)

(that's not exact code but that's what the values are when the error is fired)

我检查了netstat,在端口2112上没有侦听任何内容,这可能是导致问题的原因,我该如何诊断它呢?

I checked netstat and didn't see anything listening on port 2112, what could be causing the problem and how can I go about diagnosing it?

谢谢!

推荐答案

您可以尝试以下

from socket import *

sock=socket()
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
# then bind

从文档中

SO_REUSEADDR标志告诉内核重用处于TIME_WAIT状态的本地套接字,而不必等待其自然超时到期.

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]地址已在使用中

这是因为先前的执行使套接字处于TIME_WAIT状态,无法立即重用.

This is because the previous execution has left the socket in a TIME_WAIT state, and can’t be immediately reused.

为了防止这种情况,有一个套接字标志需要设置socket.SO_REUSEADDR:

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

这篇关于套接字错误:地址已在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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