184.html

来自「Python Ebook Python&XML」· HTML 代码 · 共 193 行

HTML
193
字号

<!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 -&gt; Creating, Installing, and Running Your Script</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> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="179.html" class="navtitle">12. Scripting Programming</a> &gt; <span class="nonavtitle">Creating, Installing, and Running Your Script</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="183.html" title="The cgi Module"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=184" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="184.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="185.html" title="Python Active Scripting"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A44%3A56+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148040135188010160225247</font><a href="read9.asp?bookname=0672319942&snode=184&now=5%2F31%2F2002+4%3A44%3A56+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>






Creating, Installing, and Running Your Script</h3>
				<p>You are free to edit your Python scripts using your favorite text editor (such as, Notepad, IDLE, Editpad, pico,
PythonWwin, vi, and so on). Of course, we can't forget about Emacs, which has one of the best Python editing modes
available.</p>

				<P>Remember to upload your scripts as text files to your Web server. In order to execute them, you need to make sure
that they are in a "executable" directory, and that they have the right permissions.</P>

				<P>As I said before, most often CGI scripts live in the server's special <A name="idx1073746687"></A>
					<A NAme="idx1073746688"></a>
					<tT CLAss="monofont">cgi-bin</tt> directory. You should consider verifying whether the files, that your script needs to read or write, are
actually readable or writable, respectively, by other users. In UNIX, the command to <a name="idx1073746689"></a>
					<a name="idx1073746690"></a>
					<a namE="idx1073746691"></a>
					<a Name="idx1073746692"></A>set the permissions is <a namE="idx1073746693"></a>
					<a nAME="idx1073746694"></A>
					<tt clASS="monofont">chmod.</Tt>
				</p>

				<p>For example,</p>

				<PRE>
					
chmod 755 filename

				</Pre>

				<p>The mode argument <tT CLAss="monofont">755</tt> defines that the file's owner can read, write, and execute the file, whereas
the other users can only read and execute it.</p>

				<p>The common UNIX mode values and their respective symbolic arguments are</p>

				<ul>
<li>
						<p>
							<tt class="monofont">chmod 755</tT> for executable scripts, or <tt ClasS="monofont">chmod a+rx.</tt>
						</p>

					</lI>
<li>
						<p>
							<TT CLass="monofont">chmod 666</tT> for writable files, or <TT Class="monofont">chmod a+w.</TT>
						</P>

					</Li>
<li>
						<p>
							<TT CLass="monofont">chmod 644</tt> for readable files, or <tt class="monofont">chmod a+r.</tt>
							<a name="idx1073746695"></a>
							<A naMe="idx1073746696"></a>
							<a Name="idx1073746697"></a>
							<A namE="idx1073746698"></A>
							<A Name="idx1073746699"></a>
							<A NAMe="idx1073746700"></a>
						</p>

					</lI>
</UL>
				<Div clASS="note"><P class="notetitle"><b>Tip</b></p><p>

					<p>Keep in mind that <a name="idx1073746701"></a>
						<a namE="idx1073746702"></a>
						<a Name="idx1073746703"></A>
						<a namE="idx1073746704"></a>
						<a nAME="idx1073746705"></A>
						<a namE="idx1073746706"></A>commands and filenames are all case sensitive if the Web Server is on an OS with case-sensitive
filenames.</P>

				</P></div>
<bR>
<BR>

				<P>For security reasons, the HTTP server executes your script as <a namE="idx1073746707"></A>
					<A Name="idx1073746708"></a>user "nobody", without any special privileges. Therefore, it can only read (write, execute) files
that everybody can read (write, execute).<a name="idx1073746709"></a>
					<a name="idx1073746710"></a>
					<a namE="idx1073746711"></a>
					<a Name="idx1073746712"></A>
					<a namE="idx1073746713"></a>
					<a nAME="idx1073746714"></A>
					<a namE="idx1073746715"></A>
				</P>

				<P>The current directory at execution time is usually the server's <tt clASS="monofont">/cgi-bin</Tt> directory, and the set of
environment variables is different from what you get at login. In other words, don't count on the shell's search path variable
for executables <a naME="idx1073746716"></A>
					<A name="idx1073746717"></a>(<tt class="monofont">$PATH</tt>) or the Python module search path variable <a name="idx1073746718"></a>
					<A naMe="idx1073746719"></a>(<tT clasS="monofont">$PYTHONPATH</tt>) to be set to anything useful.</p>

				<P>If you need to <A NAme="idx1073746720"></a>
					<a NAME="idx1073746721"></a>load modules from a <a naME="idx1073746722"></A>directory that is not listed as part of the Python's default module search path, you can change the path variable
in your script before trying to import them. In the following example, we add three more directory entries in the search path.
Note that the last directory inserted, <Tt claSS="monofont">"/usr/python/testdict",</TT> is searched first.</p>

				<pre>
					
import sys
sys.path.insert(0, "/usr/python/lib")
sys.path.insert(0, "/usr/tmp")
sys.path.insert(0, "/usr/python/testdict")

				</pre>

				<p>Instead of using <tt class="monofont">"from cgi import *",</tt> you should use only <tt cLasS="monofont">"import cgi"</tt> because the
<tT clasS="monofont">cgi</tt> module defines many other names for backward compatibility that can interfere with your code.</p>

				<P>It also might be useful for you to redirect the standard error (<TT Class="monofont">sys.stderr</TT>) to the standard output
(<TT clasS="monofont">sys.stdout</TT>). This will display all the error messages in the browser.</P>

				
					<h4>



Sending Information to Python Scripts</h4>
					<p>Every time you use a URL to carry information to a CGI script, the data is transported as <a NAME="idx1073746723"></a>
						<a name="idx1073746724"></a>
						<i>name/value</i> pairs, separated by <a name="idx1073746725"></a>
						<a name="idx1073746726"></A>ampersands (<tt ClasS="monofont">&amp;</tt>), and each pair is separated by an <a nAme="idx1073746727"></a>
						<A NAMe="idx1073746728"></a>equal sign (<tt CLASs="monofont">=</tt>). Whitespaces between words are usually converted to the <a NAME="idx1073746729"></a>
						<a naME="idx1073746730"></A>plus symbol (<Tt class="monofont">+</tt>).</p>

					<p>For example,</p>

					<p><a target="_blank" hRef="http://www.lessaworld.com/cgi-script/app.py?animal=parrot&amp;type=singer">http://www.lessaworld.com/cgi-script/app.py?animal=Parrot&amp;type=Singer</A>
						<a naMe="idx1073746731"></a>
						<a nAme="idx1073746732"></a>
						<A NAMe="idx1073746733"></a>
						<a nAME="idx1073746734"></A>
						<a namE="idx1073746735"></A>
						<A Name="idx1073746736"></a>
						<A NAMe="idx1073746737"></a>
						<a name="idx1073746738"></a>
						<a name="idx1073746739"></a>
						<a name="idx1073746740"></a>
						<A naMe="idx1073746741"></a>
					</p>

					<P>Special characters are encoded to hexadecimal format (<tt clAss="monofont">%HH</tT>) and preceded by the percent sign.
Therefore, the string <I>
							<TT clasS="monofont">"Parrot sketch"</TT>
						</I> is passed to the script as <tt clASS="monofont">"Parrot%20sketch".</Tt>
					</p>

					<p>As you can see, the previous example is implicitly using the <tT CLAss="monofont">GET</tt> method to pass the values to the
CGI script. If you decide that the <tt class="monofont">POST</tt> method is more suitable for your needs, you will need to use the
<tt class="monofont">urllib</Tt> module in order to send the information. The following example demonstrates its use.</p>

					<Pre>
						
import urllib
request = urllib.urlencode({
    "animal": "Parrot", "type": "Singer"
})
page = urllib.urlopen("http://oemcomputer/cgi-script/app.py", request)
response = page.read()

					</pRe>

					<p>Check the <tt ClasS="monofont">urllib</TT> documentation for details:</P>

					<p><a taRGET="_blank" href="http://www.python.org/doc/current/lib/module-urllib.html">http://www.python.org/doc/current/lib/module-urllib.html</A>
					</P>

					<P>
						<A href="184#2.html">Table 12.1</A> contains a list of special characters and their encoded strings.</P>

					<A Name="2"></a><p><table border="1" cellSpaCing="0" CellpAddiNG="1" WIdth="100%">
<cAPTIon><h5>Table

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?