CherryPy3 和 IIS 6.0

发布于 2024-08-10 06:28:15 字数 600 浏览 3 评论 0原文

我有一个使用 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/

但我仍然不知道运行我的网站需要什么。我找不到任何完整的示例或入门指南。我希望这里有人可以提供帮助!

干杯。

I have a small Python web application using the Cherrypy framework. I am by no means an expert in web servers.

I got Cherrypy working with Apache using mod_python on our Ubuntu server. This time, however, I have to use Windows 2003 and IIS 6.0 to host my site.

The site runs perfectly as a stand alone server - I am just so lost when it comes to getting IIS running. I have spent the past day Googling and blindly trying any and everything to get this running.

I have all the various tools installed that websites have told me to (Python 2.6, CherrpyPy 3, ISAPI-WSGI, PyWin32) and have read all the documentation I can. This blog was the most helpful:

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

But I am still lost as to what I need to run my site. I can't find any thorough examples or how-to's to even start with. I hope someone here can help!

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2024-08-17 06:28:15

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

  1. 当以 IIS 工作进程身份运行时,您将不具有与从用户进程运行站点时相同的权限。事情会破裂。特别是,如果不进行一些调整,任何想要写入文件系统的内容都可能无法工作。
  2. 如果您使用 setuptools,您可能需要使用 -Z 选项安装组件(解压所有 Egg)。
  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()

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

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

也欢迎您查看我的生产网站代码 ,它将所有这些重构为不同的模块。

I run CherryPy behind my IIS sites. There are several tricks to get it to work.

  1. When running as the IIS Worker Process identity, you won't have the same permissions as you do when you run the site from your user process. Things will break. In particular, anything that wants to write to the file system will probably not work without some tweaking.
  2. If you're using setuptools, you probably want to install your components with the -Z option (unzips all eggs).
  3. Use win32traceutil to track down problems. Be sure that in your hook script that you're importing win32traceutil. Then, when you're attempting to access the web site, if anything goes wrong, make sure it gets printed to standard out, it'll get logged to the trace utility. Use 'python -m win32traceutil' to see the output from the trace.

It's important to understand the basic process to get an ISAPI application running. I suggest first getting a hello-world WSGI application running under ISAPI_WSGI. Here's an early version of a hook script I used to validate that I was getting CherryPy to work with my web server.

#!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()

This script does several things. It (a) acts as the installer, installing itself into IIS [install_virtual_dir], (b) contains the entry point when IIS loads the DLL [__ExtensionFactory__], and (c) it creates the CherryPy WSGI instance consumed by the ISAPI handler [setup_application].

If you place this in your \inetpub\cherrypy directory and run it, it will attempt to install itself to the root of your IIS web site named "CherryPy Web Server".

You're also welcome to take a look at my production web site code, which has refactored all of this into different modules.

月棠 2024-08-17 06:28:15

好的,我成功了。感谢杰森和他的所有帮助。我需要调用

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

我在 [/] 下的配置文件中的配置文件,但由于某种原因它不喜欢那样。现在我可以启动并运行我的网络应用程序 - 然后我想我会尝试找出为什么它需要配置更新并且不喜欢我的配置文件......

OK, I got it working. Thanks to Jason and all his help. I needed to call

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

I had this in the config file under [/] but for some reason it did not like that. Now I can get my web app up and running - then I think I will try and work out why it needs that config update and doesn't like the config file I have...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文