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