事件驱动IO-udp
来源:互联网 发布:知乎北京五中大厂分校 编辑:程序博客网 时间:2024/06/15 03:11
s.py
import socketimport timeimport selectclass EventHandler: def fileno(self): 'Return the associated file descriptor' raise NotImplemented('must implement') def wants_to_receive(self): 'Return True if receiving is allowed' return False def handle_receive(self): 'Perform the receive operation' pass def wants_to_send(self): 'Return True if sending is requested' return False def handle_send(self): 'Send outgoing data' passclass UDPServer(EventHandler): def __init__(self, address): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.bind(address) def fileno(self): return self.sock.fileno() def wants_to_receive(self): return Trueclass UDPTimeServer(UDPServer): def handle_receive(self): msg, addr = self.sock.recvfrom(1) self.sock.sendto(time.ctime().encode('ascii'), addr)class UDPEchoServer(UDPServer): def handle_receive(self): msg, addr = self.sock.recvfrom(8192) self.sock.sendto(msg, addr)def event_loop(handlers): while True: wants_recv = [h for h in handlers if h.wants_to_receive()] wants_send = [h for h in handlers if h.wants_to_send()] print(wants_recv,wants_send) can_recv, can_send, _ = select.select(wants_recv, wants_send, []) print(can_recv,can_send) for h in can_recv: h.handle_receive() for h in can_send: h.handle_send()if __name__ == '__main__': handlers = [UDPEchoServer(('', 15000))] # handlers = [UDPTimeServer(('', 14000)), UDPEchoServer(('', 15000))] event_loop(handlers)
from socket import *s = socket(AF_INET, SOCK_DGRAM)s.sendto(b'hello',('localhost',15000))print(s.recvfrom(128))
0 0
- 事件驱动IO-udp
- 事件驱动IO-tcp
- 事件驱动与异步IO
- 论事件驱动与异步IO
- 信号驱动IO的UDP回射服务器
- python(十)下:事件驱动与 阻塞IO、非阻塞IO、IO多路复用、异步IO
- 关于事件驱动的学习一 异步IO aio
- 使用gtk+的iochannel进行事件驱动IO操作
- 驱动基础知识-怎么访问io,中断,产生线程,事件
- Netty 基于事件驱动模型实现的异步IO
- 根据cookbook, 多线程情况的事件驱动IO
- Nodejs的单线程、异步IO与事件驱动
- python——异步IO与事件驱动
- Nodejs之事件驱动+非阻塞io模型
- 网络编程五种IO模型之重叠IO模型-事件驱动
- [创新杯] UDP IO
- 事件Io
- 事件驱动
- 日期格式不符合要求:Unparseable date: "3e8a4d83533744c698216535a65850c0"
- python爬虫(20)使用真实浏览器打开网页的两种方法
- 安装sql server 2016报错误1402(请验证您是否具有足够的权限访问该项/Verify that you have sufficient access to that key)的解决办法
- C#.net 关键字之virtual老司机教学
- AJAX入门(被封装过的AJAX)
- 事件驱动IO-udp
- vue-router使用
- POJ 1273 Drainage Ditche(最大流模板题)
- CefSharp 屏蔽右键菜单
- redhat中通过nano创建用户
- 数组中插入元素(C++)
- 自定义标签的初步入门
- 项目整体管理·六
- 堆排序算法java实现