how to use epoll with python
1 import socket, select
2
3 EOL1 = b'\n\n'
4 EOL2 = b'\n\r\n'
5 response = b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'
6 response += b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'
7 response += b'Hello, world!'
8
9 serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
10 serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
11 serversocket.bind(('0.0.0.0', 8080))
12 serversocket.listen(1)
13 serversocket.setblocking(0)
14
15 epoll = select.epoll()
16 epoll.register(serversocket.fileno(), select.EPOLLIN)
17
18 try:
19 connections = {}; requests = {}; responses = {}
20 while True:
21 events = epoll.poll(1)
22 for fileno, event in events:
23 if fileno == serversocket.fileno():
24 connection, address = serversocket.accept()
25 connection.setblocking(0)
26 epoll.register(connection.fileno(), select.EPOLLIN)
27 connections[connection.fileno()] = connection
28 requests[connection.fileno()] = b''
29 responses[connection.fileno()] = response
30 elif event & select.EPOLLIN:
31 requests[fileno] += connections[fileno].recv(1024)
32 if EOL1 in requests[fileno] or EOL2 in requests[fileno]:
33 epoll.modify(fileno, select.EPOLLOUT)
34 print('-'*40 + '\n' + requests[fileno].decode()[:-2])
35 elif event & select.EPOLLOUT:
36 byteswritten = connections[fileno].send(responses[fileno])
37 responses[fileno] = responses[fileno][byteswritten:]
38 if len(responses[fileno]) == 0:
39 epoll.modify(fileno, 0)
40 connections[fileno].shutdown(socket.SHUT_RDWR)
41 elif event & select.EPOLLHUP:
42 epoll.unregister(fileno)
43 connections[fileno].close()
44 del connections[fileno]
45 finally:
46 epoll.unregister(serversocket.fileno())
47 epoll.close()
48 serversocket.close() EPOLLERR
Error condition happened on the associated file descriptor.
This event is also reported for the write end of a pipe when
the read end has been closed. epoll_wait(2) will always
report for this event; it is not necessary to set it in
events. EPOLLHUP
Hang up happened on the associated file descriptor.
epoll_wait(2) will always wait for this event; it is not
necessary to set it in events. Line 40: A socket shutdown is optional if a connection is closed explicitly.
This example program uses it in order to cause the client to shutdown first.
The shutdown call informs the client socket that no more data should be sent
or received and will cause a well-behaved client to close the socket connection
from it's end. Line 41: The HUP (hang-up) event indicates that the client socket has been
disconnected (i.e. closed), so this end is closed as well. There is no need
to register interest in HUP events. They are always indicated on sockets
that are registered with the epoll object. socket.shutdown(how)
Shut down one or both halves of the connection. If how is SHUT_RD, further receives are disallowed. If how is SHUT_WR, further sends are disallowed. If how is SHUT_RDWR, further sends and receives are disallowed. Depending on the platform, shutting down one half of the connection can also close the opposite half (e.g. on Mac OS X, shutdown(SHUT_WR) does not allow further reads on the other end of the connection).
how to use epoll with python的更多相关文章
- How To Use Linux epoll with Python
http://scotdoyle.com/python-epoll-howto.html Line 1: The select module contains the epoll functional ...
- fw:学好Python必读的几篇文章
学好Python必读的几篇文章 from:http://blog.csdn.net/hzxhan/article/details/8555602 分类: python2013-01-30 11:52 ...
- Python代码样例列表
扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│ Python用户推荐系统曼哈顿算法实现.py│ ...
- python网络编程——IO多路复用之select
1 IO多路复用的概念 原生socket客户端在与服务端建立连接时,即服务端调用accept方法时是阻塞的,同时服务端和客户端在收发数据(调用recv.send.sendall)时也是阻塞的.原生so ...
- Python——学好Python必读的几篇文章
作为脚本语言Python上手容易,但要学好Python能写出一手漂亮的.Pythonic的Python代码并非一日之功,本文的目的在于推荐 一些优秀的Python相关的文章(至于书大家可以看dip.l ...
- python 学习笔记12(事件驱动、IO多路复用、异步IO)
阻塞IO和非阻塞IO.同步IO和异步IO的区别 讨论背景:Linux环境下的network IO. 1.先决条件(几个重要概念) 1.1.用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32 ...
- 深入tornado中的ioLoop
本文所剖析的tornado源码版本为4.4.2 ioloop就是对I/O多路复用的封装,它实现了一个单例,将这个单例保存在IOLoop._instance中 ioloop实现了Reactor模型,将所 ...
- 网络IO-阻塞、非阻塞、IO复用、异步
网络socket输入操作分为两个阶段:等待网络数据到达和将到达内核的数据复制到应用进程缓冲区.对这两个阶段不同的处理方式将网络IO分为不同的模型:IO阻塞模型.非阻塞模型.多路复用和异步IO. 一 阻 ...
- eventlet 模块搭建 WEB 服务器
eventlet这个强悍的东东,看到我同事的一些整理.故贴出来,大家一起分享~ motivation 114.113.199.11服务器上nova服务中基于python eventlet实现的定时任务 ...
随机推荐
- POJ-1390-Blocks (复杂区间DP)
$ POJ~1390~~Blocks: $ (很难想的区间DP) $ solution: $ 很好的一道题目.看起来似乎很简单,当时一直认为可以用二维区间DP来完成,转移 $ n^3 $ . 后来发现 ...
- D0g3_Trash_Pwn_Writeup
Trash Pwn 下载文件 1 首先使用checksec查看有什么保护 可以发现,有canary保护(Stack),堆栈不可执行(NX),地址随机化没有开启(PIE) 2 使用IDA打开看看 mai ...
- LeetCode--058--最后一个单词(java)
给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...
- 记录一下LEETheme库主题切换的使用
LEETheme库的位置:https://github.com/lixiang1994/LEETheme 从https://github.com/gsdios的SDAutolayoutDemo 中的白 ...
- python测试redis是否可以使用
前提打开redis服务,windows打开方式到redis的安装目录命令行输入redis-server from redis import StrictRedis redis = StrictRedi ...
- threading.get_ident()
https://docs.python.org/3/library/threading.html Return the 'thread identifier' of the current threa ...
- TODO 思维模型 尺子 游标卡尺
100个人 两个思维切入点 1>做对的题怎么分配 m(x)表示答对x道题的最多人数f(x)表示答对x道题的人数 m(0)=5m(1)=20=>f(0)=0m(2)=m(1)+1=21=&g ...
- django中的url控制
1.django中的第一个控件:url控制 (路由分发) urls.py:请求路径与视图函数的之间的关系 步骤: 1.首先是要配置环境, 2.其次就是引路径 3.在视图的文件夹里面写相应的函 ...
- win2019
slmgr /upkslmgr /ipk N69G4-B89J2-4G8F4-WWYCC-J464Cslmgr /skms zh.us.toslmgr /ato
- 测开之路七十七:性能测试蓝图之js
//定义全局的editor = nullvar editor = null; //ace_editor的初始化函数function ace_editor() { var editor = ace.ed ...