您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用select的Python异步套接字

5b51 2022/1/14 8:20:28 python 字数 1965 阅读 450 来源 www.jb51.cc/python

我正在研究异步套接字,我有这个代码: #!/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 se

概述

#!/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()的基本类型的echo服务器,但是当我运行它时,我选择错误10038 – 尝试使用非套接字的东西进行操作.谁能告诉我有什么问题?谢谢:)

在Linux或类似的东西,我希望它的工作原理如上所述.

总结

以上是编程之家为你收集整理的使用select的Python异步套接字全部内容,希望文章能够帮你解决使用select的Python异步套接字所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶