【发布时间】:2012-08-25 01:16:23
【问题描述】:
我现在正在研究异步套接字,我有这个代码:
#!/usr/bin/env 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 = 'localhost'
port = 900
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()
它应该是一种使用 select() 的基本回显服务器,但是当我运行它时,我选择错误 10038 - 尝试使用不是套接字的东西进行操作。有人可以告诉我有什么问题吗?谢谢:)
【问题讨论】: