主要是介绍协议HTTP、ISAPI、WSGI,以及Web Application Server(集成了Web Server和Application Server,比如:CherryPy库)。最后,介绍了基于WebDav库的应用在CherryPy和IIS环境下的运行示例。本文可以看作是一个CherryPy的背景知识,方便大家理解一些概念。
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information system.[1] HTTP is the foundation of data communication for the World Wide Web.
HTTP是一个网络协议,目标是:客户端给一个URL的请求,HTTP服务器返回一点东西给客户端。
The Internet Server Application Programming Interface (ISAPI) is an N-tier API of Internet Information Services (IIS), Microsoft's collection of Windows-based web server services. The most prominent application of IIS and ISAPI is Microsoft's web server.
ISAPI是HTTP服务器的应用编程接口,用来支持HTTP服务器加载和调用的DLL。即客户端的URL请求最终会被调用到HTTP服务器的DLL中。
The Web Server Gateway Interface (WSGI /ˈwɪzˌɡiː/) defines a simple and universal interface between web servers and web applications or frameworks for the Python programming language.
WSGI用在Python中,可以认为是服务器的应用框架接口协议。有了它用户可以将网页应用独立出来,运行的时候接到任何支持WSGI接口协议的框架中即可。
WSGI的框架如上图所示,wsgiapp运行在wsgi container中,它通过middleware和webserver对接。wsgiapp需要用户自己定义,实现自己的处理逻辑,其它部分则有相应的库来实现,比如:CherryPy。
Web Distributed Authoring and Versioning (WebDAV) is an extension of the Hypertext Transfer Protocol (HTTP) that facilitates collaboration between users in editing and managing documents and files stored on World Wide Web servers. A working group of the Internet Engineering Task Force (IETF) defines WebDAV in RFC 4918.
WebDav扩展了HTTP网络协议,目的是用来支持客户端管理服务器上的文件,同时实现对文件的版本管理。
Web Application Server可以认为是一个服务器的应用模型。
CherryPy is an object-oriented web application framework using the Python programming language. It is designed for rapid development of web applications by wrapping the HTTP protocol but stays at a low level and does not offer much more than what is defined in RFC 2616.
CherryPy是基于Python的Web Application Server,它将Web Server和Application Server合并为同一的server。CherryPy是基于HTTP协议的,同时它内置的CherryPyWSGIServer模块可以支持WSGI接口协议。对CherryPy而言,Web Application只是一个函数、类、或者一个module。
WsgiDAV is a generic WebDAV server written in Python and based on WSGI.
WsgiDAV是一个基于WSGI的服务器上的Python应用,实现了WebDAV协议。
pywin32基于Python,可以让Python代码调用windows的api。
isapi-wsgi基于Python,可以让WSGI接口协议嫁接到ISAPI上,从而支持WSGI应用。
CherryPy本身就是一个支持WSGI的Server,所以可以将WsgiDAV运行在其中作为服务器的Server。下面是一个实例:
from tempfile import gettempdir from wsgidav.fs_dav_provider import FilesystemProvider from wsgidav.version import __version__ from wsgidav.wsgidav_app import DEFAULT_CONFIG, WsgiDAVApp __docformat__ = "reStructuredText" rootpath = r"D:\downloads\ocsync_test" provider = FilesystemProvider(rootpath) config = DEFAULT_CONFIG.copy() config.update({ "provider_mapping": {"/": provider}, "user_mapping": {}, "verbose": 1, "enable_loggers": [], "propsmanager": True, # True: use property_manager.PropertyManager "locksmanager": True, # True: use lock_manager.LockManager "domaincontroller": None, # None: domain_controller.WsgiDAVDomainController(user_mapping) }) app = WsgiDAVApp(config) # Following is from CherryPy from cherrypy import wsgiserver # Create WSGI server and start it. server = wsgiserver.CherryPyWSGIServer( ('192.168.4.54', 8080), app, server_name='server_name') server.start()程序的上半部分是基于WsgiDAV的用来定义一个Web Application, 下半部分是基于CherryPy,用来定义一个Web Application Server。直接运行该脚本即可。
首先,需要配置IIS,一个是根据isapi-wsgi的安装文档,设置兼容IIS6;另一个是要支持ISAPI。实例如下:
from tempfile import gettempdir from wsgidav.fs_dav_provider import FilesystemProvider from wsgidav.version import __version__ from wsgidav.wsgidav_app import DEFAULT_CONFIG, WsgiDAVApp __docformat__ = "reStructuredText" rootpath = r"D:\downloads\ocsync_test" provider = FilesystemProvider(rootpath) config = DEFAULT_CONFIG.copy() config.update({ "provider_mapping": {"/": provider}, "user_mapping": {}, "verbose": 1, "enable_loggers": [], "propsmanager": True, # True: use property_manager.PropertyManager "locksmanager": True, # True: use lock_manager.LockManager "domaincontroller": None, # None: domain_controller.WsgiDAVDomainController(user_mapping) }) app = WsgiDAVApp(config) import isapi_wsgi # The entry points for the ISAPI extension. def __ExtensionFactory__(): return isapi_wsgi.ISAPISimpleHandler(app) if __name__=='__main__': # If run from the command-line, install ourselves. from isapi.install import * params = ISAPIParameters() # Setup the virtual directories - this is a list of directories our # extension uses - in this case only 1. # Each extension has a "script map" - this is the mapping of ISAPI # extensions. sm = [ ScriptMapParams(Extension="*", Flags=0) ] # To serve from root, just set Name="/" vd = VirtualDirParameters(Server="My CherryPy", Name="/", Description = "ISAPI-WSGI ISAPISimpleHandler root Demo", ScriptMaps = sm, ScriptMapUpdate = "replace" ) params.VirtualDirs = [vd] HandleCommandLine(params)上半部分来自于WsgiDAV,同上一个实例。下半部分来自于isapi-wsgi,主要用来生成一个dll,供IIS调用,dll再调用到WsgiDAV应用中。要运行这个实例,首先要在IIS中创建名称为My CherryPy的站点,然后执行python demo.py install将WsgiDAV关联到/下,取消关联则使用python demo.py remove。(假设代码保存在demo.py中,同时命令要运行在管理员权限下)