简单的wsgi应用

流夜未央 posted @ 2011年5月12日 14:46 in 未分类 , 3029 阅读

这个模块实现了一个简单HTTP 服务(基于BaseHTTPServer),为WSGI应用提供服务。每个服务实例在一个给定的主机和端口上为一个单一的WSGI应用提供服务。如果你想 要在一个单一的主机和端口上为多个应用提供服务,你可以创建一个WSGI应用,解析PATH_INFO为每个请求选择调用的应用。(例如,使用 wsgiref.util中的shift_path_info()函数。)

make_server(host, port, app [, server_class=WSGIServer [, handler_class=WSGIRequestHandler]])

创建一个新的监听在host和port上的WSGI服务,为app接收连接。返回值是一个提供的server_class实例,并且使用指定的handler_class处理请求。app必须是一个定义在PEP 333中的WSGI应用对象。

例子用法

from wsgiref.simple_server import make_server, demo_app

httpd = make_server('', 8000, demo_app)

print "Serving HTTP on port 8000..."

# Respond to requests until process is killed

# 响应请求直到进程被杀死

httpd.serve_forever()

# Alternative: serve one request, then exit

# 二择一:服务一个请求,然后退出

##httpd.handle_request()

demo_app(environ, start_response)

这个函数是一个小的但完整的WSGI应用,返回一个包含“Hello world!”信息的文本页和在environ参数中提供的key/value对的一个列表。它的作用是检查一个WSGI服务(如wsgiref.simple_server)能正确地运行一个简单的WSGI应用。

Class WSGIServer(server_address, RequestHandlerClass)

创建WSGIServer实例。server_address应该是(host,port)元组,RequestHandlerClass应该是BaseHTTPServer的子类。BaseHTTPRequestHandler将被用来处理请求。

你通常地不需要调用构造函数,同样地make_server()函数可以为你处理所有详情。

WSGIServer是BaseHTTPServer.HTTPServer的子类,所以所有它的方法(如server_forever()和handle_request())可用。WSGIServer也提供这些特定的WSGI方法:

set_app(application)
设置回调application作为WSGI应用接收请求。

get_app()
返回当前设置地应用回调。

通常地,尽管,你不需要使用这些额外的方法,像set_app()通常被make_server()调用一样,get_app()主要地为请求处理实例的好处存在。

Class WSGIRequestHandler(request, client_address, server)
为给定的request(如,一个socket),client_address(一个(host,port)元组),和server(WSGIServer实例)创建一个HTTP处理程序。

你不需要直接地创建这个类的实例;它被自动地创建依照WSGIServer对象需要的。你可以,尽管,这个类子类作为handler_class提供给make_server()函数。子类中重载一些可能相关的方法:

get_environ()
为请求返回一个包含WSGI环境的字典。缺省实现复制WSGIServer对象的base_environ字典属性的内容,然后添加从HTTP请求中派生的各种头。每次调用这个方法,应该返回一个新的包含所有PEP 333中规范的相关的CGI环境变量的字典。

get_stderr()
返回作为wsgi.errors流使用的对象。缺省实现仅返回sys.stderr()。

handle()
处理HTTP请求。缺省实现用一个wsgiref.handlers类创建一个处理程序实例,实现实际的WSGI应用接口。

Avatar_small
deep cleaning dubai 说:
2019年10月24日 22:06

Encounter in heavy cleaning tile floors is among the most important explanations why business proprietors and home owners trust expert cleaners and you ought to too. It's not enough to get the cleansing tools as well as products in the store, but you should have in-depth understanding of what products to make use of for your unique type associated with tile. For example, what functions for ceramic tiles might not work about the granite tiles. If you are using the incorrect cleaner, it may stain as well as permanently harm your tiles. You might have to completely replace your own tile floors, which can cost you lots of money.

Avatar_small
House Cleaning Servi 说:
2021年8月26日 14:17

Aided by the a doormat in the home which in your own home to fight aided by the dust particles. Normally for any home there needs to be Doormats to fix up typically the dust quite simply. In-order to cure dust right from outside, environment holes not to mention windows, you need to use the doormats for the purpose of preventive precautions. Also you're able to clean typically the dirt not to mention remove the whole set of unnecessary things of computer.

Avatar_small
cleaning services du 说:
2021年9月18日 13:13

You may need a spray bottle to combine water plus baking coke. Now, add vinegar in buying it. A compact reaction will administer place when white white vinegar contains acetic plaque created by sugar and waters, and this baking soda is made up of salt. At this moment, add one or two drops with oil plus shake them properly and make use of it.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter