mirror of
https://github.com/python/cpython.git
synced 2024-11-30 05:15:14 +08:00
91 lines
4.8 KiB
HTML
91 lines
4.8 KiB
HTML
<HTML><HEAD><TITLE>Using python to create CGI scripts</TITLE></HEAD>
|
|
<BODY>
|
|
<H1>Using python to create CGI scripts</H1>
|
|
<HR>
|
|
|
|
In this document we will (eventually) explain how to create Python CGI scripts
|
|
for use with Personal WebServer, WebStar and probably other Mac-based HTTP servers too.
|
|
Since CGI scripts are AppleEvent servers on the mac we will also learn
|
|
a little about general AppleEvent server programming and about applet
|
|
debugging. <p>
|
|
|
|
<blockquote>Note that the current setup is very preliminary, and hence
|
|
itis probably not wise to base your strategic products on the information
|
|
in this document:-) In stead, play with the code here and join the
|
|
<a href="mailto:pythonmac-sig-request@python.org">pythonmac-sig</a>, where
|
|
we I would like to have a discussion on a real design for a Mac CGI framework
|
|
(preferrably something that will make CGI scripts portable to unix and other
|
|
platforms).
|
|
</blockquote>
|
|
|
|
<h2>AppleEvent servers</h2>
|
|
|
|
Since AppleEvent clients are easier to write and understand than servers
|
|
you should probably read the section on <a href="applescript.html">Open Scripting
|
|
clients in Python</a> first. <p>
|
|
|
|
Next, let us have a look at the AE Server framework,
|
|
<a href="../Lib/toolbox/MiniAEFrame.py">MiniAEFrame.py</a>.
|
|
This file contains two classes, <code>MiniApplication</code> and <code>AEServer</code>.
|
|
MiniApplication is a tiny replacement for <code>FrameWork.Application</code>,
|
|
suitable if your application does not need windows and such.
|
|
|
|
AEServer is a bit of glue that does part of the appleevent decoding for you. You
|
|
call <code>installaehandler</code> passing it the class and id (4-char strings)
|
|
of the event you have a handler for and the handler callback routine. When the
|
|
appleevent occurs your callback is called with the right arguments. For now,
|
|
your argument names are the 4-char values used internally by Open Scripting,
|
|
eventually there will be a translation similar to what the generated OSA client
|
|
suites provide. <p>
|
|
|
|
You can test AEServer by double-clicking it. It will react to the standard
|
|
run/open/print/quit OSA commands. If it is running as a normal python script and you
|
|
drag a file onto the interpreter the script will tell you what event it got. <p>
|
|
|
|
<h2>A Minimal CGI script</h2>
|
|
|
|
To try a CGI script you will first need a http server. Apple's Personal Webserver
|
|
is fine, but I have also used the
|
|
shareware
|
|
<a href="http://www.stairways.com/netpresenz/">NetPresenz</a>
|
|
by Peter Lewis
|
|
(don't forget to pay if you give it more than a test run!). Install your
|
|
http server, and make sure that it can serve textual documents. <p>
|
|
|
|
Next, let us have a look at our example CGI scripts. CGI scripts have to be
|
|
applications, so we will have to make an applet as explained in
|
|
<a href="example2.html">example 2</a>. Our applet code,
|
|
<a href="cgi/cgitest.cgi.py">cgitest.cgi.py</a> is a rather minimal <code>execfile</code>
|
|
statement. The reason for this is debugging: the real code is in
|
|
<a href="cgi/realcgitest.py">realcgitest.py</a>, and this way you do not have
|
|
to run mkapplet again every time you change the code. Rename realcgitest.py
|
|
to cgitest.cgi.py once you are satisfied that it works. <p>
|
|
|
|
The resource file is not very special, with one exception: since we want to do
|
|
our own appleevent handling we don't want the Python initialization code to
|
|
create argc and argv for use, since this might gobble up any appleevents we are
|
|
interested in. For this reason we have included a 'Popt' resource that disables
|
|
the argv initialization. An easy way to create this resource is to drop
|
|
the <code>.rsrc</code> file (or the finished applet, if you like) onto
|
|
<code>EditPythonPrefs</code> and set the "no argv processing" option. <p>
|
|
|
|
The code itself is actually not too complicated either. We install handlers
|
|
for "open application" and "quit" (stolen from the test code in MiniAEFrame)
|
|
and the <code>"WWW\275"/"sdoc"</code> event, the event sent on CGI execution.
|
|
The cgi handler pretty-prints the CGI arguments in HTML and returns the whole
|
|
string that is to be passed to the client. The actual parameters passed
|
|
are explained in <a href="http://www.biap.com/datapig/mrwheat/cgi_params.html">
|
|
http://www.biap.com/datapig/mrwheat/cgi_params.html</a>. <p>
|
|
|
|
To test the script drop <code>cgitest.cgi.py</code> onto <code>mkapplet</code>,
|
|
move the resulting <code>cgitest.cgi</code> to somewhere where it is reachable
|
|
by NetPresenz, and point your web browser towards it. Note that this assume you have
|
|
already renamed realcgitest.py to cgitest.cgi.py, otherwise you'll also have
|
|
to copy that file along. <p>
|
|
|
|
For Apple's Personal Webserver you need to do a bit more: you have to copy the
|
|
cgi applet to somewhere in your "Webpages" folder and you have to tell the webserver
|
|
(in the control panels) that your CGI script exists. I don't understand what the various
|
|
types of cgi scripts mean, but experiment with them.
|
|
</BODY></HTML>
|