February 28th, 2006, 03:19 PM
Python & CGI
I want to use python with my webpage. In order to test it to see if it works i tried the following basic example:
<p>Enter a year and find out if it's a leap year:
<input type="text" name="year" size="6">
Instead of running the code it just displayed it.
if year % 400 == 0:
elif year % 100 == 0:
elif year % 4 == 0:
reshtml = """Content-Type: text/html\n
<h1>Is it a leap year?</h1>
form = cgi.FieldStorage()
year = form['year'].value
message = "%s is a leap year." % year
message = "%s is not a leap year." % year
print reshtml % message
So i checked with my hosting provider to see if CGI was available or if i had to put it in the CGI-BIN directory to which i dont have access and they responded saying that yes it is available and i can run it from any folder - it doesnt have to be in CGI-BIN.
But that changes nothing because it still doesnt work. I have never done this before so where could i be going wrong?
Edit: leapyear.py is in the same folder as the webpage and action = 'xx' has been changed accordingly.
I also chmodded everything to rwx for all users - no dice.
Last edited by Civ247; February 28th, 2006 at 03:23 PM.
February 28th, 2006, 03:48 PM
ok i tried a different simpler example and named it test.cgi instead of test.py
I get the following error:
# Required header that tells the browser how to render the text.
print "Content-Type: text/plain\n\n"
# Print a simple message to the display window.
print "Hello, World!\n"
February 28th, 2006, 04:34 PM
I was dealing with that problem myself for almost a month last year and I gave up. It seems that most service providers don't support Python in their cgi-bin and you can't put anything in that bin yourself either, which again I tried myself. You have a better chance with Perl ofcourse, since for historical reasons it is more supported by servers.
February 28th, 2006, 05:03 PM
Maybe their copy of Python isn't installed in /usr/bin/python ...
Email them and ask - or try the "#!/bin/env python" format.
February 28th, 2006, 11:07 PM
do you have shell access to the server? if so, 'which python' will give you the location. but /usr/bin/env python should work. also, check the permissions on the script. it needs read and execute for everyone, since you're not sure what user the server is running the script as. could be set up as your user (probably not), nobody, www, www-data, etc, etc. could be anything, so 766 perms would be good.
March 1st, 2006, 04:48 AM
thanks for the tips. Python is definitley in /usr/bin/python - they told me that. Unfortunately i dont have shell access.
I wrote a program that scans all files in a web project and counts different types of files and counts lines of code, comments, image files etc
i did it in python because i knew it would take me twice as long in php.
the project is due on soon so im just going have to do it in php.
Thanks for the help
March 1st, 2006, 06:11 AM
I don't understand why you aren't asking your web host's support about this instead. Your simple example of printing hello world looks like it should work fine...
March 1st, 2006, 06:15 AM
yea i did that first & they were pretty good & responsive. i think the guy that answered wasnt aware of the fact that python doesnt work. I thought i had some bug in the code and didnt want to bug the hosters about that