Python在Apache上 - 获取404错误
我在找到解决方案后编辑了这个问题... 我想了解为什么这个解决方案有效,而我的方法却不行?
这可能是个傻问题。我试着搜索了一些相关的问题... 可是没有找到答案。
我正在运行 Apache/2.2.11 (Ubuntu) DAV/2 SVN/1.5.4 PHP/5.2.6-3ubuntu4.5,带有 Suhosin-Patch mod_python/3.3.1 Python/2.6.2
我有一个叫做 test.py 的脚本
#! /usr/bin/python
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "hello world"
把它当成可执行文件运行是可以的...
/var/www$ ./test.py
Content-Type: text/html
hello world
但是当我在浏览器里输入 http://localhost/test.py 时,我却得到了一个 404 错误。
我漏掉了什么吗?
我使用了这个资源来启用 Apache 上的 Python 解析。 http://ubuntuforums.org/showthread.php?t=91101
在同一个讨论串中... 下面的代码有效.. 为什么呢? #!/usr/bin/python
import sys
import time
def index(req):
# Following line causes error to be sent to browser
# rather than to log file (great for debug!)
sys.stderr = sys.stdout
#print "Content-type: text/html\n"
#print """
blah1 = """<html>
<head><title>A page from Python</title></head>
<body>
<h4>This page is generated by a Python script!</h4>
The current date and time is """
now = time.gmtime()
displaytime = time.strftime("%A %d %B %Y, %X",now)
#print displaytime,
blah1 += displaytime
#print """
blah1 += """
<hr>
Well House Consultants demonstration
</body>
</html>
"""
return blah1
2 个回答
0
如果你想让你的代码正常运行,可以把你的网页服务器设置成把 .pl 文件当作 CGI 脚本来处理,而不是使用 mod_python.publisher。
在 Apache 的配置文件中,你可以这样设置:
AddHandler cgi-script .py
如果你不想遇到 403 错误,可以修改:
<Directory /path/to/www/yourfile.py>
Options +ExecCGI
</Directory>
4
我觉得mod_python的发布处理器是希望在test.py文件里找到一个叫index
的函数。
你可以通过在网址的最后加上函数名来调用test.py里的任何函数,比如说http://localhost/test.py/any_func
,如果你没有指定函数名,默认会调用index
这个函数。