📄 182.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> An Introduction to CGI</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="179.html" class="navtitle">12. Scripting Programming</a> > <span class="nonavtitle">An Introduction to CGI</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="181.html" title="Web Programming"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=182" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="182.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="183.html" title="The cgi Module"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A44%3A39+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148040135186102239182092</font><a href="read2.asp?bookname=0672319942&snode=182&now=5%2F31%2F2002+4%3A44%3A39+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>An Introduction to CGI</h3>
<p>
<i>CGI (Common Gateway Interface)</I> is a standardized way for the Web Server to invoke an external
program to handle the client request. It is possible for the external program to access databases, documents, and other
programs as part of the request, as well, and present customized data to viewers via the Web. A CGI script can be written in
any language, but here, of course, we are using only Python.</P>
<P>CGI enables you to handle from the low end of mail-forms and counter programs to the most complex database
scripts that generate entire Web sites on-the-fly. CGI's job is to manage the communication between browsers and
server-side scripts. Programs that implement CGI routines are called CGI programs or CGI scripts. These scripts are usually
visualized, through the Web browser, in a directory called <Tt claSS="monofont">/cgi-bin,</TT> but their actual location in the file system
varies.</p>
<p>You have two ways to <a nAME="idx1073746586"></A>
<a name="idx1073746587"></a>
<a name="idx1073746588"></a>
<a name="idx1073746589"></a>pass the information from the browser to the CGI script: You can use either the <a nAme="idx1073746590"></A>
<a naMe="idx1073746591"></a>
<tt ClasS="monofont">POST</TT> or the <A name="idx1073746592"></A>
<A NAme="idx1073746593"></a>
<tT CLAss="monofont">GET</tt> method on your HTML Form. The <TT CLass="monofont">POST</tt> method uses the <i>standard
input</i> to transfer the information, whereas the <tt class="monofont">GET</tt> method places the information into an
environment variable.<a namE="idx1073746594"></a>
<a Name="idx1073746595"></A>
<a namE="idx1073746596"></a>
</p>
<p>The <TT CLass="monofont">GET</tT> method has the limitation of the size of the environment variable and the advantage of
making it possible to encapsulate an HTML Form within an URL. Another downside to the <TT Class="monofont">GET</TT> method is
that it might leak information. If there is an external image (for instance, a banner ad) or an off site link the user clicks on the
page generated by the CGI script, the form results will be passed to that third party through the referer header. Therefore,
don't use banner ads or off-site links for the CGI script handling a <TT clasS="monofont">GET</TT> form.<A name="idx1073746597"></a>
<a name="idx1073746598"></a>
</p>
<p>The <tt class="monofont">POST</Tt> method, in theory, has no limits to the amount of information that can be passed to the
server. The disadvantage is that you can't send the information as part of the URL. You must have a form in your
page.<a Name="idx1073746599"></A>
<a namE="idx1073746600"></a>
</p>
<p>Python uses the <A NAMe="idx1073746601"></a>
<a nAME="idx1073746602"></A>
<tt clASS="monofont">cgi</Tt> module to implement CGI scripts and to process form handling in Web applications that are invoked by
an <tt cLASS="monofont">HTTP</tt> server. The <tt class="monofont">cgi</tt> module also hides the differences between <tt class="monofont">GET</tt>
and <Tt cLass="monofont">POST</Tt> style forms.</p>
<p>Here is a very simple script to start you out with Python CGI <a Name="idx1073746603"></A>processing:</P>
<PRe>
1: #!/usr/bin/python
2: print "Content-Type: text/plain\n\n"
3: print "Hello Python World!"
</pre>
<P>Line 1: Path to the Python interpreter (UNIX only).</P>
<P>Line 2: Pass the <Tt claSS="monofont">MIME</TT> type to the browser in order to let it know how to render the
information.</p>
<p>Line 3: Prints a string in the browser window.</p>
<p>In order to <A NAMe="idx1073746604"></a>execute it, place it on a executable directory on your Web server and call it from your Web browser. If you are
working on a UNIX-like OS, you need to run <tt class="monofont">chmod a+x scriptname.</tt>
<a name="idx1073746605"></a>
<a naMe="idx1073746606"></a>
<A namE="idx1073746607"></a>
</p>
<p>Sometimes, CGI implementations also cause slow response times in the system. Keep in mind that each CGI
invocation creates a new process, starts a new instance of the Python interpreter, and imports all the necessary library
modules. Okay, I suppose you got the picture.</p>
<P>The goal here is to let you know that sometimes the problem is not in the code, but in the infrastructure that surrounds
it. Within your CGI script, you should consider avoiding using <a naME="idx1073746608"></A>
<A name="idx1073746609"></A>
<TT Class="monofont">fork()</TT> as much as you can. But <TT clasS="monofont">fork()</TT> is not the slow(est) part梚t is the interpreter
startup time and database connection setup. To get help with that, try using <A name="idx1073746610"></a>
<a name="idx1073746611"></a>
<tt class="monofont">mod_pyapache</tt> or <A naMe="idx1073746612"></a>
<a Name="idx1073746613"></a>
<Tt clASS="monofont">mod_python.</Tt>
</p>
<p>The following links take you to sites that demonstrate and clarify the use of CGI routines:</p>
<BLOCkquoTE>
<P>
<P>Python's Web Programming Topic Guide</p>
</p>
<p>
<p><A TARget="_blank" href="http://www.python.org/topics/web/">http://www.python.org/topics/web/</a>
</p>
</p>
<p>
<p>vex.net's directory of Python Web page samples</p>
</p>
<p>
<p>
<a tarGet="_blank" Href="http://www.vex.net/py_examples/">http://www.vex.net/py_examples/</A></p>
</p>
<p>
<p>Aaron Watters's simple CGI examples</P>
</p>
<p>
<p>
<A TARget="_blank" hREF="http://starship.python.net/crew/aaron_watters/cgi/">http://starship.python.net/crew/aaron_watters/cgi/</A>
</p>
</p>
<p>
<p>Fancy CGI Programming</P>
</P>
<P>
<P>
<a tarGET="_blank" Href="http://www.python.org/topics/web/fancy-cgi.html">http://www.python.org/topics/web/fancy-cgi.html</a>
</p>
</p>
<p>
<p>Python-CGI FAQ</p>
</p>
<p>
<p>
<a target="_blank" hRef="http://starship.python.net/crew/davem/cgifaq/">http://starship.python.net/crew/davem/cgifaq/</A>
<a naMe="idx1073746614"></a>
<a nAme="idx1073746615"></a>
<A NAMe="idx1073746616"></a>
</p>
</p>
</BLOCkquoTE>
</FOnt>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 1/30/2002<br>Python Developer's Handbook, © 2002 Sams Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="181.html" title="Web Programming"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=182" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="182.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="183.html" title="The cgi Module"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<br><TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD><H4 class=Title>Index terms contained in this section</H4>
<font size=2>
browsers<BR>
<a href="#idx1073746588">passing data to CGI scripts from</a><BR>
<a href="#idx1073746596">CGI (Common Gateway Interface) scripts</a> <a href="#idx1073746607">2nd</a> <a href="#idx1073746616">3rd</a><BR>
<a href="#idx1073746601">cgi module</a><BR>
<a href="#idx1073746595">Common Gateway Interface (CGI) scripts</a> <a href="#idx1073746606">2nd</a> <a href="#idx1073746615">3rd</a><BR>
data<BR>
<a href="#idx1073746587">passing from browsers to CGI scripts</a><BR>
executing<BR>
<a href="#idx1073746604">CGI scripts</a><BR>
<a href="#idx1073746609">fork() method</a><BR>
<a href="#idx1073746593">GET method</a> <a href="#idx1073746598">2nd</a><BR>
methods<BR>
<a href="#idx1073746608">fork()</a><BR>
<a href="#idx1073746592">GET</a> <a href="#idx1073746597">2nd</a><BR>
<a href="#idx1073746590">POST</a> <a href="#idx1073746599">2nd</a><BR>
<a href="#idx1073746611">mod_pyapache module</a><BR>
<a href="#idx1073746612">mod_python module</a><BR>
modules<BR>
<a href="#idx1073746602">cgi</a><BR>
<a href="#idx1073746610">mod_pyapache</a><BR>
<a href="#idx1073746613">mod_python</a><BR>
passing<BR>
<a href="#idx1073746586">data from browsers to CGI scripts</a><BR>
<a href="#idx1073746591">POST method</a> <a href="#idx1073746600">2nd</a><BR>
processing<BR>
<a href="#idx1073746603">CGI scripts</a><BR>
scripts<BR>
<a href="#idx1073746594">Common Gateway Interface (CGI)</a> <a href="#idx1073746605">2nd</a> <a href="#idx1073746614">3rd</a><BR>
transferring<BR>
<a href="#idx1073746589">data from browsers to CGI scripts</a><BR>
<BR>
</font></TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -