hellos = {
'Python': r" print('Hello World') ",
'Python2': r" print 'Hello World' ",
'Perl': r' print "Hello World\n"; ',
'Tcl': r' puts "Hello World" ',
'Scheme': r' (display "Hello World") (newline) ',
'SmallTalk': r" 'Hello World' print. ",
'Java': r' System.out.println("Hello World"); ',
'C': r' printf("Hello World\n"); ',
'C++': r' cout << "Hello World" << endl; ',
'Basic': r' 10 PRINT "Hello World" ',
'Fortran': r" print *, 'Hello World' ",
'Pascal': r" WriteLn('Hello World'); "
}
The module languages2common contains all the data that needs to agree between pages:
the field name as well as the syntax dictionary. The hellos syntax dictionary isn’t quite
HTML code, but its keys list can be used to generate HTML for the selection list on
the main page dynamically.
Notice that this module is stored in the same cgi-bin directory as the CGI scripts that
will use it; this makes import search paths simple—the module will be found in the
script’s current working directory, without path configuration. In general, external ref-
erences in CGI scripts are resolved as follows:
- Module imports will be relative to the CGI script’s current working directory (cgi-
bin), plus any custom path setting in place when the script runs. - When using minimal URLs, referenced pages and scripts in links and form actions
within generated HTML are relative to the prior page’s location as usual. For a CGI
script, such minimal URLs are relative to the location of the generating script itself. - Filenames referenced in query parameters and passed into scripts are normally rel-
ative to the directory containing the CGI script (cgi-bin). However, on some plat-
forms and servers they may be relative to the web server’s directory instead. For
our local web server, the latter case applies.
To prove some of these points to yourself, see and run the CGI script in the examples
package identified by URL http://localhost/cgi-bin/test-context.py: when run on Win-
dows with our local web server, it’s able to import modules in its own directory, but
filenames are relative to the parent directory where the web server is running (newly
created files appear there). Here is this script’s code, if you need to gauge how paths
are mapped for your server and platform; this server-specific treatment of relative file-
names may not be idea for portability, but this is just one of many details that can vary
per server:
import languages2common # from my dir
f = open('test-context-output.txt', 'w') # in .. server dir
f.write(languages2common.inputkey)
f.close()
print('context-type: text/html\n\nDone.\n')
1194 | Chapter 15: Server-Side Scripting