Python, HTML, and Unix

Hi Experts,

This may be the wrong category for posting my question, or it could be the perfect category to post. I have no idea how difficult my problem is.

I have python 2.3 installed in my server machine. I am trying to create a web page with python script in it. Now this can be a .html page with python embedded in it, or it could be a .py page with html embedded in it. Whatever it is, it's okay. I cannot use python templates like Kid, Spyce, etc., because my System Admin will not install new software into the machine (it's a 64 bit, cluster server, and he is very possessive about it). I have the CentOS operating system (a version of Red Hat Linux Enterprise). I followed the simple instructions given online, and created a page called trial.py with the following code in it:

#!/usr/bin/python
print "Content-type: text/html\n\n"
print "Hello World!!! <br />"
print "this is on the next line because of the line break above"

saved it into the public_html folder, and did $chmod 777 trial.py. Now, when I access this page from the browser (firefox) I get the display on the page to be the exact same code that I had written. That is, the page shows me:

#!/usr/bin/python
print "Content-type: text/html\n\n"
print "Hello World!!! <br />"
print "this is on the next line because of the line break above"

Now, the path to python, is /usr/bin/python. I have verified this with $which python, and I also have some python programs running with crontab that use this path, /usr/bin/python. I changed the file name to trial.cgi, did $chmod 777 trial.cgi, and use firefox to go to it, and I get a 403 Forbidden message saying "You don't have permission to access /~davidfrank/trial.cgi on this server".

So, I tried the other way around. I created a page called new.html, and once again followed the instructions in another online place and wrote the code as:

<html>
<body>
<script language="python2.3">
<!--
print "hello world!"
import string, re
-->
</script>
</body>
</html>

I again saved this into the public_html folder and did $chmod 777 new.html. Now when I access this page from the web browser (firefox), I get nothing on the display. The page is blank.

Please please do help me out. What am I doing wrong?! Is there a tutorial on this. Once again, I cannot use a framework like Kid or Spyce. I have to use the coding that is similar to other scripting languages.

I do not know if the server has Apache or CGI. I am guessing that my server uses Apache since several of the associated web addresses that have the same owners and name servers do use apache. So, now I create this .htaccess file (chmod 777) with the content as:

Options +ExecCGI
AddHandler cgi-script .py

and store it in the public_html folder (this has all the web page files for my username), but no effect. So, I try out the following content:

Options +ExecCGI
AddHandler cgi-script cgi pl

but still no effect. Then finally, I try out:

Options +ExecCGI
AddHandler cgi-script

and no effect after that too. I am at a complete loss now. If someone could please tell me what I am doing wrong or what I should do to get this working, I would truly appreciate it!

Thank you for your time and effort.

Hi Davidfrank,

Yes this is an easy one, but you were along the right lines at the end of the post anyway - you were almost there!

You're going to have to speak to your sysadmin to make a one line change in his httpd.conf for this apache instance.

Basically there's a line says "AllowOverride None" - get him to delete that and do a reload of apache, then your .htaccess should take effect, the important bits in there is the +ExecCGI and the AddHandler cgi-script .py - this ensures that any .py files in the current directory will be run through whatever's specified on the shebang line (#! at the top).

For this to work, you'll go with your first approach, creating trial.py and putting your python code in there. The second approach (create a .html with some script tags in) won't work here.

Hope this helps,

Craig