Using Python

liunx

Guest
Well, I was told to come here in a help desk ticket, but I still am not sure what to do. I wrote this test file and put it in my cgi-bin:<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->#!/usr/bin/python<br /># Filename: test.py<br /><br />length = 5<br />breadth = 2<br /><br />area = length * breadth<br />print 'Content-Type: text/html'<br />print 'Area is', area<br />print 'Perimeter is', 2 * (length + breadth)<!--c2--></div><!--ec2--><br /><br />Whenever I try to run the file, I get a 500 Internal Server Error. I was told to try the path /user/bin/python2.2, but that didn't work either.If anybody has a solution, it would be much appreciated.<!--content-->
Did you upload the file in ASCII mode? Not Binary and change the permissions to 755.<!--content-->
I didn't upload the file really, I made it there via Cpanel, is that my problem?<!--content-->
Did you change the permissions to 755 so that you could execute it? If not the permissions are likely 644 which will not execute and thus produce an error.<!--content-->
Yes, the permissions are set to 0755, as well as the permissions of the cgi-bin.<!--content-->
Sorry, I have no experience with Python. Maybe one of our other users that has used it will be able to offer assistance.<!--content-->
This isn't really a python problem. In any script that is outputting to a web browser, there has to be a blank line separating any HTTP header lines from the actual page content (that's how the web server knows where the HTTP header ends and the page content starts).<br /><br />Your script works correctly (no 500 error) if you add an extra 'print' command as I have it below:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->#!/usr/bin/python<br /># Filename: test.py<br /><br />length = 5<br />breadth = 2<br /><br />area = length * breadth<br />print 'Content-Type: text/html'<br />print<br />print 'Area is', area<br />print 'Perimeter is', 2 * (length + breadth)<!--c2--></div><!--ec2--><!--content-->
Well that's very interesting, thanks a lot for the help.<!--content-->
We mainly use php but we want to get familiar with python in case we use it for the google sitemap.<br /><br />We've used one perl cgi script and that was simple, we just put the file in our x.com/cgi-bin directory and everything was cool.<br /><br />If we want to use the test.py sample file in this thread then where do we put it in our file system (and please don't say /usr/bin/python, rather tell me where to look in ftp) ? In other words, one can find the perl directory by going to www then searching for the cgi-bin subdirectory, how do we do the same thing for python?<br /><br />Thanks,<br />Eric<!--content-->
CGI scripts (perl or python) can be placed in your /public_html directory (same as /www) or any subdirectory below it. If you like the extra security offered by the /public_html/cgi-bin directory, you can place them there, but you don't have to. <br /><br />If you decide to place the file somewhere within /public_html but outside of the /public_html/cgi-bin directory, you should rename the file to test.cgi so the web server will recognize it as a file to be executed instead of displayed.<!--content-->
 
Top