CherryPy3与IIS 6.0

4 投票
2 回答
2042 浏览
提问于 2025-04-15 15:41

我有一个小的Python网页应用,使用的是Cherrypy框架。我并不是网络服务器方面的专家。

我在我们的Ubuntu服务器上成功地用Apache和mod_python让Cherrypy运行起来了。不过这次,我需要在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/

但是我还是不知道如何才能让我的网站运行起来。我找不到任何详细的例子或教程来入手。我希望这里有人能帮帮我!

谢谢大家。

2 个回答

10

我在我的IIS网站后面运行CherryPy。要让它正常工作,有几个小技巧。

  1. 当你以IIS工作进程的身份运行时,你的权限和以个人用户身份运行网站时是不一样的。这会导致一些问题。特别是,任何想要写入文件系统的操作,可能都需要一些调整才能正常工作。
  2. 如果你在使用setuptools,可能需要用-Z选项来安装你的组件(这个选项会解压所有的包)。
  3. 使用win32traceutil来追踪问题。在你的钩子脚本中确保导入win32traceutil。然后,当你尝试访问网站时,如果出现任何错误,确保这些错误信息能打印到标准输出,这样就会被记录到追踪工具里。使用'python -m win32traceutil'来查看追踪输出。

了解如何让ISAPI应用程序运行的基本过程是很重要的。我建议你先让一个简单的hello-world WSGI应用程序在ISAPI_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()

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

如果你把这个放在你的\inetpub\cherrypy目录下并运行,它会尝试将自己安装到名为“CherryPy Web Server”的IIS网站根目录下。

你也可以查看我的生产网站代码,它将所有这些重构成了不同的模块。

2

好的,我搞定了。感谢Jason和他提供的所有帮助。我需要调用

cherrypy.config.update({
  'tools.sessions.on': True
})
return cherrypy.tree.mount(Root(), '/', config=path_to_config)

我之前在配置文件的[/]部分有这个设置,但不知道为什么它不喜欢那样。现在我可以让我的网页应用正常运行了——接下来我想弄明白为什么需要这个配置更新,以及为什么它不喜欢我之前的配置文件……

撰写回答