Commit e512006e authored by Guido van Rossum's avatar Guido van Rossum

Fix up indentation of examples to use 4 spaces instead of tabs.

parent 65d90eb5
...@@ -40,8 +40,8 @@ telling the client what kind of data is following. Python code to ...@@ -40,8 +40,8 @@ telling the client what kind of data is following. Python code to
generate a minimal header section looks like this: generate a minimal header section looks like this:
\begin{verbatim} \begin{verbatim}
print "Content-type: text/html" # HTML is following print "Content-type: text/html" # HTML is following
print # blank line, end of headers print # blank line, end of headers
\end{verbatim} \end{verbatim}
The second section is usually HTML, which allows the client software The second section is usually HTML, which allows the client software
...@@ -49,9 +49,9 @@ to display nicely formatted text with header, in-line images, etc. ...@@ -49,9 +49,9 @@ to display nicely formatted text with header, in-line images, etc.
Here's Python code that prints a simple piece of HTML: Here's Python code that prints a simple piece of HTML:
\begin{verbatim} \begin{verbatim}
print "<TITLE>CGI script output</TITLE>" print "<TITLE>CGI script output</TITLE>"
print "<H1>This is my first CGI script</H1>" print "<H1>This is my first CGI script</H1>"
print "Hello, world!" print "Hello, world!"
\end{verbatim} \end{verbatim}
(It may not be fully legal HTML according to the letter of the (It may not be fully legal HTML according to the letter of the
...@@ -77,16 +77,16 @@ dictionary. For instance, the following code (which assumes that the ...@@ -77,16 +77,16 @@ dictionary. For instance, the following code (which assumes that the
the fields \code{name} and \code{addr} are both set to a non-empty string: the fields \code{name} and \code{addr} are both set to a non-empty string:
\begin{verbatim} \begin{verbatim}
form = cgi.FieldStorage() form = cgi.FieldStorage()
form_ok = 0 form_ok = 0
if form.has_key("name") and form.has_key("addr"): if form.has_key("name") and form.has_key("addr"):
if form["name"].value != "" and form["addr"].value != "": if form["name"].value != "" and form["addr"].value != "":
form_ok = 1 form_ok = 1
if not form_ok: if not form_ok:
print "<H1>Error</H1>" print "<H1>Error</H1>"
print "Please fill in the name and addr fields." print "Please fill in the name and addr fields."
return return
...further form processing here... ...further form processing here...
\end{verbatim} \end{verbatim}
Here the fields, accessed through \code{form[key]}, are themselves instances Here the fields, accessed through \code{form[key]}, are themselves instances
...@@ -101,20 +101,20 @@ instance or a list of instances. For example, here's code that ...@@ -101,20 +101,20 @@ instance or a list of instances. For example, here's code that
concatenates any number of username fields, separated by commas: concatenates any number of username fields, separated by commas:
\begin{verbatim} \begin{verbatim}
username = form["username"] username = form["username"]
if type(username) is type([]): if type(username) is type([]):
# Multiple username fields specified # Multiple username fields specified
usernames = "" usernames = ""
for item in username: for item in username:
if usernames: if usernames:
# Next item -- insert comma # Next item -- insert comma
usernames = usernames + "," + item.value usernames = usernames + "," + item.value
else: else:
# First item -- don't insert comma # First item -- don't insert comma
usernames = item.value usernames = item.value
else: else:
# Single username field specified # Single username field specified
usernames = username.value usernames = username.value
\end{verbatim} \end{verbatim}
If a field represents an uploaded file, the value attribute reads the If a field represents an uploaded file, the value attribute reads the
...@@ -124,14 +124,14 @@ file attribute. You can then read the data at leasure from the file ...@@ -124,14 +124,14 @@ file attribute. You can then read the data at leasure from the file
attribute: attribute:
\begin{verbatim} \begin{verbatim}
fileitem = form["userfile"] fileitem = form["userfile"]
if fileitem.file: if fileitem.file:
# It's an uploaded file; count lines # It's an uploaded file; count lines
linecount = 0 linecount = 0
while 1: while 1:
line = fileitem.file.readline() line = fileitem.file.readline()
if not line: break if not line: break
linecount = linecount + 1 linecount = linecount + 1
\end{verbatim} \end{verbatim}
The file upload draft standard entertains the possibility of uploading The file upload draft standard entertains the possibility of uploading
...@@ -252,7 +252,7 @@ that the first line of the script contains \code{\#!} starting in column 1 ...@@ -252,7 +252,7 @@ that the first line of the script contains \code{\#!} starting in column 1
followed by the pathname of the Python interpreter, for instance: followed by the pathname of the Python interpreter, for instance:
\begin{verbatim} \begin{verbatim}
#!/usr/local/bin/python #!/usr/local/bin/python
\end{verbatim} \end{verbatim}
Make sure the Python interpreter exists and is executable by ``others''. Make sure the Python interpreter exists and is executable by ``others''.
...@@ -274,9 +274,9 @@ default module search path, you can change the path in your script, ...@@ -274,9 +274,9 @@ default module search path, you can change the path in your script,
before importing other modules, e.g.: before importing other modules, e.g.:
\begin{verbatim} \begin{verbatim}
import sys import sys
sys.path.insert(0, "/usr/home/joe/lib/python") sys.path.insert(0, "/usr/home/joe/lib/python")
sys.path.insert(0, "/usr/local/lib/python") sys.path.insert(0, "/usr/local/lib/python")
\end{verbatim} \end{verbatim}
(This way, the directory inserted last will be searched first!) (This way, the directory inserted last will be searched first!)
...@@ -312,7 +312,7 @@ in the standard \code{cgi-bin} directory, it should be possible to send it a ...@@ -312,7 +312,7 @@ in the standard \code{cgi-bin} directory, it should be possible to send it a
request by entering a URL into your browser of the form: request by entering a URL into your browser of the form:
\begin{verbatim} \begin{verbatim}
http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
\end{verbatim} \end{verbatim}
If this gives an error of type 404, the server cannot find the script If this gives an error of type 404, the server cannot find the script
...@@ -329,9 +329,9 @@ The next step could be to call the \code{cgi} module's test() function from ...@@ -329,9 +329,9 @@ The next step could be to call the \code{cgi} module's test() function from
your script: replace its main code with the single statement your script: replace its main code with the single statement
\begin{verbatim} \begin{verbatim}
cgi.test() cgi.test()
\end{verbatim} \end{verbatim}
This should produce the same results as those gotten from installing This should produce the same results as those gotten from installing
the \code{cgi.py} file itself. the \code{cgi.py} file itself.
...@@ -364,16 +364,16 @@ Here are the rules: ...@@ -364,16 +364,16 @@ Here are the rules:
For example: For example:
\begin{verbatim} \begin{verbatim}
import sys import sys
import traceback import traceback
print "Content-type: text/html" print "Content-type: text/html"
print print
sys.stderr = sys.stdout sys.stderr = sys.stdout
try: try:
...your code here... ...your code here...
except: except:
print "\n\n<PRE>" print "\n\n<PRE>"
traceback.print_exc() traceback.print_exc()
\end{verbatim} \end{verbatim}
Notes: The assignment to \code{sys.stderr} is needed because the traceback Notes: The assignment to \code{sys.stderr} is needed because the traceback
...@@ -385,11 +385,11 @@ module, you can use an even more robust approach (which only uses ...@@ -385,11 +385,11 @@ module, you can use an even more robust approach (which only uses
built-in modules): built-in modules):
\begin{verbatim} \begin{verbatim}
import sys import sys
sys.stderr = sys.stdout sys.stderr = sys.stdout
print "Content-type: text/plain" print "Content-type: text/plain"
print print
...your code here... ...your code here...
\end{verbatim} \end{verbatim}
This relies on the Python interpreter to print the traceback. The This relies on the Python interpreter to print the traceback. The
......
...@@ -40,8 +40,8 @@ telling the client what kind of data is following. Python code to ...@@ -40,8 +40,8 @@ telling the client what kind of data is following. Python code to
generate a minimal header section looks like this: generate a minimal header section looks like this:
\begin{verbatim} \begin{verbatim}
print "Content-type: text/html" # HTML is following print "Content-type: text/html" # HTML is following
print # blank line, end of headers print # blank line, end of headers
\end{verbatim} \end{verbatim}
The second section is usually HTML, which allows the client software The second section is usually HTML, which allows the client software
...@@ -49,9 +49,9 @@ to display nicely formatted text with header, in-line images, etc. ...@@ -49,9 +49,9 @@ to display nicely formatted text with header, in-line images, etc.
Here's Python code that prints a simple piece of HTML: Here's Python code that prints a simple piece of HTML:
\begin{verbatim} \begin{verbatim}
print "<TITLE>CGI script output</TITLE>" print "<TITLE>CGI script output</TITLE>"
print "<H1>This is my first CGI script</H1>" print "<H1>This is my first CGI script</H1>"
print "Hello, world!" print "Hello, world!"
\end{verbatim} \end{verbatim}
(It may not be fully legal HTML according to the letter of the (It may not be fully legal HTML according to the letter of the
...@@ -77,16 +77,16 @@ dictionary. For instance, the following code (which assumes that the ...@@ -77,16 +77,16 @@ dictionary. For instance, the following code (which assumes that the
the fields \code{name} and \code{addr} are both set to a non-empty string: the fields \code{name} and \code{addr} are both set to a non-empty string:
\begin{verbatim} \begin{verbatim}
form = cgi.FieldStorage() form = cgi.FieldStorage()
form_ok = 0 form_ok = 0
if form.has_key("name") and form.has_key("addr"): if form.has_key("name") and form.has_key("addr"):
if form["name"].value != "" and form["addr"].value != "": if form["name"].value != "" and form["addr"].value != "":
form_ok = 1 form_ok = 1
if not form_ok: if not form_ok:
print "<H1>Error</H1>" print "<H1>Error</H1>"
print "Please fill in the name and addr fields." print "Please fill in the name and addr fields."
return return
...further form processing here... ...further form processing here...
\end{verbatim} \end{verbatim}
Here the fields, accessed through \code{form[key]}, are themselves instances Here the fields, accessed through \code{form[key]}, are themselves instances
...@@ -101,20 +101,20 @@ instance or a list of instances. For example, here's code that ...@@ -101,20 +101,20 @@ instance or a list of instances. For example, here's code that
concatenates any number of username fields, separated by commas: concatenates any number of username fields, separated by commas:
\begin{verbatim} \begin{verbatim}
username = form["username"] username = form["username"]
if type(username) is type([]): if type(username) is type([]):
# Multiple username fields specified # Multiple username fields specified
usernames = "" usernames = ""
for item in username: for item in username:
if usernames: if usernames:
# Next item -- insert comma # Next item -- insert comma
usernames = usernames + "," + item.value usernames = usernames + "," + item.value
else: else:
# First item -- don't insert comma # First item -- don't insert comma
usernames = item.value usernames = item.value
else: else:
# Single username field specified # Single username field specified
usernames = username.value usernames = username.value
\end{verbatim} \end{verbatim}
If a field represents an uploaded file, the value attribute reads the If a field represents an uploaded file, the value attribute reads the
...@@ -124,14 +124,14 @@ file attribute. You can then read the data at leasure from the file ...@@ -124,14 +124,14 @@ file attribute. You can then read the data at leasure from the file
attribute: attribute:
\begin{verbatim} \begin{verbatim}
fileitem = form["userfile"] fileitem = form["userfile"]
if fileitem.file: if fileitem.file:
# It's an uploaded file; count lines # It's an uploaded file; count lines
linecount = 0 linecount = 0
while 1: while 1:
line = fileitem.file.readline() line = fileitem.file.readline()
if not line: break if not line: break
linecount = linecount + 1 linecount = linecount + 1
\end{verbatim} \end{verbatim}
The file upload draft standard entertains the possibility of uploading The file upload draft standard entertains the possibility of uploading
...@@ -252,7 +252,7 @@ that the first line of the script contains \code{\#!} starting in column 1 ...@@ -252,7 +252,7 @@ that the first line of the script contains \code{\#!} starting in column 1
followed by the pathname of the Python interpreter, for instance: followed by the pathname of the Python interpreter, for instance:
\begin{verbatim} \begin{verbatim}
#!/usr/local/bin/python #!/usr/local/bin/python
\end{verbatim} \end{verbatim}
Make sure the Python interpreter exists and is executable by ``others''. Make sure the Python interpreter exists and is executable by ``others''.
...@@ -274,9 +274,9 @@ default module search path, you can change the path in your script, ...@@ -274,9 +274,9 @@ default module search path, you can change the path in your script,
before importing other modules, e.g.: before importing other modules, e.g.:
\begin{verbatim} \begin{verbatim}
import sys import sys
sys.path.insert(0, "/usr/home/joe/lib/python") sys.path.insert(0, "/usr/home/joe/lib/python")
sys.path.insert(0, "/usr/local/lib/python") sys.path.insert(0, "/usr/local/lib/python")
\end{verbatim} \end{verbatim}
(This way, the directory inserted last will be searched first!) (This way, the directory inserted last will be searched first!)
...@@ -312,7 +312,7 @@ in the standard \code{cgi-bin} directory, it should be possible to send it a ...@@ -312,7 +312,7 @@ in the standard \code{cgi-bin} directory, it should be possible to send it a
request by entering a URL into your browser of the form: request by entering a URL into your browser of the form:
\begin{verbatim} \begin{verbatim}
http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
\end{verbatim} \end{verbatim}
If this gives an error of type 404, the server cannot find the script If this gives an error of type 404, the server cannot find the script
...@@ -329,9 +329,9 @@ The next step could be to call the \code{cgi} module's test() function from ...@@ -329,9 +329,9 @@ The next step could be to call the \code{cgi} module's test() function from
your script: replace its main code with the single statement your script: replace its main code with the single statement
\begin{verbatim} \begin{verbatim}
cgi.test() cgi.test()
\end{verbatim} \end{verbatim}
This should produce the same results as those gotten from installing This should produce the same results as those gotten from installing
the \code{cgi.py} file itself. the \code{cgi.py} file itself.
...@@ -364,16 +364,16 @@ Here are the rules: ...@@ -364,16 +364,16 @@ Here are the rules:
For example: For example:
\begin{verbatim} \begin{verbatim}
import sys import sys
import traceback import traceback
print "Content-type: text/html" print "Content-type: text/html"
print print
sys.stderr = sys.stdout sys.stderr = sys.stdout
try: try:
...your code here... ...your code here...
except: except:
print "\n\n<PRE>" print "\n\n<PRE>"
traceback.print_exc() traceback.print_exc()
\end{verbatim} \end{verbatim}
Notes: The assignment to \code{sys.stderr} is needed because the traceback Notes: The assignment to \code{sys.stderr} is needed because the traceback
...@@ -385,11 +385,11 @@ module, you can use an even more robust approach (which only uses ...@@ -385,11 +385,11 @@ module, you can use an even more robust approach (which only uses
built-in modules): built-in modules):
\begin{verbatim} \begin{verbatim}
import sys import sys
sys.stderr = sys.stdout sys.stderr = sys.stdout
print "Content-type: text/plain" print "Content-type: text/plain"
print print
...your code here... ...your code here...
\end{verbatim} \end{verbatim}
This relies on the Python interpreter to print the traceback. The This relies on the Python interpreter to print the traceback. The
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment