python on apache - getting 404
I edited this question after i found a solution... i need to understand why the solution worked instead of my method?
This is likely to be a silly question. I tried searching other questions that are related... but to no avail.
i am running Apache/2.2.11 (Ubuntu) DAV/2 SVN/1.5.4 PHP/5.2.6-3ubuntu4.5 with Suhosin-Patch mod_python/3.3.1 Pyt开发者_如何学Pythonhon/2.6.2
i have a script called test.py
#! /usr/bin/python
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "hello world"
running it as an executable works...
/var/www$ ./test.py
Content-Type: text/html
hello world
when i run http://localhost/test.py i get a 404 error.
What am i missing?
i used this resource to enable python parsing on apache. http://ubuntuforums.org/showthread.php?t=91101
From that same thread... the following code worked.. why? #!/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
I think the mod_python's Publisher Handler is expecting to find index
function in test.py.
You can call any function inside test.py by putting it in the end of URL, e.g. http://localhost/test.py/any_func
, the default function being index
if none given.
If you want to have your code working as is, you can also configure your web server to handle .pl as cgi script instead of using mod_python.publisher
on apache config:
AddHandler cgi-script .py
and if you don't want an error 403, edit:
<Directory /path/to/www/yourfile.py>
Options +ExecCGI
</Directory>
精彩评论