python - python中奇怪的选择错误

标签 python sockets python-sockets

好的,我有 Python 2.5 和 Windows XP。我将 select.select 与套接字对象一起使用。我一次又一次地尝试,但每当我运行它时,它所在的线程都会给我一个错误,如 select.error(9, "Bad file descriptor")。 代码是这样的:

import socket, select
s = socket.socket()
s.bind((socket.gethostbyname(socket.gethostname()), 1312))
s.listen(5)
inputs = [s]
outputs = []
while True:
    r, w, e = select.select(inputs, outputs, inputs)
    for sock in r:
        if sock is s:
            inputs.append(s.accept()[0])
        else:
            print s
            print s.recv(1024)

如有任何信息,我们将不胜感激。谢谢!

最佳答案

  1. 您调用了没有参数的 select.select。它应该类似于:select.select(inputs, outputs, [])

  2. else 中,您需要使用 sock,而不是 s(服务器)。

  3. 一旦对等点与先前连接的套接字断开连接,您应该将其从 inputs 列表中删除。如果 sock.recv() 返回空字符串或引发 socket.error 异常,您可以知道对等方已断开连接。如果不这样做,您最终可能会向 select.select 提供无效的套接字描述符,从而导致您提到的错误。

关于python - python中奇怪的选择错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769230/

相关文章:

c - 我的服务器在c中不接受为什么?

python ftp 服务器显示 "150 File status okay. About to open data connection."并且什么都不做

php - 如何在python中对onvif进行密码加密?

python - 流水线操作时 Redis 中的 MemoryError

python - OpenCV 错误 : Assertion failed (size. 宽度>0 && size.height>0) python

Java套接字编程: How to send <STX>string-data<ETX> over TCP connection to the server?

Python - 如何关闭打开的端口?

python - SOCK_STREAM 的作用是什么?

python - Pycharm Django Debugging 真的很慢

python - 在 Pweave 模板中使用全局变量