【问题标题】:Setting up Python on Windows/ Apache?在 Windows/Apache 上设置 Python?
【发布时间】:2010-10-01 17:36:28
【问题描述】:

我想获得一个简单的 Python“hello world”网页脚本以在 Windows Vista/Apache 上运行,但遇到了不同的问题。我正在使用 WAMP。我已经安装了mod_python 并且模块显示了,但我不太确定我应该做什么,例如http.conf(在访问http://localhost/myfolder/index.py 时,诸如 AddHandler mod_python .py 之类的东西要么将我带到一个未找到的文件,要么是一个被禁止的,或未找到模块的错误)。我可以让mod_python.publisher 工作,但我“想要”这个/需要这个吗?

谁能帮忙?

谢谢!

【问题讨论】:

  • 我如何配置 django + apache 可能很有用,在 StackOverflow 上:here

标签: python wamp mod-python


【解决方案1】:

远离mod_python。一种常见的误导性想法是 mod_python 类似于 mod_php,但用于 python。那不是真的。 Wsgi 是运行python web 应用程序的标准,由PEP 333 定义。所以请改用mod_wsgi

或者,使用一些具有服务器的 Web 框架。 Cherrypy 的特别好。您将能够独立运行您的应用程序,也可以通过mod_wsgi 运行您的应用程序。

使用cherrypy的Hello World应用示例:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

application = HelloWorld()
if __name__ == '__main__':
    cherrypy.engine.start()
    cherrypy.engine.block()

很简单吧?直接在 python 上运行这个应用程序将启动一个网络服务器。给它配置mod_wsgi 会让它在apache里面运行。

【讨论】:

  • 在 Apache 上运行 mod_wsgi 应用程序相当简单。我绝对推荐使用 CherryPy,它适用于 Apache 和 mod_wsgi,即使它有自己的轻量级服务器。
  • 用最简单的方式告诉你如何从apache运行python文件
  • 我收到了这个错误:Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, admin@localost.com and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Apache/2.2.4 (Win32) PHP/5.2.1 Server at localhost Port 80
  • 与@MarthaJames 相同的错误并且日志显示系统无法在d:/wamp/www/test.py 找到文件,但它存在于那里
【解决方案2】:

你不需要 mod_python 在网络上运行 Python 代码,你可以使用简单的 CGI 编程来运行你的 python 代码,使用以下链接中的说明:http://www.imladris.com/Scripts/PythonForWindows.html

这应该会为您提供一些使用 CGI 启用 Python 所需的配置选项,并且 Google 搜索应该会为您提供有关如何在其中编程等的大量其他信息。

如果您想要一个稍微“友好”的界面,或者对请求本身进行更多控制,Mod_python 会很有用。您可以使用它为 Apache 服务器创建请求过滤器和其他内容,并且使用发布者处理程序,您可以获得通过 python 处理网页请求的更简单方法。

发布者处理程序通过将 URL 映射到 Python 对象/函数来工作。这意味着您可以在您的 python 文件中定义一个名为 'foo' 的函数,并且对http://localhost/foo 的任何请求都会自动调用该函数。更多信息在这里:http://www.modpython.org/live/current/doc-html/hand-pub-alg-trav.html

至于 Apache 配置以使事情正常运行,这样的东西应该可以很好地为您服务

<Directory /var/www/html/python/>
  SetHandler mod_python
  PythonHandler mod_python.publisher
  PythonDebug On
</Directory>

如果您将 /var/www/html/ 设置为 Web 服务器的根目录并在其中的 python/ 目录中有一个名为 index.py 的文件,那么对 http://localhost/python/foo 的任何请求都应调用 foo() 函数在 index.py 中,如果不存在则返回 404 失败。

【讨论】:

    【解决方案3】:

    AddHandler mod_python .py

    你设置了'PythonHandler'吗?

    如今,考虑使用 WSGI 代替原生 mod-python 接口以获得更广泛的部署选项。通过 mod-python 的 WSGI 支持,或者更好的是 mod-wsgi。 (通过例如 wsgiref 的 CGI 也可以正常工作,并且很容易在您不关心其垃圾性能的开发环境中设置。)

    【讨论】:

      猜你喜欢
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 2017-07-15
      • 2016-05-21
      • 2015-07-01
      • 1970-01-01
      相关资源
      最近更新 更多