我正尝试在windows下运行以下python服务器:
"""
An echo server that uses select to handle multiple clients at a time.
Entering any line of input at the terminal will exit the server.
"""
import select
import socket
import sys
host = ''
port = 50000
backlog = 5
size = 1024
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host,port))
server.listen(backlog)
input = [server,sys.stdin]
running = 1
while running:
inputready,outputready,exceptready = select.select(input,[],[])
for s in inputready:
if s == server:
# handle the server socket
client, address = server.accept()
input.append(client)
elif s == sys.stdin:
# handle standard input
junk = sys.stdin.readline()
running = 0
else:
# handle all other sockets
data = s.recv(size)
if data:
s.send(data)
else:
s.close()
input.remove(s)
server.close() 我得到了错误消息(10038,‘试图对不是套接字的东西执行操作’)。这可能与python文档中的the remark有关:“Windows上的文件对象是不可接受的,但是套接字是可以接受的。在Windows上,底层的select()函数是由WinSock库提供的,并且不处理不是来自WinSock的文件描述符。”在互联网上有相当多的关于这个话题的帖子,但它们对我来说要么太技术性,要么就是不清楚。所以我的问题是:有没有办法在windows下使用python中的select()语句?请添加一个小示例或修改我上面的代码。谢谢!
发布于 2012-06-01 07:54:41
看起来它不像sys.stdin
如果您将输入更改为以下内容
input = [server] 异常将会消失。
这是来自the doc的
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.发布于 2012-06-01 07:57:55
我不知道你的代码是否有其他问题,但你得到的错误是因为将input传递给select.select(),问题是它包含了不是套接字的sys.stdin。在Windows下,select只适用于套接字。
顺便说一句,input是一个python函数,将其用作变量并不是一个好主意。
发布于 2017-01-14 07:00:05
当然,给出的答案是正确的。您只需从输入中删除sys.stdin,但仍在迭代中使用它:
inputready+sys.stdin中%s的
https://stackoverflow.com/questions/10842428
复制相似问题