WSGI是Web Service Gateway Interface的缩写。

WSGI标准在PEP(Python Enhancement Proposal)中定义并被许多框架实现,其中包括现广泛使用的django框架。

PythonWeb服务器接口(Python Web Server Gateway Interface,缩写为WSGI)是Python应用程序或框架和Web服务器之间的一种接口,已经被广泛接受, 它已基本达成它的可移植性方面的目标。

Nova使用了eventlet.wsgi接口。

  1. """This is a simple example of running a wsgi application with eventlet.
  2. For a more fully-featured server which supports multiple processes,
  3. multiple threads, and graceful code reloading, see:
  4.  
  5. http://pypi.python.org/pypi/Spawning/
  6. """
  7.  
  8. import eventlet
  9. from eventlet import wsgi
  10.  
  11. def hello_world(env, start_response):
  12. if env['PATH_INFO'] != '/':
  13. start_response('404 Not Found', [('Content-Type', 'text/plain')])
  14. return ['Not Found\r\n']
  15. start_response('200 OK', [('Content-Type', 'text/plain')])
  16. return ['Hello, World!\r\n']
  17.  
  18. wsgi.server(eventlet.listen(('', 8090)), hello_world)

SSL方式(Secure Socket Layer)

  1. wsgi.server(eventlet.wrap_ssl(eventlet.listen(('', 8090)),
  2. certfile='cert.crt',
  3. keyfile='private.key',
  4. server_side=True),
  5. hello_world)

流程描述:

服务器开一个socket等待客户端连接;请求来了,服务器会读出传来的数据,然后根据HTTP协议做一些初步的封装,

接着就可以调用事先注册的应用程序了,并将请求的数据塞进去;等响应处理完毕了再把数据通过socket发出去

server参数介绍:

  1. def server(sock, # Server socket, must be already bound to a port and listening(IP和端口并开启监听).
    site, # WSGI application function(事件处理函数,发送start_response响应头然后返回响应内容)
    log=None, # File-like object that logs should be written to.If not specified, sys.stderr is used.(日志处理,默认为sys.stderr用来重定向标准错误信息的)
    environ=None, # Additional parameters that go into the environ dictionary of every request(每次请求的参数,写入一个字典中)
    max_size=None, #Maximum number of client connections opened at any time by this server.(默认为1024)
    max_http_version=DEFAULT_MAX_HTTP_VERSION, # Set to "HTTP/1.0" to make the server pretend it only supports HTTP 1.0.
                                    # This can help with applications or clients that don't behave properly using HTTP 1.1.(HTTP协议版本,默认为HTTP/1.1)
    protocol=HttpProtocol, # Protocol class.(协议类,默认为HttpProtocol)
    server_event=None, # Used to collect the Server object(搜集服务器对象信息)
    minimum_chunk_size=None, # Minimum size in bytes for http chunks.  This can be used to improve performance of applications which yield many small strings, though
                          # using it technically violates the WSGI spec. This can be overridden on a per request basis by setting environ['eventlet.minimum_write_chunk_size'].
                          # 设置最小的Chunk大小,可以通过设置environ['eventlet.minimum_write_chunk_size']来覆盖.Chunk表示服务器发送给客户端的分块传输编码(Chunked transfer encoding)
    log_x_forwarded_for=True, # If True (the default), logs the contents of the x-forwarded-for header in addition to the actual client ip address in the 'client_ip' field of the log line.
                          # 默认为True,记录客户端IP日志,X-Forwarded-For(XFF)是用来识别通过HTTP代理或负载均衡方式连接到Web服务器的客户端最原始的IP地址的HTTP请求头字段。
    custom_pool=None, # A custom GreenPool instance which is used to spawn client green threads.If this is supplied, max_size is ignored.(协程池,如果启用则可以忽略前面的max_size参数)
    keepalive=True, # If set to False, disables keepalives on the server; all connections will be closed after serving one request.(控制客户端连接数是否保持alive)
    log_output=True, # A Boolean indicating if the server will log data or not.(确定服务端是否输出日志)
    log_format=DEFAULT_LOG_FORMAT, # A python format string that is used as the template to generate log lines.(日志输出格式)
    url_length_limit=MAX_REQUEST_LINE, # A maximum allowed length of the request url. If exceeded, 414 error is returned.(最大的url长度限制,默认为8192)
    debug=True, # True if the server should send exception tracebacks to the clients on 500 errors.If False, the server will respond with empty bodies.(是否发送调式信息给客户端)
    socket_timeout=None, # Timeout for client connections' socket operations. Default None means wait forever.(Socket超时时间设置,单位是秒)
    capitalize_response_headers=True) # Normalize response headers' names to Foo-Bar(是否标准化相应头)

WSGI学习系列eventlet.wsgi的更多相关文章

  1. WSGI学习系列WebOb

    1. WSGI Server <-----> WSGI Middleware<-----> WSGI Application  1.1 WSGI Server wsgi ser ...

  2. WSGI学习系列多种方式创建WebServer

    def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) ...

  3. WSGI学习系列Paste

    Paste has been under development for a while, and has lots of code in it. The code is largely decoup ...

  4. WSGI学习系列WSME

    Introduction Web Services Made Easy (WSME) simplifies the writing of REST web services by providing ...

  5. WSGI学习系列Pecan

    Pecan Introduce Pecan是一个轻量级的基于Python的Web框架, Pecan的目标并不是要成为一个“full stack”的框架, 因此Pecan本身不支持类似Session和D ...

  6. Python——eventlet.wsgi

    eventlet 的 wsgi 模块提供了一种启动事件驱动的WSGI服务器的简洁手段,可以将其作为某个应用的嵌入web服务器,或作为成熟的web服务器,一个这样的web服务器的例子就是 Spawnin ...

  7. 分布式学习系列【dubbo入门实践】

    分布式学习系列[dubbo入门实践] dubbo架构 组成部分:provider,consumer,registry,monitor: provider,consumer注册,订阅类似于消息队列的注册 ...

  8. Entity Framework Code First学习系列目录

    Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012, ...

  9. WCF学习系列汇总

    最近在学习WCF,打算把一整个系列的文章都”写“出来,包括理论和实践,这里的“写”是翻译,是国外的大牛写好的,我只是搬运工外加翻译.翻译的不好,大家请指正,谢谢了.如果觉得不错的话,也可以给我点赞,这 ...

随机推荐

  1. Trie(前缀树/字典树)及其应用

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  2. Makefile经典教程

    转自:http://blog.csdn.net/ruglcc/article/details/7814546/       makefile很重要 什么是makefile?或许很多Winodws的程序 ...

  3. Python-连接Redis并操作

    首先开启redis的外连 sch01ar@ubuntu:~$ sudo vim /etc/redis/redis.conf 把bind 127.0.0.1这行注释掉 然后重启redis sudo /e ...

  4. Mac搭建nginx+rtmp服务器

    nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择,本人在网上整理了安装流程,分享给大家并且作备忘. 一.安装Homebrow 已经安装了brow的可以直接跳过这 ...

  5. python 基础 列表 字符串转换

    1. 字符串转列表 str1 = "hi hello world" print(str1.split(" "))输出:['hi', 'hello', 'worl ...

  6. 每天一道算法题(4)——O(1)时间内删除链表节点

    1.思路 假设链表......---A--B--C--D....,要删除B.一般的做法是遍历链表并记录前驱节点,修改指针,时间为O(n).删除节点的实质为更改后驱指针指向.这里,复制C的内容至B(此时 ...

  7. 第一章 –– Java基础语法

    第一章 –– Java基础语法 span::selection, .CodeMirror-line > span > span::selection { background: #d7d4 ...

  8. PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)

    1. 首先要得到该数据库中有哪些表,所用函数 mysql_list_tables(),然后可以将获取的所有表名存到一个数组.----------------该函数由于被弃用   用show table ...

  9. 使用Realsense D400 camera系列跑rgbdslamv2

    Ubuntu16.04,kinetic 在之前写的博文<如何使用ROS查找rgbdslam代码包框架的输入>中提到,一开始不知道整体框架,只用感性认识去跑rgbdslamv2的包,是一个天 ...

  10. (PHP)redis Zset(有序集合 sorted set)操作

    /** * * Zset操作 * sorted set操作 * 有序集合 * sorted set 它在set的基础上增加了一个顺序属性,这一属性在修改添加元素的时候可以指定,每次指定后,zset会自 ...