cancel
Showing results for 
Search instead for 
Did you mean: 

CGI newbie!

stalyD
Newbie
Posts: 3
Registered: ‎24-04-2011

CGI newbie!

I'm completely new to using CGI, so please bear with my probable apparent stupidity!
I'm working through the Mark Lutz's book "Programming Python" (from O'Reilly), and I've just got as far as writing the first very, very basic CGI script.  This works fine running a basic webserver on my own machine, but I can't get things to work correctly from my PlusNet cgi space.
In my root folder on the server, I have an index.html file, which just reads
<html>
<title>Interactive Page</title>
<body>
<form method=POST action="cgi-bin/cgi101.py">
  <P><B>Enter your name:</B>
  <P><input type=text name=user>
  <P><input type=submit>
</form>
</body></html>
I have also put an empty file called .htaccess in the folder.  The permissions on both files are set to 640, as recommended.
In my cgi-bin folder I have cgi101.py which reads:
#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
print('Content-type: text/html\n')
print('<title>Reply Page</title>')
if not 'user' in form:
    print('<h1>Who are you?</h1>')
else:
    print('<h1>Hello <i>%s</i>!</h1>' % cgi.escape(form['user'].value))
There is also another empty .htaccess file, with permissions set to 640.
The index.html page displays OK.  If I set the permissions on cgi-bin/cgi101.py to 700 (as recommended on the PlusNet help pages), then when I click on the "Submit" button I get an Error 403 (Access Forbidden!) page.
If I set the permissions on cgi-bin/cgi101.py to 777, then I can access the script, but Firefox tries to download it.
Where have I gone wrong?  Or is it simply that PlusNet doesn't support Python scripts?
David
4 REPLIES 4
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: CGI newbie!

Nearly there but not quite as you discovered  Roll_eyes
The problem is that the web server doesn't have a handler for Python programs so it just passes the file to the browser with the results you've seen. The quick fix is to alter your code as follows to run the Python interpreter in a shell:
Change the "action" in your index.html file to read
<form method=POST action="cgi-bin/cgi101.cgi">
Rename your existing cgi101.py file to cgi101.cgi
Set the permissions of your index.html file to 640 (probably the default set by your FTP program).
Set the permissions of cgi101.cgi to 700.
Browse to index.html, fill in the text box and click "submit" and … I get "Hello David!" Smiley
David
David
stalyD
Newbie
Posts: 3
Registered: ‎24-04-2011

Re: CGI newbie!

Thanks for that - it worked.
But I don't quite understand what is happening.  Is the cgi script being downloaded to my computer, run there and the result sent back to the web server?  But then that would mean it would fail for a browser running on a machine without Python installed. Huh
Or is it that the server does have a python interpreter, but doesn't know that it is supposed to execute the script unless its name has a .cgi extension?  Then the server machine itself reads the #!/usr/bin/python line.  That sounds as if it must be the correct explanation, but I'd be glad if you could confirm that.
David
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: CGI newbie!

The explanation in your last paragraph applies.
The web server knows how to handle .php (PHP), .pl (Perl) and .cgi (shell script) files. What we do here is tell the shell to use the file given on the shebang line (#!/usr/bin/python) to interpret the script. This is the Python interpreter which runs on the server and interprets remaining lines in the file. Output is served to your browser starting with headers (Content-type: text/html followed by an empty line created by \n (and a second \n the print command creates)) to tell your browser what to expect. Since it is text/html your browser displays it on your screen.
I assume you put the Python script file in the /cgi-bin directory because the book tells you to. On ccgi that isn't necessary, though clearly it must be where the form action option points. Also the .htaccess files you mentioned don't serve any purpose and can be removed. Smiley
David
David
stalyD
Newbie
Posts: 3
Registered: ‎24-04-2011

Re: CGI newbie!

Thanks for all your help.
I may be back when I get to the next stage... Roll_eyes
David