套接字服务器的客户端抛出的错误不是说操作试图在非s的对象上进行

2024-05-15 00:32:12 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我正在学习的套接字服务器的客户端部分,我遇到了这个错误。我搜索了错误,找到了几篇文章,但它们似乎与我的问题无关

Traceback (most recent call last):
  File "C:/Users/Sam/PycharmProjects/prog0/client.py", line 13, in <module>
    socketRead, socketWrite, socketError = select.select(sockets, [], [])
OSError: [WinError 10038] An operation was attempted on something that is not a socket

有谁能帮我解释一下问题出在哪里,因为我真的不知道。发生此错误之前,客户端已连接到服务器

import select
import socket
import sys

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP_ADDRESS = '127.0.0.1'
PORT = 12000

server.connect((IP_ADDRESS, PORT))

while True:
    sockets = [sys.stdin, server]
    socketRead, socketWrite, socketError = select.select(sockets, [], [])

    for s in socketRead:
        if s == server:
            msg = s.recv(1024)
            print(msg)
        else:
            msg = sys.stdin.readline()
            server.send(msg)
            sys.stdout.write("<You>")
            sys.stdout.write(msg)
            sys.stdout.flush()

server.close()

Tags: inimport服务器客户端server错误stdoutsys
1条回答
网友
1楼 · 发布于 2024-05-15 00:32:12

这将在*nix系统上工作,但不能在windows上的select中使用普通文件的描述符:请参阅https://docs.python.org/3.5/library/select.html中的文档说明:

Note File objects on Windows are not acceptable, but sockets are. On Windows, the underlying select() function is provided by the WinSock library, and does not handle file descriptors that don’t originate from WinSock.

相关问题 更多 >

    热门问题