annotate Lib/cgi.py @ 9383:2b34461ba4a3 legacy-trunk

PyMapping_SetItemString() returns int, not an object.
author Guido van Rossum <guido@python.org>
date Fri, 16 Oct 1998 17:43:53 +0000
parents fac33716a218
children 620e02250c80
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5128
bf1bd64f85e0 Put back #! /usr/local/bin/python. For cgi scripts, /usr/bin/env is
Guido van Rossum <guido@python.org>
parents: 4931
diff changeset
1 #! /usr/local/bin/python
3106
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
2
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
3 """Support module for CGI (Common Gateway Interface) scripts.
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
4
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
5 This module defines a number of utilities for use by CGI scripts
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
6 written in Python.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
7
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
8
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
9 Introduction
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
10 ------------
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
11
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
12 A CGI script is invoked by an HTTP server, usually to process user
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
13 input submitted through an HTML <FORM> or <ISINPUT> element.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
14
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
15 Most often, CGI scripts live in the server's special cgi-bin
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
16 directory. The HTTP server places all sorts of information about the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
17 request (such as the client's hostname, the requested URL, the query
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
18 string, and lots of other goodies) in the script's shell environment,
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
19 executes the script, and sends the script's output back to the client.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
20
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
21 The script's input is connected to the client too, and sometimes the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
22 form data is read this way; at other times the form data is passed via
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
23 the "query string" part of the URL. This module (cgi.py) is intended
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
24 to take care of the different cases and provide a simpler interface to
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
25 the Python script. It also provides a number of utilities that help
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
26 in debugging scripts, and the latest addition is support for file
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
27 uploads from a form (if your browser supports it -- Grail 0.3 and
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
28 Netscape 2.0 do).
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
29
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
30 The output of a CGI script should consist of two sections, separated
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
31 by a blank line. The first section contains a number of headers,
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
32 telling the client what kind of data is following. Python code to
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
33 generate a minimal header section looks like this:
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
34
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
35 print "Content-type: text/html" # HTML is following
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
36 print # blank line, end of headers
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
37
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
38 The second section is usually HTML, which allows the client software
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
39 to display nicely formatted text with header, in-line images, etc.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
40 Here's Python code that prints a simple piece of HTML:
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
41
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
42 print "<TITLE>CGI script output</TITLE>"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
43 print "<H1>This is my first CGI script</H1>"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
44 print "Hello, world!"
3106
c41a8e501973 handle missing QUERY_STRING
Guido van Rossum <guido@python.org>
parents: 2918
diff changeset
45
5652
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
46 It may not be fully legal HTML according to the letter of the
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
47 standard, but any browser will understand it.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
48
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
49
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
50 Using the cgi module
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
51 --------------------
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
52
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
53 Begin by writing "import cgi". Don't use "from cgi import *" -- the
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
54 module defines all sorts of names for its own use or for backward
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
55 compatibility that you don't want in your namespace.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
56
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
57 It's best to use the FieldStorage class. The other classes define in this
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
58 module are provided mostly for backward compatibility. Instantiate it
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
59 exactly once, without arguments. This reads the form contents from
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
60 standard input or the environment (depending on the value of various
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
61 environment variables set according to the CGI standard). Since it may
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
62 consume standard input, it should be instantiated only once.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
63
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
64 The FieldStorage instance can be accessed as if it were a Python
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
65 dictionary. For instance, the following code (which assumes that the
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
66 Content-type header and blank line have already been printed) checks that
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
67 the fields "name" and "addr" are both set to a non-empty string:
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
68
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
69 form = cgi.FieldStorage()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
70 form_ok = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
71 if form.has_key("name") and form.has_key("addr"):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
72 if form["name"].value != "" and form["addr"].value != "":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
73 form_ok = 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
74 if not form_ok:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
75 print "<H1>Error</H1>"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
76 print "Please fill in the name and addr fields."
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
77 return
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
78 ...further form processing here...
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
79
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
80 Here the fields, accessed through form[key], are themselves instances
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
81 of FieldStorage (or MiniFieldStorage, depending on the form encoding).
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
82
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
83 If the submitted form data contains more than one field with the same
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
84 name, the object retrieved by form[key] is not a (Mini)FieldStorage
5652
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
85 instance but a list of such instances. If you are expecting this
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
86 possibility (i.e., when your HTML form comtains multiple fields with
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
87 the same name), use the type() function to determine whether you have
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
88 a single instance or a list of instances. For example, here's code
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
89 that concatenates any number of username fields, separated by commas:
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
90
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
91 username = form["username"]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
92 if type(username) is type([]):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
93 # Multiple username fields specified
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
94 usernames = ""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
95 for item in username:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
96 if usernames:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
97 # Next item -- insert comma
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
98 usernames = usernames + "," + item.value
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
99 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
100 # First item -- don't insert comma
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
101 usernames = item.value
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
102 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
103 # Single username field specified
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
104 usernames = username.value
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
105
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
106 If a field represents an uploaded file, the value attribute reads the
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
107 entire file in memory as a string. This may not be what you want. You can
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
108 test for an uploaded file by testing either the filename attribute or the
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
109 file attribute. You can then read the data at leasure from the file
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
110 attribute:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
111
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
112 fileitem = form["userfile"]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
113 if fileitem.file:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
114 # It's an uploaded file; count lines
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
115 linecount = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
116 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
117 line = fileitem.file.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
118 if not line: break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
119 linecount = linecount + 1
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
120
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
121 The file upload draft standard entertains the possibility of uploading
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
122 multiple files from one field (using a recursive multipart/*
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
123 encoding). When this occurs, the item will be a dictionary-like
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
124 FieldStorage item. This can be determined by testing its type
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
125 attribute, which should have the value "multipart/form-data" (or
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
126 perhaps another string beginning with "multipart/"). It this case, it
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
127 can be iterated over recursively just like the top-level form object.
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
128
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
129 When a form is submitted in the "old" format (as the query string or as a
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
130 single data part of type application/x-www-form-urlencoded), the items
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
131 will actually be instances of the class MiniFieldStorage. In this case,
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
132 the list, file and filename attributes are always None.
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
133
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
134
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
135 Old classes
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
136 -----------
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
137
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
138 These classes, present in earlier versions of the cgi module, are still
4486
3d43510b5e19 Fix truncated paragraph in doc string.
Guido van Rossum <guido@python.org>
parents: 4214
diff changeset
139 supported for backward compatibility. New applications should use the
3d43510b5e19 Fix truncated paragraph in doc string.
Guido van Rossum <guido@python.org>
parents: 4214
diff changeset
140 FieldStorage class.
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
141
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
142 SvFormContentDict: single value form content as dictionary; assumes each
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
143 field name occurs in the form only once.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
144
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
145 FormContentDict: multiple value form content as dictionary (the form
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
146 items are lists of values). Useful if your form contains multiple
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
147 fields with the same name.
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
149 Other classes (FormContent, InterpFormContentDict) are present for
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
150 backwards compatibility with really old applications only. If you still
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
151 use these and would be inconvenienced when they disappeared from a next
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
152 version of this module, drop me a note.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
153
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
154
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
155 Functions
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
156 ---------
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
157
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
158 These are useful if you want more control, or if you want to employ
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
159 some of the algorithms implemented in this module in other
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
160 circumstances.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
161
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
162 parse(fp, [environ, [keep_blank_values, [strict_parsing]]]): parse a
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
163 form into a Python dictionary.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
164
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
165 parse_qs(qs, [keep_blank_values, [strict_parsing]]): parse a query
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
166 string (data of type application/x-www-form-urlencoded).
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
167
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
168 parse_multipart(fp, pdict): parse input of type multipart/form-data (for
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
169 file uploads).
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
170
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
171 parse_header(string): parse a header like Content-type into a main
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
172 value and a dictionary of parameters.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
173
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
174 test(): complete test program.
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
175
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
176 print_environ(): format the shell environment in HTML.
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
177
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
178 print_form(form): format a form in HTML.
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
179
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
180 print_environ_usage(): print a list of useful environment variables in
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
181 HTML.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
182
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
183 escape(): convert the characters "&", "<" and ">" to HTML-safe
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
184 sequences. Use this if you need to display text that might contain
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
185 such characters in HTML. To translate URLs for inclusion in the HREF
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
186 attribute of an <A> tag, use urllib.quote().
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
187
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
188 log(fmt, ...): write a line to a log file; see docs for initlog().
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
189
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
190
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
191 Caring about security
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
192 ---------------------
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
193
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
194 There's one important rule: if you invoke an external program (e.g.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
195 via the os.system() or os.popen() functions), make very sure you don't
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
196 pass arbitrary strings received from the client to the shell. This is
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
197 a well-known security hole whereby clever hackers anywhere on the web
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
198 can exploit a gullible CGI script to invoke arbitrary shell commands.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
199 Even parts of the URL or field names cannot be trusted, since the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
200 request doesn't have to come from your form!
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
202 To be on the safe side, if you must pass a string gotten from a form
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
203 to a shell command, you should make sure the string contains only
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
204 alphanumeric characters, dashes, underscores, and periods.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
205
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
206
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
207 Installing your CGI script on a Unix system
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
208 -------------------------------------------
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
209
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
210 Read the documentation for your HTTP server and check with your local
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
211 system administrator to find the directory where CGI scripts should be
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
212 installed; usually this is in a directory cgi-bin in the server tree.
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
213
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
214 Make sure that your script is readable and executable by "others"; the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
215 Unix file mode should be 755 (use "chmod 755 filename"). Make sure
5652
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
216 that the first line of the script contains #! starting in column 1
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
217 followed by the pathname of the Python interpreter, for instance:
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
218
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
219 #! /usr/local/bin/python
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
220
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
221 Make sure the Python interpreter exists and is executable by "others".
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
222
5652
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
223 Note that it's probably not a good idea to use #! /usr/bin/env python
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 4534
diff changeset
224 here, since the Python interpreter may not be on the default path
5652
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
225 given to CGI scripts!!!
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 4534
diff changeset
226
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
227 Make sure that any files your script needs to read or write are
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
228 readable or writable, respectively, by "others" -- their mode should
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
229 be 644 for readable and 666 for writable. This is because, for
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
230 security reasons, the HTTP server executes your script as user
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
231 "nobody", without any special privileges. It can only read (write,
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
232 execute) files that everybody can read (write, execute). The current
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
233 directory at execution time is also different (it is usually the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
234 server's cgi-bin directory) and the set of environment variables is
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
235 also different from what you get at login. in particular, don't count
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
236 on the shell's search path for executables ($PATH) or the Python
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
237 module search path ($PYTHONPATH) to be set to anything interesting.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
238
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
239 If you need to load modules from a directory which is not on Python's
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
240 default module search path, you can change the path in your script,
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
241 before importing other modules, e.g.:
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
242
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
243 import sys
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
244 sys.path.insert(0, "/usr/home/joe/lib/python")
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
245 sys.path.insert(0, "/usr/local/lib/python")
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
246
5652
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
247 This way, the directory inserted last will be searched first!
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
248
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
249 Instructions for non-Unix systems will vary; check your HTTP server's
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
250 documentation (it will usually have a section on CGI scripts).
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
251
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
252
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
253 Testing your CGI script
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
254 -----------------------
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
255
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
256 Unfortunately, a CGI script will generally not run when you try it
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
257 from the command line, and a script that works perfectly from the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
258 command line may fail mysteriously when run from the server. There's
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
259 one reason why you should still test your script from the command
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
260 line: if it contains a syntax error, the python interpreter won't
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
261 execute it at all, and the HTTP server will most likely send a cryptic
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
262 error to the client.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
263
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
264 Assuming your script has no syntax errors, yet it does not work, you
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
265 have no choice but to read the next section:
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
266
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
267
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
268 Debugging CGI scripts
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
269 ---------------------
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
270
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
271 First of all, check for trivial installation errors -- reading the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
272 section above on installing your CGI script carefully can save you a
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
273 lot of time. If you wonder whether you have understood the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
274 installation procedure correctly, try installing a copy of this module
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
275 file (cgi.py) as a CGI script. When invoked as a script, the file
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
276 will dump its environment and the contents of the form in HTML form.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
277 Give it the right mode etc, and send it a request. If it's installed
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
278 in the standard cgi-bin directory, it should be possible to send it a
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
279 request by entering a URL into your browser of the form:
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
280
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
281 http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
282
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
283 If this gives an error of type 404, the server cannot find the script
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
284 -- perhaps you need to install it in a different directory. If it
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
285 gives another error (e.g. 500), there's an installation problem that
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
286 you should fix before trying to go any further. If you get a nicely
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
287 formatted listing of the environment and form content (in this
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
288 example, the fields should be listed as "addr" with value "At Home"
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
289 and "name" with value "Joe Blow"), the cgi.py script has been
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
290 installed correctly. If you follow the same procedure for your own
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
291 script, you should now be able to debug it.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
292
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
293 The next step could be to call the cgi module's test() function from
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
294 your script: replace its main code with the single statement
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
295
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
296 cgi.test()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
297
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
298 This should produce the same results as those gotten from installing
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
299 the cgi.py file itself.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
300
5652
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
301 When an ordinary Python script raises an unhandled exception (e.g.,
6009d5390584 Reworded the doc string to remove the need for The Emacs font-lock kludge.
Guido van Rossum <guido@python.org>
parents: 5499
diff changeset
302 because of a typo in a module name, a file that can't be opened,
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
303 etc.), the Python interpreter prints a nice traceback and exits.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
304 While the Python interpreter will still do this when your CGI script
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
305 raises an exception, most likely the traceback will end up in one of
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
306 the HTTP server's log file, or be discarded altogether.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
307
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
308 Fortunately, once you have managed to get your script to execute
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
309 *some* code, it is easy to catch exceptions and cause a traceback to
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
310 be printed. The test() function below in this module is an example.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
311 Here are the rules:
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
312
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
313 1. Import the traceback module (before entering the
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
314 try-except!)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
315
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
316 2. Make sure you finish printing the headers and the blank
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
317 line early
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
318
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
319 3. Assign sys.stderr to sys.stdout
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
320
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
321 3. Wrap all remaining code in a try-except statement
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
322
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
323 4. In the except clause, call traceback.print_exc()
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
324
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
325 For example:
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
326
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
327 import sys
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
328 import traceback
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
329 print "Content-type: text/html"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
330 print
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
331 sys.stderr = sys.stdout
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
332 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
333 ...your code here...
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
334 except:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
335 print "\n\n<PRE>"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
336 traceback.print_exc()
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
337
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
338 Notes: The assignment to sys.stderr is needed because the traceback
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
339 prints to sys.stderr. The print "\n\n<PRE>" statement is necessary to
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
340 disable the word wrapping in HTML.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
341
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
342 If you suspect that there may be a problem in importing the traceback
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
343 module, you can use an even more robust approach (which only uses
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
344 built-in modules):
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
345
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
346 import sys
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
347 sys.stderr = sys.stdout
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
348 print "Content-type: text/plain"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
349 print
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
350 ...your code here...
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
351
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
352 This relies on the Python interpreter to print the traceback. The
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
353 content type of the output is set to plain text, which disables all
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
354 HTML processing. If your script works, the raw HTML will be displayed
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
355 by your client. If it raises an exception, most likely after the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
356 first two lines have been printed, a traceback will be displayed.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
357 Because no HTML interpretation is going on, the traceback will
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
358 readable.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
359
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
360 When all else fails, you may want to insert calls to log() to your
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
361 program or even to a copy of the cgi.py file. Note that this requires
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
362 you to set cgi.logfile to the name of a world-writable file before the
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
363 first call to log() is made!
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
364
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
365 Good luck!
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
366
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
367
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
368 Common problems and solutions
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
369 -----------------------------
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
370
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
371 - Most HTTP servers buffer the output from CGI scripts until the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
372 script is completed. This means that it is not possible to display a
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
373 progress report on the client's display while the script is running.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
374
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
375 - Check the installation instructions above.
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
376
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
377 - Check the HTTP server's log files. ("tail -f logfile" in a separate
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
378 window may be useful!)
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
379
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
380 - Always check a script for syntax errors first, by doing something
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
381 like "python script.py".
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
382
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
383 - When using any of the debugging techniques, don't forget to add
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
384 "import sys" to the top of the script.
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
385
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
386 - When invoking external programs, make sure they can be found.
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
387 Usually, this means using absolute path names -- $PATH is usually not
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
388 set to a very useful value in a CGI script.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
389
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
390 - When reading or writing external files, make sure they can be read
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
391 or written by every user on the system.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
392
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
393 - Don't try to give a CGI script a set-uid mode. This doesn't work on
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
394 most systems, and is a security liability as well.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
395
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
396
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
397 History
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
398 -------
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
399
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
400 Michael McLay started this module. Steve Majewski changed the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
401 interface to SvFormContentDict and FormContentDict. The multipart
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
402 parsing was inspired by code submitted by Andreas Paepcke. Guido van
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
403 Rossum rewrote, reformatted and documented the module and is currently
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
404 responsible for its maintenance.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
405
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
406
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
407 XXX The module is getting pretty heavy with all those docstrings.
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
408 Perhaps there should be a slimmed version that doesn't contain all those
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
409 backwards compatible and debugging classes and functions?
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
410
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
411 """
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
412
5302
5e3bcf4ebf29 From: Clarence Gardner <clarence@king.silcom.com>
Guido van Rossum <guido@python.org>
parents: 5128
diff changeset
413 __version__ = "2.2"
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
414
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
415
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
416 # Imports
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
417 # =======
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
418
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
419 import string
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
420 import sys
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
421 import os
5988
5993a099d9b0 Changes suggested by Jim fulton:
Guido van Rossum <guido@python.org>
parents: 5870
diff changeset
422 import urllib
5993a099d9b0 Changes suggested by Jim fulton:
Guido van Rossum <guido@python.org>
parents: 5870
diff changeset
423 import mimetools
5993a099d9b0 Changes suggested by Jim fulton:
Guido van Rossum <guido@python.org>
parents: 5870
diff changeset
424 import rfc822
5993a099d9b0 Changes suggested by Jim fulton:
Guido van Rossum <guido@python.org>
parents: 5870
diff changeset
425 from StringIO import StringIO
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
426
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
427
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
428 # Logging support
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
429 # ===============
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
430
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
431 logfile = "" # Filename to log to, if not empty
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
432 logfp = None # File object to log to, if not None
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
433
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
434 def initlog(*allargs):
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
435 """Write a log message, if there is a log file.
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
436
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
437 Even though this function is called initlog(), you should always
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
438 use log(); log is a variable that is set either to initlog
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
439 (initially), to dolog (once the log file has been opened), or to
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
440 nolog (when logging is disabled).
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
441
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
442 The first argument is a format string; the remaining arguments (if
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
443 any) are arguments to the % operator, so e.g.
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
444 log("%s: %s", "a", "b")
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
445 will write "a: b" to the log file, followed by a newline.
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
446
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
447 If the global logfp is not None, it should be a file object to
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
448 which log data is written.
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
449
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
450 If the global logfp is None, the global logfile may be a string
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
451 giving a filename to open, in append mode. This file should be
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
452 world writable!!! If the file can't be opened, logging is
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
453 silently disabled (since there is no safe place where we could
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
454 send an error message).
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
455
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
456 """
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
457 global logfp, log
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
458 if logfile and not logfp:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
459 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
460 logfp = open(logfile, "a")
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
461 except IOError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
462 pass
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
463 if not logfp:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
464 log = nolog
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
465 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
466 log = dolog
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
467 apply(log, allargs)
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
468
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
469 def dolog(fmt, *args):
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
470 """Write a log message to the log file. See initlog() for docs."""
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
471 logfp.write(fmt%args + "\n")
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
472
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
473 def nolog(*allargs):
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
474 """Dummy function, assigned to log when logging is disabled."""
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
475 pass
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
476
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
477 log = initlog # The current logging function
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
478
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
479
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
480 # Parsing functions
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
481 # =================
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
482
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
483 # Maximum input we will accept when REQUEST_METHOD is POST
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
484 # 0 ==> unlimited input
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
485 maxlen = 0
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
486
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
487 def parse(fp=None, environ=os.environ, keep_blank_values=0, strict_parsing=0):
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
488 """Parse a query in the environment or from a file (default stdin)
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
489
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
490 Arguments, all optional:
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
491
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
492 fp : file pointer; default: sys.stdin
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
493
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
494 environ : environment dictionary; default: os.environ
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
495
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
496 keep_blank_values: flag indicating whether blank values in
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
497 URL encoded forms should be treated as blank strings.
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
498 A true value inicates that blanks should be retained as
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
499 blank strings. The default false value indicates that
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
500 blank values are to be ignored and treated as if they were
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
501 not included.
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
502
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
503 strict_parsing: flag indicating what to do with parsing errors.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
504 If false (the default), errors are silently ignored.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
505 If true, errors raise a ValueError exception.
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
506 """
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
507 if not fp:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
508 fp = sys.stdin
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
509 if not environ.has_key('REQUEST_METHOD'):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
510 environ['REQUEST_METHOD'] = 'GET' # For testing stand-alone
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
511 if environ['REQUEST_METHOD'] == 'POST':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
512 ctype, pdict = parse_header(environ['CONTENT_TYPE'])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
513 if ctype == 'multipart/form-data':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
514 return parse_multipart(fp, pdict)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
515 elif ctype == 'application/x-www-form-urlencoded':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
516 clength = string.atoi(environ['CONTENT_LENGTH'])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
517 if maxlen and clength > maxlen:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
518 raise ValueError, 'Maximum content length exceeded'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
519 qs = fp.read(clength)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
520 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
521 qs = '' # Unknown content-type
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
522 if environ.has_key('QUERY_STRING'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
523 if qs: qs = qs + '&'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
524 qs = qs + environ['QUERY_STRING']
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
525 elif sys.argv[1:]:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
526 if qs: qs = qs + '&'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
527 qs = qs + sys.argv[1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
528 environ['QUERY_STRING'] = qs # XXX Shouldn't, really
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
529 elif environ.has_key('QUERY_STRING'):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
530 qs = environ['QUERY_STRING']
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
531 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
532 if sys.argv[1:]:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
533 qs = sys.argv[1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
534 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
535 qs = ""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
536 environ['QUERY_STRING'] = qs # XXX Shouldn't, really
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
537 return parse_qs(qs, keep_blank_values, strict_parsing)
2918
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
538
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
539
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
540 def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
541 """Parse a query given as a string argument.
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
542
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
543 Arguments:
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
544
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
545 qs: URL-encoded query string to be parsed
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
546
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
547 keep_blank_values: flag indicating whether blank values in
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
548 URL encoded queries should be treated as blank strings.
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
549 A true value inicates that blanks should be retained as
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
550 blank strings. The default false value indicates that
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
551 blank values are to be ignored and treated as if they were
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
552 not included.
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
553
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
554 strict_parsing: flag indicating what to do with parsing errors.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
555 If false (the default), errors are silently ignored.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
556 If true, errors raise a ValueError exception.
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
557 """
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
558 name_value_pairs = string.splitfields(qs, '&')
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
559 dict = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
560 for name_value in name_value_pairs:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
561 nv = string.splitfields(name_value, '=')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
562 if len(nv) != 2:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
563 if strict_parsing:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
564 raise ValueError, "bad query field: %s" % `name_value`
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
565 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
566 name = urllib.unquote(string.replace(nv[0], '+', ' '))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
567 value = urllib.unquote(string.replace(nv[1], '+', ' '))
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
568 if len(value) or keep_blank_values:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
569 if dict.has_key (name):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
570 dict[name].append(value)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
571 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
572 dict[name] = [value]
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
573 return dict
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
574
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
575
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
576 def parse_multipart(fp, pdict):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
577 """Parse multipart input.
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
578
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
579 Arguments:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
580 fp : input file
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
581 pdict: dictionary containing other parameters of conten-type header
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
582
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
583 Returns a dictionary just like parse_qs(): keys are the field names, each
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
584 value is a list of values for that field. This is easy to use but not
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
585 much good if you are expecting megabytes to be uploaded -- in that case,
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
586 use the FieldStorage class instead which is much more flexible. Note
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
587 that content-type is the raw, unparsed contents of the content-type
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
588 header.
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
589
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
590 XXX This does not parse nested multipart parts -- use FieldStorage for
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
591 that.
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
592
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
593 XXX This should really be subsumed by FieldStorage altogether -- no
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
594 point in having two implementations of the same parsing algorithm.
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
595
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
596 """
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
597 if pdict.has_key('boundary'):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
598 boundary = pdict['boundary']
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
599 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
600 boundary = ""
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
601 nextpart = "--" + boundary
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
602 lastpart = "--" + boundary + "--"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
603 partdict = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
604 terminator = ""
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
605
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
606 while terminator != lastpart:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
607 bytes = -1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
608 data = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
609 if terminator:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
610 # At start of next part. Read headers first.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
611 headers = mimetools.Message(fp)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
612 clength = headers.getheader('content-length')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
613 if clength:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
614 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
615 bytes = string.atoi(clength)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
616 except string.atoi_error:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
617 pass
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
618 if bytes > 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
619 if maxlen and bytes > maxlen:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
620 raise ValueError, 'Maximum content length exceeded'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
621 data = fp.read(bytes)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
622 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
623 data = ""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
624 # Read lines until end of part.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
625 lines = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
626 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
627 line = fp.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
628 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
629 terminator = lastpart # End outer loop
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
630 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
631 if line[:2] == "--":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
632 terminator = string.strip(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
633 if terminator in (nextpart, lastpart):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
634 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
635 lines.append(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
636 # Done with part.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
637 if data is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
638 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
639 if bytes < 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
640 if lines:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
641 # Strip final line terminator
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
642 line = lines[-1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
643 if line[-2:] == "\r\n":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
644 line = line[:-2]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
645 elif line[-1:] == "\n":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
646 line = line[:-1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
647 lines[-1] = line
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
648 data = string.joinfields(lines, "")
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
649 line = headers['content-disposition']
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
650 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
651 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
652 key, params = parse_header(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
653 if key != 'form-data':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
654 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
655 if params.has_key('name'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
656 name = params['name']
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
657 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
658 continue
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
659 if partdict.has_key(name):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
660 partdict[name].append(data)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
661 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
662 partdict[name] = [data]
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
663
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
664 return partdict
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
665
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
666
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
667 def parse_header(line):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
668 """Parse a Content-type like header.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
669
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
670 Return the main content-type and a dictionary of options.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
671
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
672 """
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
673 plist = map(string.strip, string.splitfields(line, ';'))
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
674 key = string.lower(plist[0])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
675 del plist[0]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
676 pdict = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
677 for p in plist:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
678 i = string.find(p, '=')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
679 if i >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
680 name = string.lower(string.strip(p[:i]))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
681 value = string.strip(p[i+1:])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
682 if len(value) >= 2 and value[0] == value[-1] == '"':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
683 value = value[1:-1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
684 pdict[name] = value
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
685 return key, pdict
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
686
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
687
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
688 # Classes for field storage
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
689 # =========================
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
690
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
691 class MiniFieldStorage:
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
692
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
693 """Like FieldStorage, for use when no file uploads are possible."""
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
694
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
695 # Dummy attributes
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
696 filename = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
697 list = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
698 type = None
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
699 file = None
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
700 type_options = {}
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
701 disposition = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
702 disposition_options = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
703 headers = {}
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
704
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
705 def __init__(self, name, value):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
706 """Constructor from field name and value."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
707 self.name = name
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
708 self.value = value
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
709 # self.file = StringIO(value)
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
710
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
711 def __repr__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
712 """Return printable representation."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
713 return "MiniFieldStorage(%s, %s)" % (`self.name`, `self.value`)
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
714
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
715
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
716 class FieldStorage:
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
717
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
718 """Store a sequence of fields, reading multipart/form-data.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
719
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
720 This class provides naming, typing, files stored on disk, and
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
721 more. At the top level, it is accessible like a dictionary, whose
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
722 keys are the field names. (Note: None can occur as a field name.)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
723 The items are either a Python list (if there's multiple values) or
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
724 another FieldStorage or MiniFieldStorage object. If it's a single
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
725 object, it has the following attributes:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
726
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
727 name: the field name, if specified; otherwise None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
728
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
729 filename: the filename, if specified; otherwise None; this is the
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
730 client side filename, *not* the file name on which it is
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
731 stored (that's a temporary file you don't deal with)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
732
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
733 value: the value as a *string*; for file uploads, this
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
734 transparently reads the file every time you request the value
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
735
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
736 file: the file(-like) object from which you can read the data;
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
737 None if the data is stored a simple string
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
738
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
739 type: the content-type, or None if not specified
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
740
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
741 type_options: dictionary of options specified on the content-type
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
742 line
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
743
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
744 disposition: content-disposition, or None if not specified
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
745
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
746 disposition_options: dictionary of corresponding options
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
747
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
748 headers: a dictionary(-like) object (sometimes rfc822.Message or a
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
749 subclass thereof) containing *all* headers
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
750
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
751 The class is subclassable, mostly for the purpose of overriding
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
752 the make_file() method, which is called internally to come up with
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
753 a file open for reading and writing. This makes it possible to
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
754 override the default choice of storing all files in a temporary
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
755 directory and unlinking them as soon as they have been opened.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
756
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
757 """
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
758
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
759 def __init__(self, fp=None, headers=None, outerboundary="",
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
760 environ=os.environ, keep_blank_values=0, strict_parsing=0):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
761 """Constructor. Read multipart/* until last part.
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
762
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
763 Arguments, all optional:
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
764
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
765 fp : file pointer; default: sys.stdin
8284
f834e197bd14 Make Tim O'Malley's requested change: in FieldStorage.__init__(), when
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
766 (not used when the request method is GET)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
767
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
768 headers : header dictionary-like object; default:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
769 taken from environ as per CGI spec
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
770
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
771 outerboundary : terminating multipart boundary
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
772 (for internal use only)
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
773
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
774 environ : environment dictionary; default: os.environ
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
775
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
776 keep_blank_values: flag indicating whether blank values in
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
777 URL encoded forms should be treated as blank strings.
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
778 A true value inicates that blanks should be retained as
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
779 blank strings. The default false value indicates that
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
780 blank values are to be ignored and treated as if they were
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
781 not included.
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
782
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
783 strict_parsing: flag indicating what to do with parsing errors.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
784 If false (the default), errors are silently ignored.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
785 If true, errors raise a ValueError exception.
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
786
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
787 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
788 method = 'GET'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
789 self.keep_blank_values = keep_blank_values
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
790 self.strict_parsing = strict_parsing
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
791 if environ.has_key('REQUEST_METHOD'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
792 method = string.upper(environ['REQUEST_METHOD'])
8548
fac33716a218 Treat "HEAD" same as "GET", so that CGI scripts won't fail.
Guido van Rossum <guido@python.org>
parents: 8485
diff changeset
793 if method == 'GET' or method == 'HEAD':
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
794 if environ.has_key('QUERY_STRING'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
795 qs = environ['QUERY_STRING']
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
796 elif sys.argv[1:]:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
797 qs = sys.argv[1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
798 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
799 qs = ""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
800 fp = StringIO(qs)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
801 if headers is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
802 headers = {'content-type':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
803 "application/x-www-form-urlencoded"}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
804 if headers is None:
8485
7f94b4958056 Be more careful than the previous patch. The default content-type
Guido van Rossum <guido@python.org>
parents: 8477
diff changeset
805 headers = {}
7f94b4958056 Be more careful than the previous patch. The default content-type
Guido van Rossum <guido@python.org>
parents: 8477
diff changeset
806 if method == 'POST':
7f94b4958056 Be more careful than the previous patch. The default content-type
Guido van Rossum <guido@python.org>
parents: 8477
diff changeset
807 # Set default content-type for POST to what's traditional
7f94b4958056 Be more careful than the previous patch. The default content-type
Guido van Rossum <guido@python.org>
parents: 8477
diff changeset
808 headers['content-type'] = "application/x-www-form-urlencoded"
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
809 if environ.has_key('CONTENT_TYPE'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
810 headers['content-type'] = environ['CONTENT_TYPE']
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
811 if environ.has_key('CONTENT_LENGTH'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
812 headers['content-length'] = environ['CONTENT_LENGTH']
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
813 self.fp = fp or sys.stdin
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
814 self.headers = headers
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
815 self.outerboundary = outerboundary
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
816
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
817 # Process content-disposition header
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
818 cdisp, pdict = "", {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
819 if self.headers.has_key('content-disposition'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
820 cdisp, pdict = parse_header(self.headers['content-disposition'])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
821 self.disposition = cdisp
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
822 self.disposition_options = pdict
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
823 self.name = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
824 if pdict.has_key('name'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
825 self.name = pdict['name']
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
826 self.filename = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
827 if pdict.has_key('filename'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
828 self.filename = pdict['filename']
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
829
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
830 # Process content-type header
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
831 ctype, pdict = "text/plain", {}
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
832 if self.headers.has_key('content-type'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
833 ctype, pdict = parse_header(self.headers['content-type'])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
834 self.type = ctype
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
835 self.type_options = pdict
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
836 self.innerboundary = ""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
837 if pdict.has_key('boundary'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
838 self.innerboundary = pdict['boundary']
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
839 clen = -1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
840 if self.headers.has_key('content-length'):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
841 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
842 clen = string.atoi(self.headers['content-length'])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
843 except:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
844 pass
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
845 if maxlen and clen > maxlen:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
846 raise ValueError, 'Maximum content length exceeded'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
847 self.length = clen
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
848
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
849 self.list = self.file = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
850 self.done = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
851 self.lines = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
852 if ctype == 'application/x-www-form-urlencoded':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
853 self.read_urlencoded()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
854 elif ctype[:10] == 'multipart/':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
855 self.read_multi()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
856 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
857 self.read_single()
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
858
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
859 def __repr__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
860 """Return a printable representation."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
861 return "FieldStorage(%s, %s, %s)" % (
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
862 `self.name`, `self.filename`, `self.value`)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
863
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
864 def __getattr__(self, name):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
865 if name != 'value':
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
866 raise AttributeError, name
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
867 if self.file:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
868 self.file.seek(0)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
869 value = self.file.read()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
870 self.file.seek(0)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
871 elif self.list is not None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
872 value = self.list
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
873 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
874 value = None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
875 return value
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
876
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
877 def __getitem__(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
878 """Dictionary style indexing."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
879 if self.list is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
880 raise TypeError, "not indexable"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
881 found = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
882 for item in self.list:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
883 if item.name == key: found.append(item)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
884 if not found:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
885 raise KeyError, key
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
886 if len(found) == 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
887 return found[0]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
888 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
889 return found
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
890
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
891 def keys(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
892 """Dictionary style keys() method."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
893 if self.list is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
894 raise TypeError, "not indexable"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
895 keys = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
896 for item in self.list:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
897 if item.name not in keys: keys.append(item.name)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
898 return keys
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
899
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
900 def has_key(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
901 """Dictionary style has_key() method."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
902 if self.list is None:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
903 raise TypeError, "not indexable"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
904 for item in self.list:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
905 if item.name == key: return 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
906 return 0
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
907
4931
18023e7c3862 Add __len__ method
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
908 def __len__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
909 """Dictionary style len(x) support."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
910 return len(self.keys())
4931
18023e7c3862 Add __len__ method
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
911
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
912 def read_urlencoded(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
913 """Internal: read data in query string format."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
914 qs = self.fp.read(self.length)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
915 dict = parse_qs(qs, self.keep_blank_values, self.strict_parsing)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
916 self.list = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
917 for key, valuelist in dict.items():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
918 for value in valuelist:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
919 self.list.append(MiniFieldStorage(key, value))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
920 self.skip_lines()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
921
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
922 def read_multi(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
923 """Internal: read a part that is itself multipart."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
924 self.list = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
925 part = self.__class__(self.fp, {}, self.innerboundary)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
926 # Throw first part away
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
927 while not part.done:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
928 headers = rfc822.Message(self.fp)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
929 part = self.__class__(self.fp, headers, self.innerboundary)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
930 self.list.append(part)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
931 self.skip_lines()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
932
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
933 def read_single(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
934 """Internal: read an atomic part."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
935 if self.length >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
936 self.read_binary()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
937 self.skip_lines()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
938 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
939 self.read_lines()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
940 self.file.seek(0)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
941
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
942 bufsize = 8*1024 # I/O buffering size for copy to file
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
943
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
944 def read_binary(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
945 """Internal: read binary data."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
946 self.file = self.make_file('b')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
947 todo = self.length
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
948 if todo >= 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
949 while todo > 0:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
950 data = self.fp.read(min(todo, self.bufsize))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
951 if not data:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
952 self.done = -1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
953 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
954 self.file.write(data)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
955 todo = todo - len(data)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
956
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
957 def read_lines(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
958 """Internal: read lines until EOF or outerboundary."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
959 self.file = self.make_file('')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
960 if self.outerboundary:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
961 self.read_lines_to_outerboundary()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
962 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
963 self.read_lines_to_eof()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
964
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
965 def read_lines_to_eof(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
966 """Internal: read lines until EOF."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
967 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
968 line = self.fp.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
969 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
970 self.done = -1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
971 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
972 self.lines.append(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
973 self.file.write(line)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
974
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
975 def read_lines_to_outerboundary(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
976 """Internal: read lines until outerboundary."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
977 next = "--" + self.outerboundary
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
978 last = next + "--"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
979 delim = ""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
980 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
981 line = self.fp.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
982 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
983 self.done = -1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
984 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
985 self.lines.append(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
986 if line[:2] == "--":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
987 strippedline = string.strip(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
988 if strippedline == next:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
989 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
990 if strippedline == last:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
991 self.done = 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
992 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
993 odelim = delim
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
994 if line[-2:] == "\r\n":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
995 delim = "\r\n"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
996 line = line[:-2]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
997 elif line[-1] == "\n":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
998 delim = "\n"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
999 line = line[:-1]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1000 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1001 delim = ""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1002 self.file.write(odelim + line)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1003
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1004 def skip_lines(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1005 """Internal: skip lines until outer boundary if defined."""
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1006 if not self.outerboundary or self.done:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1007 return
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1008 next = "--" + self.outerboundary
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1009 last = next + "--"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1010 while 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1011 line = self.fp.readline()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1012 if not line:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1013 self.done = -1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1014 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1015 self.lines.append(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1016 if line[:2] == "--":
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1017 strippedline = string.strip(line)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1018 if strippedline == next:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1019 break
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1020 if strippedline == last:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1021 self.done = 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1022 break
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1023
5988
5993a099d9b0 Changes suggested by Jim fulton:
Guido van Rossum <guido@python.org>
parents: 5870
diff changeset
1024 def make_file(self, binary=None):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1025 """Overridable: return a readable & writable file.
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1026
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1027 The file will be used as follows:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1028 - data is written to it
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1029 - seek(0)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1030 - data is read from it
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1031
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1032 The 'binary' argument is unused -- the file is always opened
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1033 in binary mode.
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1034
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1035 This version opens a temporary file for reading and writing,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1036 and immediately deletes (unlinks) it. The trick (on Unix!) is
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1037 that the file can still be used, but it can't be opened by
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1038 another process, and it will automatically be deleted when it
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1039 is closed or when the current process terminates.
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1040
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1041 If you want a more permanent file, you derive a class which
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1042 overrides this method. If you want a visible temporary file
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1043 that is nevertheless automatically deleted when the script
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1044 terminates, try defining a __del__ method in a derived class
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1045 which unlinks the temporary files you have created.
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1046
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1047 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1048 import tempfile
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1049 return tempfile.TemporaryFile("w+b")
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1050
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
1051
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
1052
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1053 # Backwards Compatibility Classes
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1054 # ===============================
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056 class FormContentDict:
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1057 """Basic (multiple values per field) form content as dictionary.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1058
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1059 form = FormContentDict()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1060
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1061 form[key] -> [value, value, ...]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1062 form.has_key(key) -> Boolean
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1063 form.keys() -> [key, key, ...]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1064 form.values() -> [[val, val, ...], [val, val, ...], ...]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1065 form.items() -> [(key, [val, val, ...]), (key, [val, val, ...]), ...]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1066 form.dict == {key: [val, val, ...], ...}
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1067
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1068 """
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
1069 def __init__(self, environ=os.environ):
3958
b86c80448d35 For method=POST, append a query string from the environment or from
Guido van Rossum <guido@python.org>
parents: 3797
diff changeset
1070 self.dict = parse(environ=environ)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1071 self.query_string = environ['QUERY_STRING']
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1072 def __getitem__(self,key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1073 return self.dict[key]
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1074 def keys(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1075 return self.dict.keys()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1076 def has_key(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1077 return self.dict.has_key(key)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1078 def values(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1079 return self.dict.values()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1080 def items(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1081 return self.dict.items()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1082 def __len__( self ):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1083 return len(self.dict)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1085
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1086 class SvFormContentDict(FormContentDict):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1087 """Strict single-value expecting form content as dictionary.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1088
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1089 IF you only expect a single value for each field, then form[key]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1090 will return that single value. It will raise an IndexError if
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1091 that expectation is not true. IF you expect a field to have
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1092 possible multiple values, than you can use form.getlist(key) to
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1093 get all of the values. values() and items() are a compromise:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1094 they return single strings where there is a single value, and
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1095 lists of strings otherwise.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1096
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1097 """
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1098 def __getitem__(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1099 if len(self.dict[key]) > 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1100 raise IndexError, 'expecting a single value'
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1101 return self.dict[key][0]
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1102 def getlist(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1103 return self.dict[key]
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1104 def values(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1105 lis = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1106 for each in self.dict.values():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1107 if len( each ) == 1 :
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1108 lis.append(each[0])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1109 else: lis.append(each)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1110 return lis
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1111 def items(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1112 lis = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1113 for key,value in self.dict.items():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1114 if len(value) == 1 :
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1115 lis.append((key, value[0]))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1116 else: lis.append((key, value))
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1117 return lis
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1119
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1120 class InterpFormContentDict(SvFormContentDict):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1121 """This class is present for backwards compatibility only."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1122 def __getitem__( self, key ):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1123 v = SvFormContentDict.__getitem__( self, key )
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1124 if v[0] in string.digits+'+-.' :
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1125 try: return string.atoi( v )
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1126 except ValueError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1127 try: return string.atof( v )
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1128 except ValueError: pass
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1129 return string.strip(v)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1130 def values( self ):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1131 lis = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1132 for key in self.keys():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1133 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1134 lis.append( self[key] )
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1135 except IndexError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1136 lis.append( self.dict[key] )
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1137 return lis
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1138 def items( self ):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1139 lis = []
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1140 for key in self.keys():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1141 try:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1142 lis.append( (key, self[key]) )
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1143 except IndexError:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1144 lis.append( (key, self.dict[key]) )
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1145 return lis
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1146
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1147
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148 class FormContent(FormContentDict):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1149 """This class is present for backwards compatibility only."""
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1150 def values(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1151 if self.dict.has_key(key) :return self.dict[key]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1152 else: return None
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1153 def indexed_value(self, key, location):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1154 if self.dict.has_key(key):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1155 if len (self.dict[key]) > location:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1156 return self.dict[key][location]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1157 else: return None
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1158 else: return None
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1159 def value(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1160 if self.dict.has_key(key): return self.dict[key][0]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1161 else: return None
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1162 def length(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1163 return len(self.dict[key])
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1164 def stripped(self, key):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1165 if self.dict.has_key(key): return string.strip(self.dict[key][0])
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1166 else: return None
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1167 def pars(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1168 return self.dict
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1171 # Test/debug code
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1172 # ===============
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1173
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
1174 def test(environ=os.environ):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1175 """Robust test CGI script, usable as main program.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1176
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1177 Write minimal HTTP headers and dump all information provided to
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1178 the script in HTML form.
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1180 """
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1181 import traceback
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1182 print "Content-type: text/html"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1183 print
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1184 sys.stderr = sys.stdout
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1185 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1186 form = FieldStorage() # Replace with other classes to test those
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1187 print_form(form)
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
1188 print_environ(environ)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1189 print_directory()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1190 print_arguments()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1191 print_environ_usage()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1192 def f():
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1193 exec "testing print_exception() -- <I>italics?</I>"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1194 def g(f=f):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1195 f()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1196 print "<H3>What follows is a test, not an actual exception:</H3>"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1197 g()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1198 except:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1199 print_exception()
4072
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1200
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1201 # Second try with a small maxlen...
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1202 global maxlen
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1203 maxlen = 50
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1204 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1205 form = FieldStorage() # Replace with other classes to test those
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1206 print_form(form)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1207 print_environ(environ)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1208 print_directory()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1209 print_arguments()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1210 print_environ_usage()
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1211 except:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1212 print_exception()
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1213
4072
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1214 def print_exception(type=None, value=None, tb=None, limit=None):
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1215 if type is None:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1216 type, value, tb = sys.exc_info()
4072
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1217 import traceback
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1218 print
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1219 print "<H3>Traceback (innermost last):</H3>"
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1220 list = traceback.format_tb(tb, limit) + \
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1221 traceback.format_exception_only(type, value)
4072
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1222 print "<PRE>%s<B>%s</B></PRE>" % (
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1223 escape(string.join(list[:-1], "")),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1224 escape(list[-1]),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1225 )
6347
2656d1eea792 Use sys.exc_info() where needed.
Guido van Rossum <guido@python.org>
parents: 5988
diff changeset
1226 del tb
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1227
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
1228 def print_environ(environ=os.environ):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1229 """Dump the shell environment as HTML."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1230 keys = environ.keys()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1231 keys.sort()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1232 print
3629
e54d39107249 capitalize H3 headers; add 'cgi.' prefix to example code
Guido van Rossum <guido@python.org>
parents: 3451
diff changeset
1233 print "<H3>Shell Environment:</H3>"
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1234 print "<DL>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1235 for key in keys:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1236 print "<DT>", escape(key), "<DD>", escape(environ[key])
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1237 print "</DL>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1238 print
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1239
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1240 def print_form(form):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1241 """Dump the contents of a form as HTML."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1242 keys = form.keys()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1243 keys.sort()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1244 print
3629
e54d39107249 capitalize H3 headers; add 'cgi.' prefix to example code
Guido van Rossum <guido@python.org>
parents: 3451
diff changeset
1245 print "<H3>Form Contents:</H3>"
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1246 print "<DL>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1247 for key in keys:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1248 print "<DT>" + escape(key) + ":",
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1249 value = form[key]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1250 print "<i>" + escape(`type(value)`) + "</i>"
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1251 print "<DD>" + escape(`value`)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1252 print "</DL>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1253 print
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1254
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1255 def print_directory():
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1256 """Dump the current directory as HTML."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1257 print
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1258 print "<H3>Current Working Directory:</H3>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1259 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1260 pwd = os.getcwd()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1261 except os.error, msg:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1262 print "os.error:", escape(str(msg))
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1263 else:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1264 print escape(pwd)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1265 print
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1266
3451
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1267 def print_arguments():
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1268 print
3629
e54d39107249 capitalize H3 headers; add 'cgi.' prefix to example code
Guido van Rossum <guido@python.org>
parents: 3451
diff changeset
1269 print "<H3>Command Line Arguments:</H3>"
3451
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1270 print
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1271 print sys.argv
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1272 print
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1273
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 def print_environ_usage():
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1275 """Dump a list of environment variables used by CGI as HTML."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1276 print """
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1277 <H3>These environment variables could have been set:</H3>
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1278 <UL>
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1279 <LI>AUTH_TYPE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1280 <LI>CONTENT_LENGTH
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1281 <LI>CONTENT_TYPE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 <LI>DATE_GMT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1283 <LI>DATE_LOCAL
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1284 <LI>DOCUMENT_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1285 <LI>DOCUMENT_ROOT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1286 <LI>DOCUMENT_URI
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1287 <LI>GATEWAY_INTERFACE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1288 <LI>LAST_MODIFIED
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1289 <LI>PATH
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1290 <LI>PATH_INFO
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 <LI>PATH_TRANSLATED
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1292 <LI>QUERY_STRING
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1293 <LI>REMOTE_ADDR
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1294 <LI>REMOTE_HOST
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1295 <LI>REMOTE_IDENT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1296 <LI>REMOTE_USER
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1297 <LI>REQUEST_METHOD
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1298 <LI>SCRIPT_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 <LI>SERVER_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300 <LI>SERVER_PORT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1301 <LI>SERVER_PROTOCOL
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302 <LI>SERVER_ROOT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 <LI>SERVER_SOFTWARE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304 </UL>
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1305 In addition, HTTP headers sent by the server may be passed in the
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1306 environment as well. Here are some common variable names:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1307 <UL>
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1308 <LI>HTTP_ACCEPT
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1309 <LI>HTTP_CONNECTION
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1310 <LI>HTTP_HOST
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1311 <LI>HTTP_PRAGMA
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1312 <LI>HTTP_REFERER
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1313 <LI>HTTP_USER_AGENT
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1314 </UL>
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1315 """
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1316
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1317
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1318 # Utilities
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1319 # =========
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320
5870
fe4f1eb14d53 Add optional 'quote' flag argument to escape(); if true, translate '"'
Guido van Rossum <guido@python.org>
parents: 5654
diff changeset
1321 def escape(s, quote=None):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1322 """Replace special characters '&', '<' and '>' by SGML entities."""
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1323 s = string.replace(s, "&", "&amp;") # Must be done first!
6941
9104784af159 Use string.replace instead of regsub.[g]sub.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
1324 s = string.replace(s, "<", "&lt;")
9104784af159 Use string.replace instead of regsub.[g]sub.
Guido van Rossum <guido@python.org>
parents: 6347
diff changeset
1325 s = string.replace(s, ">", "&gt;",)
5870
fe4f1eb14d53 Add optional 'quote' flag argument to escape(); if true, translate '"'
Guido van Rossum <guido@python.org>
parents: 5654
diff changeset
1326 if quote:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6941
diff changeset
1327 s = string.replace(s, '"', "&quot;")
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1328 return s
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1329
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1330
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1331 # Invoke mainline
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1332 # ===============
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1333
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1334 # Call test() when this file is run as a script (not imported as a module)
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1335 if __name__ == '__main__':
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1336 test()