2011-04-11 58 views
1

我有一个文件test.py/var/www和我在Ubuntu 10.10上使用apache2。我已经mod_python安装和配置/etc/apache2/sites-available/defaultpython在apache2上的404错误

<Directory /var/www/> 
    Options Indexes FollowSymLinks MultiViews 
    AllowOverride AuthConfig 
    Order allow,deny 
    allow from all 

    AddHandler mod_python .py 
    PythonHandler mod_python.publisher 
    PythonDebug On 

    # Uncomment this directive is you want to see apache2's 
    # default start page (in /apache2-default) when you go to/
    #RedirectMatch ^/$ /apache2-default/ 
</Directory> 

如果把

def index(req): 
    return "Test successful"; 

然后我得到Test successful

如果我把

#!/usr/bin/env python 
print "Content-Type: text/html" 
print 
print """\ 
<html> 
<body> 
<h2>Hello World!</h2> 
</body> 
</html> 
""" 

我得到404: Not found错误

更多可以发现here

任何任何想法??

问候,

experimentX

+0

如果你把什么?你的问题到底是什么? – Cameron 2011-04-11 07:11:03

+0

@Cameron 404找不到 – 2011-04-11 07:11:37

+0

谢谢!有一个类似的问题试图让下载的Python脚本运行(我的第一个Python遇到),这个问题让我走向了正确的方向:'print'与'def index(req):' – 2013-05-29 20:18:00

回答

2

默认情况下,mod_python在尝试处理请求时会查找方法def index(req),否则您可以指定要在URL中调用的函数。请注意,函数必须返回一个特定的值。

所以,你可以这样做

s ="""\ 
<html> 
<body> 
<h2>Hello World!</h2> 
</body> 
</html> 
""" 

def index(): 
    return s 

的URL travelsal的mod_python下是illustrated here.

+1

什么是地狱!也许我应该用其他'mod's作为plaes建议 – 2011-04-11 07:41:49

+0

@experimentX - 尝试阅读我超链接的文章。它在mod_wsgi上有一些很好的细节,可以帮助你开始。顺便说一句,mod_python并不坏,只有它是旧的,可​​能已经死了。 :) – 2011-04-11 07:49:02

1

相反的mod_python我会建议你使用mod_wsgi因为有许多使用WSGI的框架状FlaskCherryPyBottle,等...

很酷的事情有了这些框架,您甚至不必在开发机器上使用Apache,而是可以使用其内置的测试服务器。

+0

仍然,...我来自PHP。如果我能够这样做会感觉更好。 – 2011-04-11 07:20:02

+1

@experimentX:好的,自从WSGI协议和mod_wsgi出来后,mod_python已经被弃用了:http://en.wikipedia.org/wiki/Mod_python#Problems – plaes 2011-04-11 07:23:56

+0

我看到..将安装并启用'mod_wsgi'解决我的问题以及如何禁用'mod_python' – 2011-04-11 07:29:38