CGI Introduction
To use CGI, you must have access to a Web server running on a UNIX-based platform.
CGI scripts can be included in interpreted scripting languages such as Perl, UNIX shells, TCL or DOS, and compiled programming languages such as C or Visual Basic.
Which kind is Python?
TCL stands for the Tool Command Language, which is a CGI scripting language. Dr. John K. Ousterhout developed it. It is a portable and extensible shell language. It is often used with the Tk toolkit.
Requirements
check which of the following is necessary all the time, rather than just for the course.
You must have access to a Web server running on a UNIX-based platform, or a web server that supports server-side includes (SSI), Perl, and be able to route script output to sendmail (a UNIX program that handles e-mail messages).
You can use any HTML Text editor, but Microsoft Notepad and Wordpiles may not load well. Possibilities include The Programmer's File Editor (PFE), BBEdit for Macs, Adobe PageMill, Homesite, Webber, HotDog, CoffeeCup HTML Editor. Text files formats differ between platforms (e.g. DOS/Windows and Unix). For instance, Notepad, etc. include an invisible line-feed which may cause problems on your web server.
You will need an FTP (File Transfer Protocol) client to upload files to the web server, for example CuteFTP, WS_FTP, or Anarchie or Fetch for the Mac.
If FTP fails, try a Telnet client, such as the one included with Windows 95, BetterTelnet (for Macs), or Tera Term. With Telnet clients you access the web server as if you were a terminal attached direct to the computer.
You also need a web browser, of course.
CGI Script
The first line of your script, e.g. (#!/usr/bin/perl)., must be tailored to your web server. Web servers usually have a special area for scripts, so you must place them there. CGI scripts are often placed in
http://host.name/cgi-bin/
You can add any valid HTML code at the bottom, but __END__ must be on a line by it-self (Perl requires that) and you must have a carriage return at the end of the line and an extra carriage return at the end of the file.
Connection Status
Web transactions proceed as follows:
- client connects to server with server request
- server acknowledges requests, sends the information requested (e.g. .html, .gif).
- connection terminated
Because the connection is terminated, the serve has no memory of previous connections or requests, so this is state information is not available to the CGI script.
One way round it would be to pass information about previous states as parameters,along with the current parameter, for example:
http://some.host.name/cgi-bin/myscript?this_try1=I&Previous_tries=A+E
This means rebuilding all the HTML pages on the fly to insert the previous responses into each URL. This can be inefficient and lead to very long URLs.
Another way is to assign a session ID and store the responses in its temporary file. For example:
http://some.host.name/cgi-bin/myscript?ID=2468&this_try=I
A workaround to this problem is to assign a session ID and keep a client's responses stored in a temporary file for that session ID. The session ID is then inserted into the links on the page; the URLs send the new response together with the session ID. Using this method, each letter in the hangman page would contain a URL that looks something like this for the letter "A" guess, assuming the session ID is 1234. The link URLs need to be written just once on the fly to include the appropriate session ID, The server can read the session ID file, use the data in it, and update it each time.
The server would not know when the client had left, however, so would need a timeout if the server is not to become full of such files, or the client is not to be greeted with out of date data next time they visit.
With Unix, a cron job can be set up to execute at regular intervals to check the last modification date and time of all state files and to remove the older ones.
Performance considerations
In addition to the normal processing of HTML, the Web server has to initiate a process to handle the CGI script. Creating and deleting such processes can constitute a heavy load, slowing down your system or even making it grind to a halt. It may be necessary to add hardware such as a faster CPU or more memory. Alternatively, you may opt for a software solution such as the Java programming language or the Netscape Server API.
Java-aware browsers can execute interactive programs instead of the server, and the program is transmitted as part of the data stream in the form of Java applets. The server still serves the HTML and the applets to the browser and, using scripts of the Netscape API, submits registration forms, accesses databases and performs database searches.
The other solution, which lets programs be executed within the server rather than as a separate process, is the Netscape Server API. Programs are stored as code libraries and the server can dynamically link to them and execute them. This can only be done on a Netscape server, and after you have modified the server configuration files to include your software libraries.
The client/server environment is evolving, so new, improved ways of providing interactivity will be along.
