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