Using Linux epoll with Python
来源:互联网 发布:中国网络自由 编辑:程序博客网 时间:2024/05/21 19:18
Author:海峰 http://weibo.com/344736086
http://yanheven.github.io/
http://blog.csdn.net/yanheven1
transfer from http://scotdoyle.com/python-epoll-howto.html
import socket, selectEOL1 = b'\n\n'EOL2 = b'\n\r\n'response = b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'response += b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'response += b'Hello, world!'serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)serversocket.bind(('0.0.0.0', 8080))serversocket.listen(1)serversocket.setblocking(0)serversocket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)epoll = select.epoll()epoll.register(serversocket.fileno(), select.EPOLLIN)try: connections = {}; requests = {}; responses = {} while True: events = epoll.poll(1) for fileno, event in events: if fileno == serversocket.fileno(): connection, address = serversocket.accept() connection.setblocking(0) epoll.register(connection.fileno(), select.EPOLLIN) connections[connection.fileno()] = connection requests[connection.fileno()] = b'' responses[connection.fileno()] = response elif event & select.EPOLLIN: requests[fileno] += connections[fileno].recv(1024) if EOL1 in requests[fileno] or EOL2 in requests[fileno]: epoll.modify(fileno, select.EPOLLOUT) print('-'*40 + '\n' + requests[fileno].decode()[:-2]) elif event & select.EPOLLOUT: byteswritten = connections[fileno].send(responses[fileno]) responses[fileno] = responses[fileno][byteswritten:] if len(responses[fileno]) == 0: epoll.modify(fileno, 0) connections[fileno].shutdown(socket.SHUT_RDWR) elif event & select.EPOLLHUP: epoll.unregister(fileno) connections[fileno].close() del connections[fileno]finally: epoll.unregister(serversocket.fileno()) epoll.close() serversocket.close()
1 0
- Using Linux epoll with Python
- How To Use Linux epoll with Python
- How To Use Linux epoll with Python
- How To Use Linux epoll with Python
- How To Use Linux epoll with Python
- How To Use Linux epoll with Python
- How To Use Linux epoll with Python
- Using Databases with Python
- Using SPI with Linux
- how to use linux epoll with c
- Write Excel files with Python using xlwt
- using OpenCV with python for descriptor extraction
- Using Databases with Python-Counting Organizations
- eoprocessing with Python using Open Source GIS
- Using Databases with Python quiz 1
- What is EPOLL? EPOLL vs Select call? And How to implement UDP server in Linux using EPOLL?
- What is EPOLL? Epoll vs Poll vs Select call ? And How to implement UDP server in Linux using EPOLL?
- python epoll
- jdk与jre的区别
- 莫往Applicaotion存缓存/app被系统回收之后再打开发生了什么
- Android-Toast和Notification
- hdu4126树形dp + 错误分析
- 通过缓冲区进行文件复制的操作
- Using Linux epoll with Python
- Google地图百度地图GPS经纬度偏移转换(JAVA)
- 黑马程序员--java笔记15--网络编程 Java.net
- Validate Binary Search Tree
- Study JavaScript《JS操作SVG的一些知识》
- 畅谈Javascript设计模式(序)
- iOS 中图片循环动画
- Windows基础-实时录音程序(WaveXXX)
- hdoj Clarke and five-pointed star 5563 (判断能否组成五角星)