annotate Lib/cgi.py @ 5792:dffabc620e4e legacy-trunk

Tweak re_tests and test_re to differentiate between groups that have no value and groups that are out of bounds.
author Guido van Rossum <guido@python.org>
date Tue, 15 Jul 1997 15:49:52 +0000
parents 8e14d54a6419
children fe4f1eb14d53
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
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
35 print "Content-type: text/html" # HTML is following
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
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
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
42 print "<TITLE>CGI script output</TITLE>"
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
43 print "<H1>This is my first CGI script</H1>"
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
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
3629
e54d39107249 capitalize H3 headers; add 'cgi.' prefix to example code
Guido van Rossum <guido@python.org>
parents: 3451
diff changeset
69 form = cgi.FieldStorage()
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
70 form_ok = 0
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
71 if form.has_key("name") and form.has_key("addr"):
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
72 if form["name"].value != "" and form["addr"].value != "":
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
73 form_ok = 1
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
74 if not form_ok:
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
75 print "<H1>Error</H1>"
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
76 print "Please fill in the name and addr fields."
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
77 return
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
78 ...further form processing here...
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
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
91 username = form["username"]
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
92 if type(username) is type([]):
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
93 # Multiple username fields specified
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
94 usernames = ""
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
95 for item in username:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
96 if usernames:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
97 # Next item -- insert comma
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
98 usernames = usernames + "," + item.value
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
99 else:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
100 # First item -- don't insert comma
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
101 usernames = item.value
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
102 else:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
103 # Single username field specified
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
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
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
112 fileitem = form["userfile"]
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
113 if fileitem.file:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
114 # It's an uploaded file; count lines
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
115 linecount = 0
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
116 while 1:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
117 line = fileitem.file.readline()
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
118 if not line: break
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
119 linecount = linecount + 1
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
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 4534
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
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
243 import sys
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
244 sys.path.insert(0, "/usr/home/joe/lib/python")
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
245 sys.path.insert(0, "/usr/local/lib/python")
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
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
281 http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
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
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
296 cgi.test()
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
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
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
313 1. Import the traceback module (before entering the
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
314 try-except!)
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
315
3435
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
316 2. Make sure you finish printing the headers and the blank
f72050dd61a9 Reformatting only.
Guido van Rossum <guido@python.org>
parents: 3432
diff changeset
317 line early
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
318
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
319 3. Assign sys.stderr to sys.stdout
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
320
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
321 3. Wrap all remaining code in a try-except statement
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
322
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
323 4. In the except clause, call traceback.print_exc()
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
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
327 import sys
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
328 import traceback
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
329 print "Content-type: text/html"
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
330 print
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
331 sys.stderr = sys.stdout
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
332 try:
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
333 ...your code here...
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
334 except:
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
335 print "\n\n<PRE>"
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
336 traceback.print_exc()
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
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
346 import sys
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
347 sys.stderr = sys.stdout
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
348 print "Content-type: text/plain"
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
349 print
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
350 ...your code here...
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
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
422
4214
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
423
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
424 # Logging support
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
425 # ===============
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
426
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
427 logfile = "" # Filename to log to, if not empty
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
428 logfp = None # File object to log to, if not None
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 def initlog(*allargs):
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
431 """Write a log message, if there is a log file.
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
432
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
433 Even though this function is called initlog(), you should always
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
434 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
435 (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
436 nolog (when logging is disabled).
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
437
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
438 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
439 any) are arguments to the % operator, so e.g.
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
440 log("%s: %s", "a", "b")
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
441 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
442
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
443 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
444 which log data is written.
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
445
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
446 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
447 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
448 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
449 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
450 send an error message).
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
451
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
452 """
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
453 global logfp, log
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
454 if logfile and not logfp:
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
455 try:
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
456 logfp = open(logfile, "a")
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
457 except IOError:
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
458 pass
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
459 if not logfp:
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
460 log = nolog
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
461 else:
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
462 log = dolog
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
463 apply(log, allargs)
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
464
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
465 def dolog(fmt, *args):
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
466 """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
467 logfp.write(fmt%args + "\n")
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 nolog(*allargs):
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
470 """Dummy function, assigned to log when logging is disabled."""
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
471 pass
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 log = initlog # The current logging function
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
474
688d566cb15b Added logging support.
Guido van Rossum <guido@python.org>
parents: 4213
diff changeset
475
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
476 # Parsing functions
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
477 # =================
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
478
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
479 # 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
480 # 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
481 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
482
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
483 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
484 """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
485
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
486 Arguments, all optional:
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
487
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
488 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
489
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
490 environ : environment dictionary; default: os.environ
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 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
493 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
494 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
495 blank strings. The default false value indicates that
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
496 blank values are to be ignored and treated as if they were
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
497 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
498
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
499 strict_parsing: flag indicating what to do with parsing errors.
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
500 If false (the default), errors are silently ignored.
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
501 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
502 """
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
503 if not fp:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
504 fp = sys.stdin
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
505 if not environ.has_key('REQUEST_METHOD'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
506 environ['REQUEST_METHOD'] = 'GET' # For testing stand-alone
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
507 if environ['REQUEST_METHOD'] == 'POST':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
508 ctype, pdict = parse_header(environ['CONTENT_TYPE'])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
509 if ctype == 'multipart/form-data':
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
510 return 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
511 elif ctype == 'application/x-www-form-urlencoded':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
512 clength = string.atoi(environ['CONTENT_LENGTH'])
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
513 if maxlen and clength > maxlen:
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
514 raise ValueError, 'Maximum content length exceeded'
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
515 qs = fp.read(clength)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 else:
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
517 qs = '' # Unknown content-type
3958
b86c80448d35 For method=POST, append a query string from the environment or from
Guido van Rossum <guido@python.org>
parents: 3797
diff changeset
518 if environ.has_key('QUERY_STRING'):
b86c80448d35 For method=POST, append a query string from the environment or from
Guido van Rossum <guido@python.org>
parents: 3797
diff changeset
519 if qs: qs = qs + '&'
b86c80448d35 For method=POST, append a query string from the environment or from
Guido van Rossum <guido@python.org>
parents: 3797
diff changeset
520 qs = qs + environ['QUERY_STRING']
b86c80448d35 For method=POST, append a query string from the environment or from
Guido van Rossum <guido@python.org>
parents: 3797
diff changeset
521 elif sys.argv[1:]:
b86c80448d35 For method=POST, append a query string from the environment or from
Guido van Rossum <guido@python.org>
parents: 3797
diff changeset
522 if qs: qs = qs + '&'
b86c80448d35 For method=POST, append a query string from the environment or from
Guido van Rossum <guido@python.org>
parents: 3797
diff changeset
523 qs = qs + sys.argv[1]
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
524 environ['QUERY_STRING'] = qs # XXX Shouldn't, really
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
525 elif environ.has_key('QUERY_STRING'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
526 qs = environ['QUERY_STRING']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
527 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
528 if sys.argv[1:]:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
529 qs = sys.argv[1]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
530 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
531 qs = ""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
532 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
533 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
534
cfa9b3a84872 added parse_qs(query_string)
Guido van Rossum <guido@python.org>
parents: 2538
diff changeset
535
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
536 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
537 """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
538
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
539 Arguments:
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
540
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
541 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
542
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
543 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
544 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
545 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
546 blank strings. The default false value indicates that
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
547 blank values are to be ignored and treated as if they were
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
548 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
549
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
550 strict_parsing: flag indicating what to do with parsing errors.
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
551 If false (the default), errors are silently ignored.
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
552 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
553 """
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
554 import urllib, regsub
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
555 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
556 dict = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
557 for name_value in name_value_pairs:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
558 nv = string.splitfields(name_value, '=')
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
559 if len(nv) != 2:
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
560 if strict_parsing:
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
561 raise ValueError, "bad query field: %s" % `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
562 continue
5302
5e3bcf4ebf29 From: Clarence Gardner <clarence@king.silcom.com>
Guido van Rossum <guido@python.org>
parents: 5128
diff changeset
563 name = urllib.unquote(regsub.gsub('+', ' ', nv[0]))
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
564 value = urllib.unquote(regsub.gsub('+', ' ', nv[1]))
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
565 if len(value) or keep_blank_values:
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
566 if dict.has_key (name):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
567 dict[name].append(value)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
568 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
569 dict[name] = [value]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
570 return dict
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
571
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
572
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
573 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
574 """Parse multipart input.
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
575
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
576 Arguments:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
577 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
578 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
579
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
580 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
581 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
582 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
583 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
584 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
585 header.
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
586
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
587 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
588 that.
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 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
591 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
592
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
593 """
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
594 import mimetools
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
595 if pdict.has_key('boundary'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
596 boundary = pdict['boundary']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
597 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
598 boundary = ""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
599 nextpart = "--" + boundary
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
600 lastpart = "--" + boundary + "--"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
601 partdict = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
602 terminator = ""
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
603
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
604 while terminator != lastpart:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
605 bytes = -1
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
606 data = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
607 if terminator:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
608 # At start of next part. Read headers first.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
609 headers = mimetools.Message(fp)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
610 clength = headers.getheader('content-length')
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
611 if clength:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
612 try:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
613 bytes = string.atoi(clength)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
614 except string.atoi_error:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
615 pass
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
616 if bytes > 0:
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
617 if maxlen and bytes > maxlen:
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
618 raise ValueError, 'Maximum content length exceeded'
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
619 data = fp.read(bytes)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
620 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
621 data = ""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
622 # Read lines until end of part.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
623 lines = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
624 while 1:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
625 line = fp.readline()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
626 if not line:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
627 terminator = lastpart # End outer loop
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
628 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
629 if line[:2] == "--":
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
630 terminator = string.strip(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
631 if terminator in (nextpart, lastpart):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
632 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
633 lines.append(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
634 # Done with part.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
635 if data is None:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
636 continue
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
637 if bytes < 0:
3797
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
638 if lines:
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
639 # Strip final line terminator
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
640 line = lines[-1]
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
641 if line[-2:] == "\r\n":
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
642 line = line[:-2]
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
643 elif line[-1:] == "\n":
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
644 line = line[:-1]
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
645 lines[-1] = line
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
646 data = string.joinfields(lines, "")
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
647 line = headers['content-disposition']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
648 if not line:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
649 continue
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
650 key, params = parse_header(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
651 if key != 'form-data':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
652 continue
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
653 if params.has_key('name'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
654 name = params['name']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
655 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
656 continue
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
657 if partdict.has_key(name):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
658 partdict[name].append(data)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
659 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
660 partdict[name] = [data]
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
661
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
662 return partdict
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
663
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
664
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
665 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
666 """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
667
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
668 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
669
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
670 """
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
671 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
672 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
673 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
674 pdict = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
675 for p in plist:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
676 i = string.find(p, '=')
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
677 if i >= 0:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
678 name = string.lower(string.strip(p[:i]))
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
679 value = string.strip(p[i+1:])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
680 if len(value) >= 2 and value[0] == value[-1] == '"':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
681 value = value[1:-1]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
682 pdict[name] = value
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
683 return key, pdict
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
684
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
685
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
686 # Classes for field storage
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
687 # =========================
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
688
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
689 class MiniFieldStorage:
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
690
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
691 """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
692
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
693 # Dummy attributes
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
694 filename = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
695 list = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
696 type = None
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
697 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
698 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
699 disposition = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
700 disposition_options = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
701 headers = {}
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
702
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
703 def __init__(self, name, value):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
704 """Constructor from field name and value."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
705 from StringIO import StringIO
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
706 self.name = name
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
707 self.value = value
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
708 # self.file = StringIO(value)
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
709
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
710 def __repr__(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
711 """Return printable representation."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
712 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
713
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 class FieldStorage:
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
716
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
717 """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
718
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
719 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
720 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
721 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
722 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
723 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
724 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
725
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
726 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
727
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
728 filename: the filename, if specified; otherwise None; this is the
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
729 client side filename, *not* the file name on which it is
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
730 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
731
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
732 value: the value as a *string*; for file uploads, this
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
733 transparently reads the file every time you request the value
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
734
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
735 file: the file(-like) object from which you can read the data;
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
736 None if the data is stored a simple string
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
737
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
738 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
739
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
740 type_options: dictionary of options specified on the content-type
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
741 line
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
742
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
743 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
744
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
745 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
746
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
747 headers: a dictionary(-like) object (sometimes rfc822.Message or a
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
748 subclass thereof) containing *all* headers
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
749
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
750 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
751 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
752 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
753 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
754 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
755
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
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
758 def __init__(self, fp=None, headers=None, outerboundary="",
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
759 environ=os.environ, keep_blank_values=0, strict_parsing=0):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
760 """Constructor. Read multipart/* until last part.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
761
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
762 Arguments, all optional:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
763
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
764 fp : file pointer; default: sys.stdin
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
765
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
766 headers : header dictionary-like object; default:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
767 taken from environ as per CGI spec
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
768
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
769 outerboundary : terminating multipart boundary
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
770 (for internal use only)
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
771
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
772 environ : environment dictionary; default: os.environ
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
773
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
774 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
775 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
776 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
777 blank strings. The default false value indicates that
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
778 blank values are to be ignored and treated as if they were
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
779 not included.
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
780
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
781 strict_parsing: flag indicating what to do with parsing errors.
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
782 If false (the default), errors are silently ignored.
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
783 If true, errors raise a ValueError exception.
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
784
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
785 """
5654
8e14d54a6419 Tweak default source of query a bit so it is possible to invoke
Guido van Rossum <guido@python.org>
parents: 5652
diff changeset
786 method = 'GET'
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
787 self.keep_blank_values = keep_blank_values
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
788 self.strict_parsing = strict_parsing
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
789 if environ.has_key('REQUEST_METHOD'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
790 method = string.upper(environ['REQUEST_METHOD'])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
791 if not fp and method == 'GET':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
792 if environ.has_key('QUERY_STRING'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
793 qs = environ['QUERY_STRING']
5654
8e14d54a6419 Tweak default source of query a bit so it is possible to invoke
Guido van Rossum <guido@python.org>
parents: 5652
diff changeset
794 elif sys.argv[1:]:
8e14d54a6419 Tweak default source of query a bit so it is possible to invoke
Guido van Rossum <guido@python.org>
parents: 5652
diff changeset
795 qs = sys.argv[1]
8e14d54a6419 Tweak default source of query a bit so it is possible to invoke
Guido van Rossum <guido@python.org>
parents: 5652
diff changeset
796 else:
8e14d54a6419 Tweak default source of query a bit so it is possible to invoke
Guido van Rossum <guido@python.org>
parents: 5652
diff changeset
797 qs = ""
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
798 from StringIO import StringIO
5654
8e14d54a6419 Tweak default source of query a bit so it is possible to invoke
Guido van Rossum <guido@python.org>
parents: 5652
diff changeset
799 fp = StringIO(qs)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
800 if headers is None:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
801 headers = {'content-type':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
802 "application/x-www-form-urlencoded"}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
803 if headers is None:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
804 headers = {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
805 if environ.has_key('CONTENT_TYPE'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
806 headers['content-type'] = environ['CONTENT_TYPE']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
807 if environ.has_key('CONTENT_LENGTH'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
808 headers['content-length'] = environ['CONTENT_LENGTH']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
809 self.fp = fp or sys.stdin
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
810 self.headers = headers
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
811 self.outerboundary = outerboundary
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
812
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
813 # Process content-disposition header
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
814 cdisp, pdict = "", {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
815 if self.headers.has_key('content-disposition'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
816 cdisp, pdict = parse_header(self.headers['content-disposition'])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
817 self.disposition = cdisp
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
818 self.disposition_options = pdict
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
819 self.name = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
820 if pdict.has_key('name'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
821 self.name = pdict['name']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
822 self.filename = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
823 if pdict.has_key('filename'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
824 self.filename = pdict['filename']
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
825
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
826 # Process content-type header
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
827 ctype, pdict = "text/plain", {}
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
828 if self.headers.has_key('content-type'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
829 ctype, pdict = parse_header(self.headers['content-type'])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
830 self.type = ctype
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
831 self.type_options = pdict
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
832 self.innerboundary = ""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
833 if pdict.has_key('boundary'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
834 self.innerboundary = pdict['boundary']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
835 clen = -1
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
836 if self.headers.has_key('content-length'):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
837 try:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
838 clen = string.atoi(self.headers['content-length'])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
839 except:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
840 pass
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
841 if maxlen and clen > maxlen:
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
842 raise ValueError, 'Maximum content length exceeded'
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
843 self.length = clen
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
844
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
845 self.list = self.file = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
846 self.done = 0
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
847 self.lines = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
848 if ctype == 'application/x-www-form-urlencoded':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
849 self.read_urlencoded()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
850 elif ctype[:10] == 'multipart/':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
851 self.read_multi()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
852 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
853 self.read_single()
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
854
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
855 def __repr__(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
856 """Return a printable representation."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
857 return "FieldStorage(%s, %s, %s)" % (
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
858 `self.name`, `self.filename`, `self.value`)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
859
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
860 def __getattr__(self, name):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
861 if name != 'value':
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
862 raise AttributeError, name
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
863 if self.file:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
864 self.file.seek(0)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
865 value = self.file.read()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
866 self.file.seek(0)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
867 elif self.list is not None:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
868 value = self.list
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
869 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
870 value = None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
871 return value
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
872
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
873 def __getitem__(self, key):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
874 """Dictionary style indexing."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
875 if self.list is None:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
876 raise TypeError, "not indexable"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
877 found = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
878 for item in self.list:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
879 if item.name == key: found.append(item)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
880 if not found:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
881 raise KeyError, key
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
882 if len(found) == 1:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
883 return found[0]
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
884 else:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
885 return found
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
886
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
887 def keys(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
888 """Dictionary style keys() method."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
889 if self.list is None:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
890 raise TypeError, "not indexable"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
891 keys = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
892 for item in self.list:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
893 if item.name not in keys: keys.append(item.name)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
894 return keys
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
895
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
896 def has_key(self, key):
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
897 """Dictionary style has_key() method."""
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
898 if self.list is None:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
899 raise TypeError, "not indexable"
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
900 for item in self.list:
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
901 if item.name == key: return 1
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
902 return 0
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
903
4931
18023e7c3862 Add __len__ method
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
904 def __len__(self):
18023e7c3862 Add __len__ method
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
905 """Dictionary style len(x) support."""
18023e7c3862 Add __len__ method
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
906 return len(self.keys())
18023e7c3862 Add __len__ method
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
907
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
908 def read_urlencoded(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
909 """Internal: read data in query string format."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
910 qs = self.fp.read(self.length)
4534
d90d7b541ada Added 'strict_parsing' option to all parsing functions. This causes a
Guido van Rossum <guido@python.org>
parents: 4486
diff changeset
911 dict = parse_qs(qs, self.keep_blank_values, self.strict_parsing)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
912 self.list = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
913 for key, valuelist in dict.items():
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
914 for value in valuelist:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
915 self.list.append(MiniFieldStorage(key, value))
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
916 self.skip_lines()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
917
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
918 def read_multi(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
919 """Internal: read a part that is itself multipart."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
920 import rfc822
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
921 self.list = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
922 part = self.__class__(self.fp, {}, self.innerboundary)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
923 # Throw first part away
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
924 while not part.done:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
925 headers = rfc822.Message(self.fp)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
926 part = self.__class__(self.fp, headers, self.innerboundary)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
927 self.list.append(part)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
928 self.skip_lines()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
929
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
930 def read_single(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
931 """Internal: read an atomic part."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
932 if self.length >= 0:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
933 self.read_binary()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
934 self.skip_lines()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
935 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
936 self.read_lines()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
937 self.file.seek(0)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
938
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
939 bufsize = 8*1024 # I/O buffering size for copy to file
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
940
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
941 def read_binary(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
942 """Internal: read binary data."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
943 self.file = self.make_file('b')
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
944 todo = self.length
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
945 if todo >= 0:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
946 while todo > 0:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
947 data = self.fp.read(min(todo, self.bufsize))
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
948 if not data:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
949 self.done = -1
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
950 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
951 self.file.write(data)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
952 todo = todo - len(data)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
953
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
954 def read_lines(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
955 """Internal: read lines until EOF or outerboundary."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
956 self.file = self.make_file('')
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
957 if self.outerboundary:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
958 self.read_lines_to_outerboundary()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
959 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
960 self.read_lines_to_eof()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
961
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
962 def read_lines_to_eof(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
963 """Internal: read lines until EOF."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
964 while 1:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
965 line = self.fp.readline()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
966 if not line:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
967 self.done = -1
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
968 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
969 self.lines.append(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
970 self.file.write(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
971
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
972 def read_lines_to_outerboundary(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
973 """Internal: read lines until outerboundary."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
974 next = "--" + self.outerboundary
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
975 last = next + "--"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
976 delim = ""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
977 while 1:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
978 line = self.fp.readline()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
979 if not line:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
980 self.done = -1
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
981 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
982 self.lines.append(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
983 if line[:2] == "--":
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
984 strippedline = string.strip(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
985 if strippedline == next:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
986 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
987 if strippedline == last:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
988 self.done = 1
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
989 break
4213
e31b4defad5b Fixed embarrassing logic bug in read_lines_to_outerboundary().
Guido van Rossum <guido@python.org>
parents: 4144
diff changeset
990 odelim = delim
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
991 if line[-2:] == "\r\n":
3797
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
992 delim = "\r\n"
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
993 line = line[:-2]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
994 elif line[-1] == "\n":
3797
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
995 delim = "\n"
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
996 line = line[:-1]
3797
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
997 else:
51a7c8fe7c7b Remove all CRLF -> LF translation for file uploads, since we cannot
Guido van Rossum <guido@python.org>
parents: 3794
diff changeset
998 delim = ""
4213
e31b4defad5b Fixed embarrassing logic bug in read_lines_to_outerboundary().
Guido van Rossum <guido@python.org>
parents: 4144
diff changeset
999 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
1000
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1001 def skip_lines(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1002 """Internal: skip lines until outer boundary if defined."""
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1003 if not self.outerboundary or self.done:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1004 return
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1005 next = "--" + self.outerboundary
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1006 last = next + "--"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1007 while 1:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1008 line = self.fp.readline()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1009 if not line:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1010 self.done = -1
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1011 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1012 self.lines.append(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1013 if line[:2] == "--":
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1014 strippedline = string.strip(line)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1015 if strippedline == next:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1016 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1017 if strippedline == last:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1018 self.done = 1
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1019 break
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1020
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1021 def make_file(self, binary):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1022 """Overridable: return a readable & writable file.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1023
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1024 The file will be used as follows:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1025 - data is written to it
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1026 - seek(0)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1027 - data is read from it
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1028
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1029 The 'binary' argument is 'b' if the file should be created in
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1030 binary mode (on non-Unix systems), '' otherwise.
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1031
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1032 This version opens a temporary file for reading and writing,
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1033 and immediately deletes (unlinks) it. The trick (on Unix!) is
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1034 that the file can still be used, but it can't be opened by
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1035 another process, and it will automatically be deleted when it
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1036 is closed or when the current process terminates.
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1037
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1038 If you want a more permanent file, you derive a class which
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1039 overrides this method. If you want a visible temporary file
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1040 that is nevertheless automatically deleted when the script
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1041 terminates, try defining a __del__ method in a derived class
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1042 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
1043
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1044 """
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1045 import tempfile
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1046 tfn = tempfile.mktemp()
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1047 f = open(tfn, "w%s+" % binary)
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1048 os.unlink(tfn)
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1049 return f
3437
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
1050
d70149202c93 Added FieldStorage class, which stores parts in files.
Guido van Rossum <guido@python.org>
parents: 3435
diff changeset
1051
3447
db76fd390f74 Change to use real temporary files instead of StringIO. Correct and
Guido van Rossum <guido@python.org>
parents: 3446
diff changeset
1052 # 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
1053 # ===============================
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055 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
1056 """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
1057
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1058 form = FormContentDict()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1059
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1060 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
1061 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
1062 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
1063 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
1064 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
1065 form.dict == {key: [val, val, ...], ...}
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1066
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1067 """
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
1068 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
1069 self.dict = parse(environ=environ)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1070 self.query_string = environ['QUERY_STRING']
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1071 def __getitem__(self,key):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1072 return self.dict[key]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1073 def keys(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1074 return self.dict.keys()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1075 def has_key(self, key):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1076 return self.dict.has_key(key)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1077 def values(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1078 return self.dict.values()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1079 def items(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1080 return self.dict.items()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1081 def __len__( self ):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1082 return len(self.dict)
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1083
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 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
1086 """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
1087
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1088 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
1089 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
1090 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
1091 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
1092 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
1093 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
1094 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
1095
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 def __getitem__(self, key):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1098 if len(self.dict[key]) > 1:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1099 raise IndexError, 'expecting a single value'
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1100 return self.dict[key][0]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1101 def getlist(self, key):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1102 return self.dict[key]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1103 def values(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1104 lis = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1105 for each in self.dict.values():
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1106 if len( each ) == 1 :
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1107 lis.append(each[0])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1108 else: lis.append(each)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1109 return lis
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1110 def items(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1111 lis = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1112 for key,value in self.dict.items():
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1113 if len(value) == 1 :
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1114 lis.append((key, value[0]))
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1115 else: lis.append((key, value))
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1116 return lis
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1117
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 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
1120 """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
1121 def __getitem__( self, key ):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1122 v = SvFormContentDict.__getitem__( self, key )
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1123 if v[0] in string.digits+'+-.' :
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1124 try: return string.atoi( v )
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1125 except ValueError:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1126 try: return string.atof( v )
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1127 except ValueError: pass
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1128 return string.strip(v)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1129 def values( self ):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1130 lis = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1131 for key in self.keys():
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1132 try:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1133 lis.append( self[key] )
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1134 except IndexError:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1135 lis.append( self.dict[key] )
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1136 return lis
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1137 def items( self ):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1138 lis = []
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1139 for key in self.keys():
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1140 try:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1141 lis.append( (key, self[key]) )
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1142 except IndexError:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1143 lis.append( (key, self.dict[key]) )
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1144 return lis
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1145
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 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
1148 """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
1149 def values(self, key):
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1150 if self.dict.has_key(key) :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
1151 else: return None
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1152 def indexed_value(self, key, location):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1153 if self.dict.has_key(key):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1154 if len (self.dict[key]) > location:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1155 return self.dict[key][location]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1156 else: return None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1157 else: return None
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1158 def value(self, key):
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1159 if self.dict.has_key(key): 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
1160 else: return None
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1161 def length(self, key):
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1162 return len(self.dict[key])
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1163 def stripped(self, key):
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1164 if self.dict.has_key(key): return string.strip(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
1165 else: return None
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1166 def pars(self):
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1167 return self.dict
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1170 # Test/debug code
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1171 # ===============
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
1173 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
1174 """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
1175
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1176 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
1177 the script in HTML form.
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1179 """
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1180 import traceback
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1181 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
1182 print
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1183 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
1184 try:
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1185 form = FieldStorage() # Replace with other classes to test those
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1186 print_form(form)
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
1187 print_environ(environ)
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1188 print_directory()
3451
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1189 print_arguments()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1190 print_environ_usage()
4072
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1191 def f():
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1192 exec "testing print_exception() -- <I>italics?</I>"
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1193 def g(f=f):
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1194 f()
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1195 print "<H3>What follows is a test, not an actual exception:</H3>"
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1196 g()
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1197 except:
4072
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1198 print_exception()
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1199
5499
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1200 # 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
1201 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
1202 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
1203 try:
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1204 form = FieldStorage() # Replace with other classes to test those
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1205 print_form(form)
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1206 print_environ(environ)
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1207 print_directory()
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1208 print_arguments()
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1209 print_environ_usage()
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1210 except:
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1211 print_exception()
b905372edd4f Untested changes by Skip Montanaro to have an optional limit on the
Guido van Rossum <guido@python.org>
parents: 5302
diff changeset
1212
4072
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1213 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
1214 if type is None:
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1215 type, value, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1216 import traceback
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1217 print
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1218 print "<H3>Traceback (innermost last):</H3>"
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1219 list = traceback.format_tb(tb, limit) + \
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1220 traceback.format_exception_only(type, value)
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1221 print "<PRE>%s<B>%s</B></PRE>" % (
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1222 escape(string.join(list[:-1], "")),
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1223 escape(list[-1]),
f66562536f11 Added print_exception() convenience function.
Guido van Rossum <guido@python.org>
parents: 3958
diff changeset
1224 )
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1225
3794
d1c47f4f1541 Changes by Jim Fulton: pass environ around as arg;
Guido van Rossum <guido@python.org>
parents: 3630
diff changeset
1226 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
1227 """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
1228 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
1229 keys.sort()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1230 print
3629
e54d39107249 capitalize H3 headers; add 'cgi.' prefix to example code
Guido van Rossum <guido@python.org>
parents: 3451
diff changeset
1231 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
1232 print "<DL>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1233 for key in keys:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1234 print "<DT>", escape(key), "<DD>", escape(environ[key])
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1235 print "</DL>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1236 print
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1237
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1238 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
1239 """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
1240 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
1241 keys.sort()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1242 print
3629
e54d39107249 capitalize H3 headers; add 'cgi.' prefix to example code
Guido van Rossum <guido@python.org>
parents: 3451
diff changeset
1243 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
1244 print "<DL>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1245 for key in keys:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1246 print "<DT>" + escape(key) + ":",
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1247 value = form[key]
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1248 print "<i>" + escape(`type(value)`) + "</i>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1249 print "<DD>" + escape(`value`)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1250 print "</DL>"
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1251 print
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1252
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1253 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
1254 """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
1255 print
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1256 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
1257 try:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1258 pwd = os.getcwd()
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1259 except os.error, msg:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1260 print "os.error:", escape(str(msg))
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1261 else:
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1262 print escape(pwd)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1263 print
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1264
3451
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1265 def print_arguments():
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1266 print
3629
e54d39107249 capitalize H3 headers; add 'cgi.' prefix to example code
Guido van Rossum <guido@python.org>
parents: 3451
diff changeset
1267 print "<H3>Command Line Arguments:</H3>"
3451
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1268 print
5abbc9b5fdf0 Added print_arguments() function.
Guido van Rossum <guido@python.org>
parents: 3447
diff changeset
1269 print sys.argv
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
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1272 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
1273 """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
1274 print """
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1275 <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
1276 <UL>
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277 <LI>AUTH_TYPE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278 <LI>CONTENT_LENGTH
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1279 <LI>CONTENT_TYPE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1280 <LI>DATE_GMT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1281 <LI>DATE_LOCAL
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 <LI>DOCUMENT_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1283 <LI>DOCUMENT_ROOT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1284 <LI>DOCUMENT_URI
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1285 <LI>GATEWAY_INTERFACE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1286 <LI>LAST_MODIFIED
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1287 <LI>PATH
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1288 <LI>PATH_INFO
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1289 <LI>PATH_TRANSLATED
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1290 <LI>QUERY_STRING
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 <LI>REMOTE_ADDR
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1292 <LI>REMOTE_HOST
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1293 <LI>REMOTE_IDENT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1294 <LI>REMOTE_USER
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1295 <LI>REQUEST_METHOD
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1296 <LI>SCRIPT_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1297 <LI>SERVER_NAME
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1298 <LI>SERVER_PORT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 <LI>SERVER_PROTOCOL
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300 <LI>SERVER_ROOT
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1301 <LI>SERVER_SOFTWARE
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302 </UL>
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1303 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
1304 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
1305 <UL>
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1306 <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
1307 <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
1308 <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
1309 <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
1310 <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
1311 <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
1312 </UL>
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1313 """
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1314
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1315
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1316 # Utilities
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1317 # =========
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1318
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1319 def escape(s):
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1320 """Replace special characters '&', '<' and '>' by SGML entities."""
3446
623ba9bdeb22 Considerable documentation changes and a few import rearrangements.
Guido van Rossum <guido@python.org>
parents: 3444
diff changeset
1321 import regsub
3444
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1322 s = regsub.gsub("&", "&amp;", s) # Must be done first!
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1323 s = regsub.gsub("<", "&lt;", s)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1324 s = regsub.gsub(">", "&gt;", s)
e76ca34d48f3 Reformatted with 4-space indentation. Added some quick docs to the
Guido van Rossum <guido@python.org>
parents: 3437
diff changeset
1325 return s
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1327
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1328 # Invoke mainline
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1329 # ===============
2135
4f90b474beec new files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1330
3432
6abc75ec6d29 File upload and revamped documentation.
Guido van Rossum <guido@python.org>
parents: 3106
diff changeset
1331 # 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
1332 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
1333 test()