Mod_python 不产生输出
刚刚使用 Python 2.4.3 在 CentOS 5 (Apache 2.2.3) 服务器上安装并配置了 mod_python 3.2.8。 Apache 可以很好地加载它。
我激活了 mpinfo 测试页面并且它有效。所以我用以下代码编写了一个简单的“Hello World”:
from mod_python import apache
def handler(req):
req.content_type = 'text/plain'
req.write("Hello World!")
req.flush()
return apache.OK
它输出一个空白页面,没有文本,也没有源代码。如果我有意识地创建一个语法错误,我会在 URL 上得到错误输出,例如(当我在“def”之前放置一个空格时):
Mod_python error: "PythonHandler mod_python.cgihandler"
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
result = object(req)
File "/usr/lib/python2.4/site-packages/mod_python/cgihandler.py", line 96, in handler
imp.load_module(module_name, fd, path, desc)
File "/var/www/vhosts/localhost/httpdocs/mptest.py", line 3
def handler(req):
^
SyntaxError: invalid syntax
我花了大约五个小时浏览不同的教程、常见问题解答和故障排除指南,但找不到这个exakt问题的描述。
您认为问题/原因可能是什么?
编辑:这是该站点的 Apache 配置...
<Directory />
Options FollowSymLinks
AllowOverride None
AddHandler mod_python .py
PythonHandler mptest
PythonDebug On
</Directory>
编辑 2: 啊,我忘记提及的另一件事是我打算使用 mod_python 来编写 Apache 扩展。应用程序本身是用 PHP 编写的,但我需要在服务器上进行一些安全调整:)
Just installed and configured mod_python 3.2.8 on a CentOS 5 (Apache 2.2.3) server with Python 2.4.3. It is loaded fine by Apache.
I activated the mpinfo test page and it works. So I wrote a simple "Hello World" with the following code:
from mod_python import apache
def handler(req):
req.content_type = 'text/plain'
req.write("Hello World!")
req.flush()
return apache.OK
It outputs a blank page, with no text and no source. If I consciously create a syntax error I get the error output on the URL, for example (when I put a space before "def"):
Mod_python error: "PythonHandler mod_python.cgihandler"
Traceback (most recent call last):
File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
result = object(req)
File "/usr/lib/python2.4/site-packages/mod_python/cgihandler.py", line 96, in handler
imp.load_module(module_name, fd, path, desc)
File "/var/www/vhosts/localhost/httpdocs/mptest.py", line 3
def handler(req):
^
SyntaxError: invalid syntax
I have spent about five hours browsing different tutorials, faqs and trouble shooting guides but can't find a description of this exakt issue.
What do you think could be the issue/cause?
EDIT: Here is the Apache configuration for the site...
<Directory />
Options FollowSymLinks
AllowOverride None
AddHandler mod_python .py
PythonHandler mptest
PythonDebug On
</Directory>
EDIT 2: Ah, another thing I forgot to mention is that I intend to use mod_python to write Apache extensions. The application itself is written in PHP but I need to make some security tweeks on the server :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用
mod_python
。一个常见的错误是将
mod_python
视为“mod_php
,but for python”,而这是不正确的。mod_python
更适合编写 apache 扩展,而不是 Web 应用程序。Python Web 应用程序和 Web 服务器之间使用的标准化协议(不仅apache) 是 WSGI。使用它可以确保您可以将应用程序发布到任何符合 wsgi 的 Web 服务器(几乎所有现代 Web 服务器都符合 wsgi)
在 apache 上,使用
mod_wsgi
代替。您的示例在 apache 上使用 wsgi 标准和
mod_wsgi
重写:mywebapp.py
:Apache 配置:
现在只需转到
http://localhost/myapp
> 并且脚本将运行。此外,此根目录下的任何访问(即http://localhost/myapp/stuff/here
)都将由该脚本处理。选择一个 Web 框架是个好主意。 CherryPy。 塔架。 Django。它们让事情变得更加容易。
一个不错的网站是 wsgi.org
Don't use
mod_python
.A common mistake is to take
mod_python
as "mod_php
, but for python" and that is not true.mod_python
is more suited to writing apache extensions, not web applications.The standartized protocol to use between python web applications and web servers (not only apache) is WSGI. Using it ensures that you can publish your application to any wsgi-compliant webserver (almost all modern web servers are wsgi-compliant)
On apache, use
mod_wsgi
instead.Your example rewritten using the wsgi standard and
mod_wsgi
on apache:mywebapp.py
:Apache configuration:
Now just go to
http://localhost/myapp
and the script will run. Additionally, any access under this root (i.e.http://localhost/myapp/stuff/here
) will be handled by this script.It's a good idea to choose a web framework. CherryPy. Pylons. Django. They make things even easier.
A good website to look at is wsgi.org
您最初的问题是正在调用 mod_python.cgihandler 来处理请求。这意味着您的 Python 脚本文件被解释为 CGI 脚本。因此,难怪它不会返回任何内容。
您可能在启用 mod_python.cgihandler 的 Apache 配置中存在冲突的定义。
Your original problem is that mod_python.cgihandler is being called to handle the request. This means your Python script file is being interpreted as a CGI script. Thus, no wonder it doesn't return anything.
You likely have conflicting definition in your Apache configuration which is enabling the mod_python.cgihandler.
为了清楚起见,我做了一个全新的答案......
我决定安装 mod_wsgi 。所以我已经设置了它,当我转到测试文件时,我只看到页面源代码。我还没有花任何时间来查找问题,所以当我解决问题或决定需要更多帮助时,我会回复您:)
谢谢:)
I make a complete new answer for clarity...
I decided to install mod_wsgi instead. So I've set it up and when I go to my testfile I just see the page source. I haven't been spending any time on finding the issue yet, so I'll get back to you when I either solve the problem or decide that I need more help :)
Thank you :)