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