[Tutor] Creating your first CGI-script

Kirk Bailey idiot1@netzero.net
Thu Jun 5 19:00:03 2003


ok. For a script to include output IN a web page, you have to invoke it with a tag in 
the page. The server has to LOOK for suc h tags in the web page. You can tell it to 
parse ALL pages, or to parse pages with a particular name, typically '(name).shtml'. 
This let's it same a little time by NOT looking for tags in pages where you did not use 
them. .html for them what don't, shtml for them what did.

ASSUMING you are running apache as the web server, here is the link to the Apachi ssi 
tutoral:
http://httpd.apache.org/docs/howto/ssi.html


This tag looks like:
         <!--#include virtual="/cgi-bin/counter.pl" -->

You can also use ssi to include intresting information that is NOT a script.

Now, some pages are ENTIRELY the output of a cgi script. Here is an example of such a page:
	http://www.tinylist.org/cgi-bin/TLmastermenu.py

THE ENTIRE PAGE was created by a script, from soup to nuts.


When you write a script, you need to do something very important; you have to tell it 
how to be a program.

Scripts are not executable machine language; they are INTERPETED by another program, 
that reads them and 'does something appropriate' in response  to each line in the 
script. If you ever wrote a program in BASIC, you were probably using an INTERPETER. 
Python is basically a interpeted language. You also can write scripts for the sh shell 
in unix, and batch file scripts for MS-DOS/Windows. With unix shell scripts and perl and 
python, you have to tell the thing what program will interpet it- batch files are a bit 
different. This is done in the VERY FIRST LINE, which we nickname a 'shebang'. It looks 
like a comment, but it's not:

#!/usr/bin/python

is an example shebang. After the '#!', you have to provide the path to python in your 
computer, and the name of python's interpeter program ('python'). MAKE SURE the path in 
YOUR shebang really points at YOUR python, DO NOT just copy that example I just typed out.

In a shell script, it's the same. It would be

#!/usr/sbin/sh

ASSUMING that was where your 'sh' shell lived.

A script has to be executable. In unix, this means turn the X bit on, with the chmod 
command.

ns# chmod +x helloworld.py

Would do it nicely.

WEB scripts whould be in the web cgi-bin, which is under the web directory where your 
web pages live. As any script here can probably be run by ALL THE WORLD, you want to 
keep anything real sensitive and dangerous OUT OF HERE.

When a browser wants to invoke a program, it includes it in the url. For instance, look 
at that url for the menu I typed out:

http://www.tinylist.org/cgi-bin/TLmastermenu.py

The url is providing a domain name, a path under the web page, and the name of a 
program. The server executes that program, and shoots back any output it gets from it, 
if it can, and can understand it.

If the program is going to say something that needs to be displayed in the browser, you 
have to let the server know what is coming at it, or it will probably become confused 
and barf electrons all over the place. So the first thing the script
should 'say' is

print 'Content-type: text/html'	# header for the server's information!
print				# it needs a blank line to seperate the header
				# from the remaining output.

then you can print the output normally.

So now we can bang out a simple hello world script.

#!/usr/bin/python
print'Content type: text/html'
print
print 'hello world!'


And HERE is a link to the apache cgi tutoral.

http://httpd.apache.org/docs/howto/cgi.html.html#yourfirstcgiprogram

I  hope this helps. If anyone wants to jumop down my throat for this long letter. please 
have the courtesy to do it off list.




Ole Jensen wrote:
> First of all, I have no experince in web programming, but my plans are 
> to get into that sooner rather than later. So if my assumptions below 
> are hillariously wrong feel free to correct me before what I think I 
> know sticks with me :-).
> 
> I know there is more to web programming other than CGI, I just don't 
> know what (I have heard of SQL (databases?), XML (The future of the web, 
> maybe? (XHTML)), but I'm not sure that this has anything directly to do 
> with Python. So what I'm hoping you can help me with; is what I need to 
> make CGI scripts.
> I guess it's possible to create and test CGI locally (i.e. without 
> uploading it to the net). Then again does it require some server 
> software to run cgi scripts (or rather test them). I assume (from 
> reading this tutor mail 
> <http://mail.python.org/pipermail/tutor/2003-June/022841.html) >)that 
> the necesary software is included in the Python download.
> Anyway if not, I'm using Win XP and hoping you can recommend some server 
> software to use to test my CGI-scripts.
> Also, my current homepage server (through my ISP) does not support CGI, 
> so if you now of any free (as in no-cost) web hosters that support 
> Python I'ld be happy to know. I've been looking at www.freezope.org 
> <http://www.freezope.org> but they don't support cgi so I'm not really 
> sure if thats what would suit my needs, BTW what can you use their 
> service to create?
>  
> My appologies if this is trivial but to me there seems to be some 
> missing link between the python newbie section to the basic CGI 
> programming section (under tutorials)
>  
> Well I seemed to have gone on long enough for now. So thanks to anyone 
> reading this far ;-)


-- 

end

Cheers!
         Kirk D Bailey
                               think
http://www.howlermonkey.net/ +-----+ http://www.tinylist.org/
http://www.listville.net/    | BOX | http://www.sacredelectron.org/
                              +-----+
"Thou art free"-ERIS          think    'Got a light?'-Promethieus

.