diff options
author | Guido van Rossum <guido@python.org> | 1997-02-28 16:37:49 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-02-28 16:37:49 (GMT) |
commit | 20e4210ec4b7303f966939484c0d0023060cc442 (patch) | |
tree | 376403940d8bbb338d45c1cd3b4d1771040ee7bc /Doc/lib/libcgi.tex | |
parent | 17e973cb32e96151987f9e7b0a8775934bf9e8a6 (diff) | |
download | cpython-20e4210ec4b7303f966939484c0d0023060cc442.zip cpython-20e4210ec4b7303f966939484c0d0023060cc442.tar.gz cpython-20e4210ec4b7303f966939484c0d0023060cc442.tar.bz2 |
Fix up indentation of examples to use 4 spaces instead of tabs.
Diffstat (limited to 'Doc/lib/libcgi.tex')
-rw-r--r-- | Doc/lib/libcgi.tex | 118 |
1 files changed, 59 insertions, 59 deletions
diff --git a/Doc/lib/libcgi.tex b/Doc/lib/libcgi.tex index d736f39..4768f0f 100644 --- a/Doc/lib/libcgi.tex +++ b/Doc/lib/libcgi.tex @@ -40,8 +40,8 @@ telling the client what kind of data is following. Python code to generate a minimal header section looks like this: \begin{verbatim} - print "Content-type: text/html" # HTML is following - print # blank line, end of headers + print "Content-type: text/html" # HTML is following + print # blank line, end of headers \end{verbatim} 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. Here's Python code that prints a simple piece of HTML: \begin{verbatim} - print "<TITLE>CGI script output</TITLE>" - print "<H1>This is my first CGI script</H1>" - print "Hello, world!" + print "<TITLE>CGI script output</TITLE>" + print "<H1>This is my first CGI script</H1>" + print "Hello, world!" \end{verbatim} (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 the fields \code{name} and \code{addr} are both set to a non-empty string: \begin{verbatim} - form = cgi.FieldStorage() - form_ok = 0 - if form.has_key("name") and form.has_key("addr"): - if form["name"].value != "" and form["addr"].value != "": - form_ok = 1 - if not form_ok: - print "<H1>Error</H1>" - print "Please fill in the name and addr fields." - return - ...further form processing here... + form = cgi.FieldStorage() + form_ok = 0 + if form.has_key("name") and form.has_key("addr"): + if form["name"].value != "" and form["addr"].value != "": + form_ok = 1 + if not form_ok: + print "<H1>Error</H1>" + print "Please fill in the name and addr fields." + return + ...further form processing here... \end{verbatim} 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 concatenates any number of username fields, separated by commas: \begin{verbatim} - username = form["username"] - if type(username) is type([]): - # Multiple username fields specified - usernames = "" - for item in username: - if usernames: - # Next item -- insert comma - usernames = usernames + "," + item.value - else: - # First item -- don't insert comma - usernames = item.value - else: - # Single username field specified - usernames = username.value + username = form["username"] + if type(username) is type([]): + # Multiple username fields specified + usernames = "" + for item in username: + if usernames: + # Next item -- insert comma + usernames = usernames + "," + item.value + else: + # First item -- don't insert comma + usernames = item.value + else: + # Single username field specified + usernames = username.value \end{verbatim} 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 attribute: \begin{verbatim} - fileitem = form["userfile"] - if fileitem.file: - # It's an uploaded file; count lines - linecount = 0 - while 1: - line = fileitem.file.readline() - if not line: break - linecount = linecount + 1 + fileitem = form["userfile"] + if fileitem.file: + # It's an uploaded file; count lines + linecount = 0 + while 1: + line = fileitem.file.readline() + if not line: break + linecount = linecount + 1 \end{verbatim} 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 followed by the pathname of the Python interpreter, for instance: \begin{verbatim} - #!/usr/local/bin/python + #!/usr/local/bin/python \end{verbatim} 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, before importing other modules, e.g.: \begin{verbatim} - import sys - sys.path.insert(0, "/usr/home/joe/lib/python") - sys.path.insert(0, "/usr/local/lib/python") + import sys + sys.path.insert(0, "/usr/home/joe/lib/python") + sys.path.insert(0, "/usr/local/lib/python") \end{verbatim} (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 request by entering a URL into your browser of the form: \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} 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 your script: replace its main code with the single statement \begin{verbatim} - cgi.test() + cgi.test() \end{verbatim} - + This should produce the same results as those gotten from installing the \code{cgi.py} file itself. @@ -364,16 +364,16 @@ Here are the rules: For example: \begin{verbatim} - import sys - import traceback - print "Content-type: text/html" - print - sys.stderr = sys.stdout - try: - ...your code here... - except: - print "\n\n<PRE>" - traceback.print_exc() + import sys + import traceback + print "Content-type: text/html" + print + sys.stderr = sys.stdout + try: + ...your code here... + except: + print "\n\n<PRE>" + traceback.print_exc() \end{verbatim} 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 built-in modules): \begin{verbatim} - import sys - sys.stderr = sys.stdout - print "Content-type: text/plain" - print - ...your code here... + import sys + sys.stderr = sys.stdout + print "Content-type: text/plain" + print + ...your code here... \end{verbatim} This relies on the Python interpreter to print the traceback. The |