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的更多相关文章

  1. How To Use Linux epoll with Python

    http://scotdoyle.com/python-epoll-howto.html Line 1: The select module contains the epoll functional ...

  2. fw:学好Python必读的几篇文章

    学好Python必读的几篇文章 from:http://blog.csdn.net/hzxhan/article/details/8555602 分类: python2013-01-30 11:52  ...

  3. Python代码样例列表

    扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│       Python用户推荐系统曼哈顿算法实现.py│    ...

  4. python网络编程——IO多路复用之select

    1 IO多路复用的概念 原生socket客户端在与服务端建立连接时,即服务端调用accept方法时是阻塞的,同时服务端和客户端在收发数据(调用recv.send.sendall)时也是阻塞的.原生so ...

  5. Python——学好Python必读的几篇文章

    作为脚本语言Python上手容易,但要学好Python能写出一手漂亮的.Pythonic的Python代码并非一日之功,本文的目的在于推荐 一些优秀的Python相关的文章(至于书大家可以看dip.l ...

  6. python 学习笔记12(事件驱动、IO多路复用、异步IO)

    阻塞IO和非阻塞IO.同步IO和异步IO的区别 讨论背景:Linux环境下的network IO. 1.先决条件(几个重要概念) 1.1.用户空间与内核空间 现在操作系统都是采用虚拟存储器,那么对32 ...

  7. 深入tornado中的ioLoop

    本文所剖析的tornado源码版本为4.4.2 ioloop就是对I/O多路复用的封装,它实现了一个单例,将这个单例保存在IOLoop._instance中 ioloop实现了Reactor模型,将所 ...

  8. 网络IO-阻塞、非阻塞、IO复用、异步

    网络socket输入操作分为两个阶段:等待网络数据到达和将到达内核的数据复制到应用进程缓冲区.对这两个阶段不同的处理方式将网络IO分为不同的模型:IO阻塞模型.非阻塞模型.多路复用和异步IO. 一 阻 ...

  9. eventlet 模块搭建 WEB 服务器

    eventlet这个强悍的东东,看到我同事的一些整理.故贴出来,大家一起分享~ motivation 114.113.199.11服务器上nova服务中基于python eventlet实现的定时任务 ...

随机推荐

  1. 随笔4 Dictionary<K,V>

    本来说是想介绍一下Hashtable的,但是发现HashMap和Hashtable最开始的不同就是在于HashMap继承了AbstractMap,而Hashtable继承了Dictionary< ...

  2. Sass @debug

    @debug 在 Sass 中是用来调试的,当你的在 Sass 的源码中使用了 @debug 指令之后,Sass 代码在编译出错时,在命令终端会输出你设置的提示 Bug: @debug 10em + ...

  3. spring boot generator

    pom.xml 插件引用依赖 <build> <plugins> <plugin> <groupId>org.springframework.boot& ...

  4. django之配置文件setting.py

    一:配置文件setting.py中的简单配置更改 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 简单解释 ...

  5. NODE升级到V12.X.X

    Node.js 是一个基于Chrome JavaScript运行时的平台,可轻松构建快速,可扩展的网络应用程序.最新版本 node.js yum存储库 由其官方网站维护.使用本教程添加yum存储库,并 ...

  6. 【转】iis解决应用程序池**提供服务的进程意外终止进程ID是**。进程退出代码是'0x80'

    转自:http://blog.sina.com.cn/s/blog_56a68d5501013xdd.html 我们公司旗下的红黑互联会遇到这种问题 事件类型: 警告事件来源: W3SVC事件种类: ...

  7. 安装及启动Tomcat

    安装及启动Tomcat 法一:从命令行启动Tomcat: 配置环境变量 Windos+R输入cmd打开dos窗口转到D:\apache-tomcat-7.0.54\bin目录,并输入startup.b ...

  8. 音悦台 api分析

    用户订阅MV更新 http://uapi.yinyuetai.com/i/flw/subscribe-video-list?page=1&pageSize=200&uid=XXXXXX ...

  9. pillow模块快速学习

    一.pillow的简单使用 1.安装 方式一: 如果配置了python中的script路径为环境变量,直接cmd中执行如下命令: pip3 install pillow 方式二: 通过编译器环境,如p ...

  10. ECMAScript 2015 可迭代协议:迭代普通对象

    可迭代协议允许 JavaScript 对象去定义或定制它们的迭代行为, 例如(定义)在一个 for..of结构中什么值可以被循环(得到). 一些内置类型都是内置的可迭代类型并且有默认的迭代行为( 比如 ...