blob: 01ea85e1eb9e1bd7b6e507ef0514ddbc60d93ec5 [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
Jack Jansen2f2c5f62000-04-22 21:50:33 +00007for use with Personal WebServer, WebStar and probably other Mac-based HTTP servers too.
Jack Jansene1c3f361996-09-09 01:48:40 +00008Since 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
Jack Jansene1c3f361996-09-09 01:48:40 +000033AEServer is a bit of glue that does part of the appleevent decoding for you. You
34call <code>installaehandler</code> passing it the class and id (4-char strings)
35of the event you have a handler for and the handler callback routine. When the
36appleevent occurs your callback is called with the right arguments. For now,
37your argument names are the 4-char values used internally by Open Scripting,
38eventually there will be a translation similar to what the generated OSA client
39suites provide. <p>
40
41You can test AEServer by double-clicking it. It will react to the standard
42run/open/print/quit OSA commands. If it is running as a normal python script and you
Jack Jansenef5cd051996-09-17 12:39:12 +000043drag a file onto the interpreter the script will tell you what event it got. <p>
Jack Jansene1c3f361996-09-09 01:48:40 +000044
45<h2>A Minimal CGI script</h2>
46
Jack Jansen2f2c5f62000-04-22 21:50:33 +000047To try a CGI script you will first need a http server. Apple's Personal Webserver
48is fine, but I have also used the
Jack Jansene1c3f361996-09-09 01:48:40 +000049shareware
Just van Rossum8f11d8e2000-03-28 20:54:50 +000050<a href="http://www.stairways.com/netpresenz/">NetPresenz</a>
51by Peter Lewis
Jack Jansene1c3f361996-09-09 01:48:40 +000052(don't forget to pay if you give it more than a test run!). Install your
53http server, and make sure that it can serve textual documents. <p>
54
55Next, let us have a look at our example CGI scripts. CGI scripts have to be
56applications, so we will have to make an applet as explained in
57<a href="example2.html">example 2</a>. Our applet code,
58<a href="cgi/cgitest.cgi.py">cgitest.cgi.py</a> is a rather minimal <code>execfile</code>
59statement. The reason for this is debugging: the real code is in
60<a href="cgi/realcgitest.py">realcgitest.py</a>, and this way you do not have
61to run mkapplet again every time you change the code. Rename realcgitest.py
62to cgitest.cgi.py once you are satisfied that it works. <p>
63
64The resource file is not very special, with one exception: since we want to do
65our own appleevent handling we don't want the Python initialization code to
66create argc and argv for use, since this might gobble up any appleevents we are
67interested in. For this reason we have included a 'Popt' resource that disables
68the argv initialization. An easy way to create this resource is to drop
69the <code>.rsrc</code> file (or the finished applet, if you like) onto
70<code>EditPythonPrefs</code> and set the "no argv processing" option. <p>
71
72The code itself is actually not too complicated either. We install handlers
73for "open application" and "quit" (stolen from the test code in MiniAEFrame)
74and the <code>"WWW\275"/"sdoc"</code> event, the event sent on CGI execution.
75The cgi handler pretty-prints the CGI arguments in HTML and returns the whole
76string that is to be passed to the client. The actual parameters passed
77are explained in <a href="http://www.biap.com/datapig/mrwheat/cgi_params.html">
78http://www.biap.com/datapig/mrwheat/cgi_params.html</a>. <p>
79
80To test the script drop <code>cgitest.cgi.py</code> onto <code>mkapplet</code>,
81move the resulting <code>cgitest.cgi</code> to somewhere where it is reachable
Jack Jansen2f2c5f62000-04-22 21:50:33 +000082by NetPresenz, and point your web browser towards it. Note that this assume you have
83already renamed realcgitest.py to cgitest.cgi.py, otherwise you'll also have
84to copy that file along. <p>
85
86For Apple's Personal Webserver you need to do a bit more: you have to copy the
87cgi applet to somewhere in your "Webpages" folder and you have to tell the webserver
88(in the control panels) that your CGI script exists. I don't understand what the various
89types of cgi scripts mean, but experiment with them.
Jack Jansena2139fe1998-02-25 15:40:35 +000090</BODY></HTML>