带select的异步套接字-Python [英] Asynchronous sockets with select - Python

查看:126
本文介绍了带select的异步套接字-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在研究异步套接字,并且我有以下代码:

i am studying asynchronous sockets right now and i have this code:

#!/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-尝试使用非套接字的东西进行操作.有人可以告诉我怎么了吗?谢谢:)

It shoud be a basic kind of echo server using select(), but when I run it, I ger select error 10038 - attemp to manipulation with something which is not socket. Can someone tell me what is wrong? Thank you:)

推荐答案

您在Windows上工作,不是吗?在Windows上,select仅在套接字上起作用.但是sys.stdin没有套接字.从第15行中删除它,它应该可以工作.

You are working on Windows, aren't you? On Windows select works only on sockets. But sys.stdin is no socket. Remove it from line 15 and it should work.

在Linux或类似系统上,我希望它能像上面列出的那样工作.

On Linux or the like I would expect it working as listed above.

这篇关于带select的异步套接字-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆