好吧,在一些套接字和网络实验中,我设置了一个非常基本的聊天客户端/服务器,可以在 Unix 上完美运行。现在我在转换为 win32 时遇到了一些错误。在前言中,我了解到 Windows 上的 select.select() 不会接受套接字对象,并且(我认为)通过不向其传递套接字对象而是向其传递套接字编号来进行连贯补偿。但是,该脚本仍然卡在 select.select() 函数上,我不知道为什么。该脚本仅挂起,直到服务器收到消息,之后它允许客户端发送消息,但无论如何客户端都无法从服务器接收消息。我正在尽我所能地解决这两个错误,但我的研究却毫无进展。这是问题代码,提前致谢。
while True:
socket_list.append(s)
read_sockets, write_sockets, error_sockets = select.select(socket_list, [], [], 20)
if not (read_sockets or write_sockets or error_sockets):
if afk == False:
s.send('[Status]: '+str(_user)+' has gone afk.\n')
sys.stdout.write('\n[+]: You have gone afk.\n')
afk = True
prompt()
for sock in read_sockets:
print ('Starting for sock in read_sockets') #DEBUG#
if sock == s:
print ('Getting here.') #DEBUG#
data = sock.recv(4096)
if not data:
sys.stdout.write('[!]: Disconnected from chat server by server.\n'+W)
choice = raw_input('[*]: Press Enter to continue.')
_logic()
else:
sys.stdout.write(data)
else:
# Rest of the Program (Runs correctly) #
最佳答案
听起来您忘记将套接字设置为非阻塞。与几乎所有状态报告功能一样,select
并不对 future 做出保证。您还需要处理 read
返回“将阻止”指示的情况。您不能依靠 select
来预测 future read
操作的结果。
关于python - Windows 上 select.select() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16285362/