CherryPy3和IIS 6.0

hsi*_*mah 4 python iis-6 cherrypy isapi-wsgi

我有一个使用Cherrypy框架的小型Python Web应用程序.我绝不是网络服务器的专家.

我在我们的Ubuntu服务器上使用mod_python让Cherrypy使用Apache.但是,这一次,我必须使用Windows 2003和IIS 6.0来托管我的网站.

该网站作为一个独立的服务器完美运行 - 我在使IIS运行时迷失了方向.我在过去的一天里用谷歌搜索并盲目地尝试任何一切,以便让它运行起来.

我已经安装了网站告诉我的所有各种工具(Python 2.6,CherrpyPy 3,ISAPI-WSGI,PyWin32),并且已经阅读了所有文档.这篇博客最有帮助:

http://whatschrisdoing.com/blog/2008/07/10/turbogears-isapi-wsgi-iis/

但我仍然迷失了运行我的网站所需要的东西.我找不到任何详尽的例子或者如何开始.我希望这里有人可以提供帮助!

干杯.

Jas*_*mbs 10

我在IIS网站后面运行CherryPy.有几个技巧可以让它发挥作用.

  1. 作为IIS工作进程标识运行时,您将不具有与从用户进程运行站点时相同的权限.事情会破裂.特别是,任何想要写入文件系统的东西如果没有一些调整就可能无法工作.
  2. 如果您正在使用setuptools,则可能需要使用-Z选项安装组件(解压缩所有鸡蛋).
  3. 使用win32traceutil来追踪问题.确保在钩子脚本中导入win32traceutil.然后,当您尝试访问该网站时,如果出现任何问题,请确保将其打印到标准输出,它将被记录到跟踪实用程序.使用'python -m win32traceutil'查看跟踪的输出.

了解运行ISAPI应用程序的基本过程非常重要.我建议首先在ISAPI_WSGI下运行一个hello-world WSGI应用程序.这是一个钩子脚本的早期版本,我用它来验证我是否正在使用CherryPy来处理我的Web服务器.

#!python

"""
Things to remember:
easy_install munges permissions on zip eggs.
anything that's installed in a user folder (i.e. setup develop) will probably not work.
There may still exist an issue with static files.
"""


import sys
import os
import isapi_wsgi

# change this to '/myapp' to have the site installed to only a virtual
#  directory of the site.
site_root = '/'

if hasattr(sys, "isapidllhandle"):
    import win32traceutil

appdir = os.path.dirname(__file__)
egg_cache = os.path.join(appdir, 'egg-tmp')
if not os.path.exists(egg_cache):
    os.makedirs(egg_cache)
os.environ['PYTHON_EGG_CACHE'] = egg_cache
os.chdir(appdir)

import cherrypy
import traceback

class Root(object):
    @cherrypy.expose
    def index(self):
        return 'Hai Werld'

def setup_application():
    print "starting cherrypy application server"
    #app_root = os.path.dirname(__file__)
    #sys.path.append(app_root)
    app = cherrypy.tree.mount(Root(), site_root)
    print "successfully set up the application"
    return app

def __ExtensionFactory__():
    "The entry point for when the ISAPIDLL is triggered"
    try:
        # import the wsgi app creator
        app = setup_application()
        return isapi_wsgi.ISAPISimpleHandler(app)
    except:
        import traceback
        traceback.print_exc()
        f = open(os.path.join(appdir, 'critical error.txt'), 'w')
        traceback.print_exc(file=f)
        f.close()

def install_virtual_dir():
    import isapi.install
    params = isapi.install.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 = [
        isapi.install.ScriptMapParams(Extension="*", Flags=0)
    ]
    vd = isapi.install.VirtualDirParameters(
        Server="CherryPy Web Server",
        Name=site_root,
        Description = "CherryPy Application",
        ScriptMaps = sm,
        ScriptMapUpdate = "end",
        )
    params.VirtualDirs = [vd]
    isapi.install.HandleCommandLine(params)

if __name__=='__main__':
    # If run from the command-line, install ourselves.
    install_virtual_dir()
Run Code Online (Sandbox Code Playgroud)

这个脚本做了几件事.它(a)充当安装程序,将其自身安装到IIS [install_virtual_dir]中,(b)包含IIS加载DLL [__ExtensionFactory__]时的入口点,以及(c)它创建ISAPI处理程序使用的CherryPy WSGI实例[setup_application] ].

如果将其放在\ inetpub\cherrypy目录中并运行它,它将尝试将自身安装到名为"CherryPy Web Server"的IIS网站的根目录.

您也可以查看我的生产网站代码,该代码已将所有这些代码重构为不同的模块.