list+mod_python at joreybump.com
Thu Apr 15 13:50:16 EST 2004
Eustaquio Rangel de Oliveira Jr. wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Jorey Bump wrote: > > |Assuming you're using publisher and your DocumentRoot is /var/www/html, > |you can do this: > > | <Directory /var/www/html> > | AddHandler python-program .py > | PythonHandler mod_python.publisher > | PythonDebug On > | </Directory> > > |That would allow you to use .py files almost anywhere. Specify a > |subdirectory if you want to limit it. There are no conflicts with > |mod_php. > > Hey, thanks for your answer. I did this, and when I try to run test.py: > > from mod_python import apache > > def handler(req): > req.content_type = "text/plain" > req.send_http_header() > req.write("Hello, world!<br>") > r = range(0,10) > for n in r: > req.write(str(n)+"<br>") > return apache.OK > > I got a: > > Not Found > The requested URL /test.py was not found on this server. > Apache/1.3.29 Server at taq.localhost Port 80 It's much simpler than that. Try this in test.py: def hello(req): x = "Hello, world!" return x Then go to: http://localhost/test.py/hello You'll see the text, and the server will send it as Content-Type: text/plain; charset=ISO-8859-1 Now add this function: def hellohtml(req): x = """<html> <head> <title>Hello, World!</title> </head> <body> <h1>Hello, World!</h1> </body> </html> """ return x Go to: http://localhost/test.py/hellohtml It's now HTML, with the appropriate header: Content-Type: text/html; charset=ISO-8859-1 Finally, add a function that takes an argument from the URL: def hellohtmlarg(req, arg): x = """<html> <head> <title>%s</title> </head> <body> <h1>%s</h1> </body> </html> """ % (arg, arg) return x Go to: http://localhost/test.py/hellohtmlarg?arg=Hello,%20World! You can see that you generally don't need to worry about sending headers or importing the apache module with mod_python.publisher. Just construct your page as a string and return it. Publisher does the rest for you. If you need to set special headers, you can do it like this: req.headers_out['Pragma'] = 'no-cache' req.headers_out['Cache-Control'] = 'no-cache' req.headers_out['Expires'] = '-1' But I prefer to do it in httpd.conf or .htaccess, myself: Header set Pragma "no-cache" Header set Cache-Control "no-cache" Header set Expires "-1" It's a matter of taste.