gpt4 book ai didi

python - 套接字服务器的客户端不会抛出错误,表明尝试对非套接字的内容进行操作

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:31 25 4
gpt4 key购买 nike

我正在学习制作的套接字服务器的客户端部分收到此错误。我已经搜索了该错误,并找到了几篇文章,但它们似乎与我的问题无关。

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

最佳答案

这将在 *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.

关于python - 套接字服务器的客户端不会抛出错误,表明尝试对非套接字的内容进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53086478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com