blob: 3932b2842c62ea0a21161846445d7d57617172f9 [file] [log] [blame]
Jack Jansene1c3f361996-09-09 01:48:40 +00001<HTML><HEAD><TITLE>Using python to create CGI scripts</TITLE></HEAD>
2<BODY>
3<H1>Using python to create CGI scripts</H1>
4<HR>
5
6In this document we will (eventually) explain how to create Python CGI scripts
7for use with NetPresenz and probably other Mac-based HTTP servers too.
8Since CGI scripts are AppleEvent servers on the mac we will also learn
9a little about general AppleEvent server programming and about applet
10debugging. <p>
11
12<blockquote>Note that the current setup is very preliminary, and hence
13itis probably not wise to base your strategic products on the information
14in this document:-) In stead, play with the code here and join the
15<a href="mailto:pythonmac-sig-request@python.org">pythonmac-sig</a>, where
16we I would like to have a discussion on a real design for a Mac CGI framework
17(preferrably something that will make CGI scripts portable to unix and other
18platforms).
19</blockquote>
20
21<h2>AppleEvent servers</h2>
22
23Since AppleEvent clients are easier to write and understand than servers
24you should probably read the section on <a href="applescript.html">Open Scripting
25clients in Python</a> first. <p>
26
27Next, let us have a look at the AE Server framework,
28<a href="../Lib/toolbox/MiniAEFrame.py">MiniAEFrame.py</a>.
29This file contains two classes, <code>MiniApplication</code> and <code>AEServer</code>.
30MiniApplication is a tiny replacement for <code>FrameWork.Application</code>,
31suitable if your application does not need windows and such.
32
33<blockquote>Actually, Framework.Application has a problem for AE Servers,
34due to the way it expects to be quit through an exception, and raising an exception
35while inside an Apple Event handler is a very bad idea. This will be fixed.
36</blockquote>
37
38AEServer is a bit of glue that does part of the appleevent decoding for you. You
39call <code>installaehandler</code> passing it the class and id (4-char strings)
40of the event you have a handler for and the handler callback routine. When the
41appleevent occurs your callback is called with the right arguments. For now,
42your argument names are the 4-char values used internally by Open Scripting,
43eventually there will be a translation similar to what the generated OSA client
44suites provide. <p>
45
46You can test AEServer by double-clicking it. It will react to the standard
47run/open/print/quit OSA commands. If it is running as a normal python script and you
48drag a file onto the interpreter the script will tell you what event is got. <p>
49
50<h2>A Minimal CGI script</h2>
51
52To try a CGI script you will first need a http server. I have used the
53shareware
54<a href="http://www.share.com/peterlewis/netpresenz/netpresenz.html">NetPresenz</a>
55by <a href="http://www.share.com/peterlewis/">Peter Lewis</a>
56(don't forget to pay if you give it more than a test run!). Install your
57http server, and make sure that it can serve textual documents. <p>
58
59Next, let us have a look at our example CGI scripts. CGI scripts have to be
60applications, so we will have to make an applet as explained in
61<a href="example2.html">example 2</a>. Our applet code,
62<a href="cgi/cgitest.cgi.py">cgitest.cgi.py</a> is a rather minimal <code>execfile</code>
63statement. The reason for this is debugging: the real code is in
64<a href="cgi/realcgitest.py">realcgitest.py</a>, and this way you do not have
65to run mkapplet again every time you change the code. Rename realcgitest.py
66to cgitest.cgi.py once you are satisfied that it works. <p>
67
68The resource file is not very special, with one exception: since we want to do
69our own appleevent handling we don't want the Python initialization code to
70create argc and argv for use, since this might gobble up any appleevents we are
71interested in. For this reason we have included a 'Popt' resource that disables
72the argv initialization. An easy way to create this resource is to drop
73the <code>.rsrc</code> file (or the finished applet, if you like) onto
74<code>EditPythonPrefs</code> and set the "no argv processing" option. <p>
75
76The code itself is actually not too complicated either. We install handlers
77for "open application" and "quit" (stolen from the test code in MiniAEFrame)
78and the <code>"WWW\275"/"sdoc"</code> event, the event sent on CGI execution.
79The cgi handler pretty-prints the CGI arguments in HTML and returns the whole
80string that is to be passed to the client. The actual parameters passed
81are explained in <a href="http://www.biap.com/datapig/mrwheat/cgi_params.html">
82http://www.biap.com/datapig/mrwheat/cgi_params.html</a>. <p>
83
84To test the script drop <code>cgitest.cgi.py</code> onto <code>mkapplet</code>,
85move the resulting <code>cgitest.cgi</code> to somewhere where it is reachable
86by NetPresenz, and point your web browser towards it.