annotate Lib/cgi.py @ 13163:a09e8635534d legacy-trunk

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