在 Windows/Apache 上设置 Python?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/449055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting up Python on Windows/ Apache?
提问by Philipp Lenssen
I want to get a simple Python "hello world" web page script to run on Windows Vista/ Apache but hit different walls. I'm using WAMP. I've installed mod_python
and the module shows, but I'm not quite sure what I'm supposed to do in e.g. http.conf (things like AddHandler mod_python .py either bring me to a file not found, or a forbidden, or module not found errors when accessing http://localhost/myfolder/index.py). I can get mod_python.publisher
to work but do I "want" this/ need this?
我想获得一个简单的 Python“hello world”网页脚本,以便在 Windows Vista/Apache 上运行,但遇到了不同的问题。我正在使用 WAMP。我已经安装mod_python
并且模块显示,但我不太确定我应该在例如 http.conf 中做什么(像 AddHandler mod_python .py 之类的东西要么把我带到一个未找到的文件,或者一个禁止的,或者模块访问http://localhost/myfolder/index.py时未发现错误)。我可以mod_python.publisher
上班,但我“想要”这个/需要这个吗?
Can anyone help?
任何人都可以帮忙吗?
Thanks!
谢谢!
回答by nosklo
Stay away from mod_python
. One common misleading idea is that mod_python
is like mod_php
, but for python. That is not true. Wsgiis the standard to run python web applications, defined by PEP 333. So use mod_wsgi
instead.
远离mod_python
. 一种常见的误导性想法是mod_python
like mod_php
, but for python 。那不是真的。Wsgi是运行 Python Web 应用程序的标准,由PEP 333定义。所以mod_wsgi
改用。
Or alternatively, use some web framework that has a server. Cherrypy's one is particulary good. You will be able to run your application both standalone and through mod_wsgi
.
或者,使用一些具有服务器的 Web 框架。Cherrypy的一个特别好。您将能够独立运行您的应用程序,也可以通过mod_wsgi
.
An example of Hello World application using cherrypy:
使用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()
Very easy huh? Running this application directly on python will start a webserver. Configuring mod_wsgi
to it will make it run inside apache.
很容易吧?直接在 python 上运行这个应用程序将启动一个网络服务器。对其进行配置mod_wsgi
将使其在 apache 中运行。
回答by Daniel Bruce
You do not NEED mod_python to run Python code on the web, you could use simple CGI programming to run your python code, with the instructions in the following link: http://www.imladris.com/Scripts/PythonForWindows.html
您不需要 mod_python 在网络上运行 Python 代码,您可以使用简单的 CGI 编程来运行您的 Python 代码,使用以下链接中的说明:http: //www.imladris.com/Scripts/PythonForWindows.html
That should give you some of the configuration options you need to enable Python with CGI, and a google search should give you reams of other info on how to program in it and such.
这应该会为您提供使用 CGI 启用 Python 所需的一些配置选项,谷歌搜索应该会为您提供有关如何在其中编程等的大量其他信息。
Mod_python is useful if you want a slightly more "friendly" interface, or more control over the request itself. You can use it to create request filters and other things for the Apache server, and with the publisher handler you get a simpler way of handling webpage requests via python.
如果你想要一个稍微“友好”的界面,或者对请求本身有更多的控制,Mod_python 很有用。您可以使用它来为 Apache 服务器创建请求过滤器和其他内容,并且通过发布者处理程序,您可以获得一种通过 python 处理网页请求的更简单的方法。
The publisher handler works by mapping URLs to Python objects/functions. This means you can define a function named 'foo' in your python file, and any request to http://localhost/foowould call that function automatically. More info here: http://www.modpython.org/live/current/doc-html/hand-pub-alg-trav.html
发布者处理程序通过将 URL 映射到 Python 对象/函数来工作。这意味着您可以在 python 文件中定义一个名为 'foo' 的函数,任何对http://localhost/foo 的请求都会自动调用该函数。更多信息:http: //www.modpython.org/live/current/doc-html/hand-pub-alg-trav.html
As for the Apache config to make things work, something like this should serve you well
至于使事情正常进行的 Apache 配置,这样的事情应该很适合您
<Directory /var/www/html/python/>
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
If you have /var/www/html/ set up as your web server's root and have a file called index.py in the python/ directory in there, then any request to http://localhost/python/fooshould call the foo() function in index.py, or fail with a 404 if it doesn't exist.
如果您将 /var/www/html/ 设置为您的 Web 服务器的根目录,并且在其中的 python/ 目录中有一个名为 index.py 的文件,那么对http://localhost/python/foo 的任何请求都应该调用 foo () 函数在 index.py 中,或者如果它不存在则以 404 失败。
回答by bobince
AddHandler mod_python .py
AddHandler mod_python .py
Have you set 'PythonHandler'?
你设置了'PythonHandler'吗?
These days, consider using WSGI instead of native mod-python interfaces for more wide-ranging deployment options. Either through mod-python's WSGI support, or, maybe better, mod-wsgi. (CGI via eg. wsgiref will also work fine and is easy to get set up on a development environment where you don't care about its rubbish performance.)
现在,考虑使用 WSGI 而不是本机 mod-python 接口以获得更广泛的部署选项。通过 mod-python 的 WSGI 支持,或者,也许更好,通过 mod-wsgi。(通过例如 wsgiref 的 CGI 也可以正常工作,并且很容易在您不关心其垃圾性能的开发环境中进行设置。)