博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python实现的poll异步io的echoserver
阅读量:5906 次
发布时间:2019-06-19

本文共 3785 字,大约阅读时间需要 12 分钟。

hot3.png

#!/usr/bin/env python#Asynchronous Echo Server#filename:echoserver.pyimport socket,traceback,os,sys,selectclass stateclass:        stdmask = select.POLLERR|select.POLLHUP|select.POLLNVAL                def __init__(self,mastersock):                #Initialize the state class                     self.p = select.poll()                self.mastersock = mastersock                self.watchread(mastersock.fileno())                self.buffers = {}                self.sockets = {mastersock.fileno():mastersock}                        def fd2socket(self,fd):                return self.sockets[fd]                        def watchread(self,fd):                self.p.register(fd,select.POLLIN|self.stdmask)                        def watchwrite(self,fd):                self.p.register(fd,select.POLLOUT|self.stdmask)                        def watchboth(self,fd):                self.p.register(fd,select.POLLIN|select.POLLOUT|self.stdmask)                        def dontwatch(self,fd):                self.p.unregister(fd)                        def newconn(self,sock):                fd = sock.fileno()                self.watchboth(fd)                self.buffers[fd] = "Welcome to the echoserver,%s\n"%str(sock.getpeername())                self.sockets[fd] = sock                        def readevent(self,fd):                try:                        self.buffers[fd] += self.fd2socket(fd).recv(4096)                except:                            self.closeout(fd)                self.watchboth(fd)                        def writeevent(self,fd):                        if not len(self.buffers[fd]):                        self.watchread(fd)                        return                try:                        byteswritten = self.fd2socket(fd).send(self.buffers[fd])                except:                        self.closeout[fd]                self.buffers[fd] = self.buffers[fd][byteswritten:]                if not len(self.buffers[fd]):                        self.watchread(fd)                                def errorevent(self,fd):                self.closeout(fd)                        def closeout(self,fd):                self.dontwatch(fd)                try:                        self.fd2socket(fd).close()                except:                        pass                del self.buffers[fd]                del self.sockets[fd]                        def loop(self):                while 1:                        result = self.p.poll()                        for fd,event in result:                                if fd == self.mastersock.fileno() and event  == select.POLLIN:                                       try:                                                newsock,addr = self.fd2socket(fd).accept()                                                newsock.setblocking(0)                                                print "Got connection from",newsock.getpeername()                                                self.newconn(newsock)                                        except:                                                pass                                elif event == select.POLLIN:                                                                                self.readevent(fd)                                elif event == select.POLLOUT:                                                                              self.writeevent(fd)                                else :                                                                               self.errorevent(fd)host = ''port = 51423s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)s.bind((host,port))s.listen(1)s.setblocking(0)state = stateclass(s)state.loop()

转载于:https://my.oschina.net/u/557955/blog/196872

你可能感兴趣的文章
AJAX-初学AJAX本地环境配置
查看>>
【精选】Nginx负载均衡学习笔记(一)实现HTTP负载均衡和TCP负载均衡(官方和OpenResty两种负载配置)...
查看>>
Linux常用基本命令( rmdir, rm, mv )
查看>>
POJ2406 Power Strings(KMP)
查看>>
JavaScript-console的使用_016
查看>>
两种方式设置iframe的高度区别
查看>>
Iterator 和 for...of 循环
查看>>
关于iOS 11.x微信连wifi流程中,在Portal页无法拉起微信问题的简单记录
查看>>
Python GUI库wxPython官网Hello World示例的逐行解释
查看>>
RE·WORK 巅峰对话:深度学习将彻底改变医疗健康领域
查看>>
Codeforces Round #442 (Div. 2) A B
查看>>
极值问题(acms)
查看>>
swift UI专项训练8 展示数据
查看>>
一起学shell(十一)之安全的shell脚本:起点
查看>>
Microsoft® Deployment Toolkit 2010之快速部署Windows 7
查看>>
LNMP的技术讲解
查看>>
SVN Hooks的介绍及使用
查看>>
Oracle 字符集的查看和修改【上】
查看>>
tomcat注册windows服务
查看>>
使用qq邮箱的smpt服务发送邮件一定要记得用ssl
查看>>