加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用select的Python异步套接字

发布时间:2020-12-16 21:38:53 所属栏目: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 select import socket import sys
我正在研究异步套接字,我有这个代码:
#!/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 – 尝试使用非套接字的东西进行操作.谁能告诉我有什么问题?谢谢:)

解决方法

你正在使用Windows,不是吗?在Windows上,select仅适用于套接字.但是sys.stdin不是套接字.从第15行删除它应该工作.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读