Python——eventlet.wsgi
eventlet 的 wsgi 模块提供了一种启动事件驱动的WSGI服务器的简洁手段,可以将其作为某个应用的嵌入web服务器,或作为成熟的web服务器,一个这样的web服务器的例子就是 Spawning。
目录
2. eventlet.wsgi.format_data_time()
一、Eventlet 的 WSGI server
要启动一个 wsgi 服务器,只需要创建一个套接字,然后用它调用 eventlet.wsgi.server() 就可以。
例如:
from eventlet import wsgi
import eventlet def hello_world(env, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello, World!\r\n'] wsgi.server(eventlet.listen(('', 8090)), hello_world)
这个简单的 server 使用 eventlet.listen() 创建了一个套接字,wsgi.server() 监听对应的地址、端口等,将请求传递给 WSGI 应用 hello_world 处理。
一个稍微形象一些的例子如下:
import eventlet
from eventlet import wsgi def hello_world(env, start_response):
if env['PATH_INFO'] != '/':
start_response('404 Not Found', [('Content-Type', 'text/plain')])
return ['Not Found\r\n']
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello, World!\r\n'] wsgi.server(eventlet.listen(('', 8090)), hello_world)
这个例子非常简洁地诠释了 WSGI 的应用接口规范,也涵盖了 eventlet.wsgi 模块中起 server 的用法。
1.
eventlet.wsgi.server()
eventlet.wsgi.server(
socket,
site,
log=None,
environ=None,
max_size=None,
max_http_version='HTTP/1.1',
protocol=<class eventlet.wsgi.HttpProtocol at 0x7f4f68192f58>, server_event=None,
minimum_chunk_size=None,
log_x_forwarded_for=True,
custom_pool=None,
keepalive=True,
log_output=True,
log_format='%(client_ip)s - - [%(date_time)s] "%(request_line)s" %(status_code)s %(body_length)s %(wall_seconds).6f',
url_length_limit=8192,
debug=True,
socket_timeout=None,
capitalize_response_headers=True)
这个调用封装了很多功能,创建一个 WSGI server 来处理指定套接字中产生的请求,这个函数会一直循环下去,即时刻监听请求。当 server 退出时,对应的套接字对象 socket 也会被关闭但是底层的文件描述字会被保留,所以这个时候如果用这个 socket 调用 dup() ,将会仍然可以使用。
参数:
| socket | Server socket, 已经绑定一个端口且监听中 |
| site | 实现 WSGI 应用的函数 |
|
log |
日志输出的位置,应该是一个类文件(file-like)对象,缺省为 sys.stderr |
| environ | 添加到每一次请求的 environ 字典中的额外参量 |
| max_size | 这个 server 时刻允许的最大客户端连接数 |
| max_http_version | 设为 “HTTP/1.0” 会只支持 HTTP 1.0. 可以支持那些在 HTTP 1.1 下并不能正常工作的应用 |
| protocol | 弃用,协议类 |
| server_event | 弃用,用来收集 Server 对象 |
| minimum_chunk_size | 以字节记的HTTP块的最小尺寸。 可以用来改进哪些产生很多小字串的应用的性能。尽管使用它在技术上违背 WSGI 规范,但可以通过设置environ[‘eventlet.minimum_write_chunk_size’]来在每次请求的程度上覆写 |
| log_x_forwarded_for | 如果为 True (缺省), 除了在日志的 ‘client_ip’ 段记录实际的客户端 ip 地址外,还记录HTTP头中的 x-forwarded-for 的内容 |
| custom_pool | 一个定制的 GreenPool 实例,用来孵化客户端的 greenthreads. 如果设置了该项,无视参数 max_size |
| keepalive | 设为False 会禁止 server keepalives,所有的连接都会在服务完一个请求后关闭 |
| log_output | Boolean 值,指示 server 是否记录日志 |
| log_format | A python format string that is used as the template to generate log lines. The following values can be formatted into it: client_ip, date_time, request_line, status_code, body_length, wall_seconds. The default is a good example of how to use it |
| url_length_limit | 请求URL的最大长度,如果超长,返回414错误 |
| debug | 如果想要服务器将异常追溯信息子啊500错误中发回给客户端,这里就设置为True。 如果这里设置为 False,server 将会响应空值 |
| socket_timeout | 客户端连接的套接字操作超时限制,缺省为 None,意味着永久等待 |
| capitalize_response_headers | 大写响应头的字段名称,缺省为True |
2.
eventlet.wsgi.format_date_time(timestamp)
将Unix时间戳格式化为一个 HTTP 标准字符串。
二、SSL
要创建一个安全的 server ,只需要传入一个 SSL 包裹的套接字即可:
wsgi.server(eventlet.wrap_ssl(
eventlet.listen(('', 8090)),
certfile='cert.crt',
keyfile='private.key',
server_side=True),
hello_world)
应用可以通过环境变量 env['wsgi.url_scheme'] 判断自己是不是在一个SSL server中。
三、支持 Post Hooks的非标准扩展
Eventlet 的 WSGI server 支持对于 WSGI 规范的非标准扩展——用 env['eventlet.posthooks'] 包含一个有若干post hooks 的数组,这些 post hooks 会在完全发送完响应后被调用。每一个 post hook 是一个格式为 (func, args, kwargs) 的元组,以 WSGI environment 字典加 args 和 kwargs 为参数调用 func。
例如:
from eventlet import wsgi
import eventlet def hook(env, arg1, arg2, kwarg3=None, kwarg4=None):
print('Hook called: %s %s %s %s %s' % (env, arg1, arg2, kwarg3, kwarg4)) def hello_world(env, start_response):
env['eventlet.posthooks'].append(
(hook, ('arg1', 'arg2'), {'kwarg3': 3, 'kwarg4': 4}))
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello, World!\r\n'] wsgi.server(eventlet.listen(('', 8090)), hello_world)
上面的代码会为每一个处理的请求打印 WSGI 环境和其他的函数参数。
当需要在完全响应一个客户端请求后(或客户端过早地断开连接后)执行一段代码时 Post hook 非常有用。 一个例子就是精确记录带宽的使用情况,因为用户断开连接时消耗的带宽小于 Content-Length 中标出的值。
四、“100 Continue” 响应头
Eventlet 的 WSGI server 支持发送(可选的)headers 和 HTTP “100 Continue” 临时响应. This is useful in such cases where a WSGI server expects to complete a PUT request as a single HTTP request/response pair, and also wants to communicate back to client as part of the same HTTP transaction. An example is where the HTTP server wants to pass hints back to the client about characteristics of data payload it can accept. As an example, an HTTP server may pass a hint in a header the accompanying “100 Continue” response to the client indicating it can or cannot accept encrypted data payloads, and thus client can make the encrypted vs unencrypted decision before starting to send the data).
This works well for WSGI servers as the WSGI specification mandates HTTP expect/continue mechanism (PEP333).
要定义 “100 Continue” 响应头, 需要调用 env['wsgi.input'] 里的 set_hundred_continue_response_header() 如下:
from eventlet import wsgi
import eventlet def wsgi_app(env, start_response):
# Define "100 Continue" response headers
env['wsgi.input'].set_hundred_continue_response_headers(
[('Hundred-Continue-Header-1', 'H1'),
('Hundred-Continue-Header-k', 'Hk')])
# The following read() causes "100 Continue" response to
# the client. Headers 'Hundred-Continue-Header-1' and
# 'Hundred-Continue-Header-K' are sent with the response
# following the "HTTP/1.1 100 Continue\r\n" status line
text = env['wsgi.input'].read()
start_response('200 OK', [('Content-Length', str(len(text)))])
return [text]
Python——eventlet.wsgi的更多相关文章
- WSGI学习系列eventlet.wsgi
WSGI是Web Service Gateway Interface的缩写. WSGI标准在PEP(Python Enhancement Proposal)中定义并被许多框架实现,其中包括现广泛使用的 ...
- Python的WSGI(Web Server Gateway Interface)服务器
Python的WSGI(Web Server Gateway Interface)服务器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- Python——eventlet
eventlet语境下的“绿色线程”普通线程之间的区别: 1. 绿色线程几乎没有开销,不用像保留普通线程一样保留“绿色线程”,每一个网络连接对应至少一个“绿色线程”: 2. 绿色线程需要人为的设置使其 ...
- Python记录wsgi
类实现wsgi app from wsgiref.util import setup_testing_defaults from wsgiref.simple_server import make_s ...
- ubuntu python apache2 wsgi django框架
在ubuntu上通过apatch2和wsgi部署django (亲手做过!!!) 一,我的python.django.apatch2版本: python:python -V 2.7.3 django: ...
- python之WSGI与Guincorn
WSGI与Guincorn WSGI WSGI (Web Server Gateway Interface),WSGI是为Python语言定义的Web服务器和Web应用程序之间的一种通用接口. 如下图 ...
- python的WSGI接口
WSGI:Web Server Gateway Interface. WSGI是为python语言定义的web服务器和web应用程序或框架之间的一种简单而实用的接口.wsgi是一个web组件的接口规范 ...
- Python——eventlet.websocket
使用该模块可以方便地创建 websocket 服务器,要创建一个websocket服务器,只需要将一个句柄函数用装饰器 WebSocketWSGI 装饰即可,然后这个函数就可以当做一个WSGI应用: ...
- Python——eventlet.hubs
Hub构成了 Eventlet 的事件循环,它分发 I/O 事件.调度 greenthread.Hub的存在使得协程被提升为 greenthreads. Eventlet 有多种hub的实现,所以在使 ...
随机推荐
- 【教程】Source Insight 关联 .S文件,汇编文件
加载.s汇编文件 做ARM嵌入式开发时,有时得整汇编代码,但在SI里建立PROJECT并ADD TREE的时候,根据默认设置并不会把该TREE里面所有汇编文件都包含进来,默认只加了.inc和.asm后 ...
- 单独的 python 脚本文件使用 django 自带的 model
django1.9.5&python3.4.4 文件结构 在一个爬虫脚本中将爬取的数据通过django自带的model保存到数据库 修改的文件(其余pycharm新建Django项目生成, ...
- vi卡死解决办法
玩了这么多年linux 居然不知道这个..特此记录. 使用vim时,如果你不小心按了 Ctrl + s后,你会发现不能输入任何东西了,像死掉了一般,其实vim并没有死掉,这时vim只是停止向终端输出而 ...
- JAVA-JSP内置对象之application对象获得服务器版本
相关资料:<21天学通Java Web开发> application对象获得服务器版本1.通过application对象的getMajorVersion()方法和getMinorVersi ...
- 基于jQuery实现的腾讯互动娱乐网站特效
分享一款基于jQuery实现的腾讯互动娱乐网站特效.腾讯互动娱乐网站jQuery特效是一款右侧带伸缩选项卡,支持鼠标滚轮滚动切换特效代码.效果图如下: 在线预览 源码下载 实现的代码. html代 ...
- java基础篇---网络编程(UDP程序设计)
UDP程序设计 在TCP的索引操作都必须建立可靠地连接,这样一来肯定会浪费大量的系统性能,为了减少这种开销,在网络中又提供了另外一种传输协议---UDP,不可靠的连接,这种协议在各个聊天工具中被广泛的 ...
- centos7 安装oracle jdk 与openjdk 实现切换
1. 分别安装oraclejdk 与openjdk #下载安装oraclejdk rpm -ivh --prefix=/usr.java/java1.8 ***.rpm #安装openjdk su - ...
- PHPUnit 在phpstrom中composer项目的应用配置
在phpstorm的composer搭建的项目调试时出现这种错误时:是其配置的错误 'Cannot create phar '/data/AppStorm/DesignPatternsPHP/vend ...
- TCP粘包, UDP丢包, nagle算法
一.TCP粘包 1. 什么时候考虑粘包 如果利用tcp每次发送数据,就与对方建立连接,然后双方发送完一段数据后,就关闭连接,这样就不会出现粘包问题(因为只有一种包结构,类似于http协议,UDP不会出 ...
- Redis集群方案<转>
为什么集群? 通常,为了提高网站响应速度,总是把热点数据保存在内存中而不是直接从后端数据库中读取.Redis是一个很好的Cache工具.大型网站应用,热点数据量往往巨大,几十G上百G是很正常的事儿,在 ...