Python 套接字错误“[Errno 9] Bad file descriptor”的原因可能是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17207051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What could be the reason for a socket error "[Errno 9] Bad file descriptor"
提问by Alex
I have a complex python (2.7.3) script which is trying to open a socket connection via
我有一个复杂的 python (2.7.3) 脚本,它试图通过
self.socket.close()
# doing something else
self.socket.connect((host, port))
but all I get is the following socket error:
但我得到的是以下套接字错误:
error: [Errno 9] Bad file descriptor
The host:portaccepts connections as I have verified this with nc host portmanually. So what could be the possible reasons I get this error for opening a connection to the given port, which actually works?
在host:port我已经验证了这一点与接受连接nc host port手动。那么我在打开与给定端口的连接时出现此错误的可能原因是什么?
I cannot and will not post the full script as it is too complex and irrelavent for this question. I just would like to know all possible reasons for this error, and how to check them and fix them.
我不能也不会发布完整的脚本,因为它对于这个问题来说太复杂和无关紧要了。我只想知道此错误的所有可能原因,以及如何检查和修复它们。
采纳答案by Atmaram Shetye
You will need to create a new socket object. Maybe self.socket = socket.socket()after closing the previous socket and before connecting.
您将需要创建一个新的套接字对象。也许self.socket = socket.socket()在关闭前一个套接字之后和连接之前。

