Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 1 | #!/usr/local/bin/python |
Guido van Rossum | 1c9daa8 | 1995-09-18 21:52:37 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 3 | """Support module for CGI (Common Gateway Interface) scripts. |
Guido van Rossum | 1c9daa8 | 1995-09-18 21:52:37 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 5 | This module defines a number of utilities for use by CGI scripts written in |
| 6 | Python. |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 7 | |
| 8 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 9 | Introduction |
| 10 | ------------ |
| 11 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 12 | A CGI script is invoked by an HTTP server, usually to process user |
| 13 | input submitted through an HTML <FORM> or <ISINPUT> element. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 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). |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 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: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 34 | |
| 35 | print "Content-type: text/html" # HTML is following |
| 36 | print # blank line, end of headers |
| 37 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 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: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 41 | |
| 42 | print "<TITLE>CGI script output</TITLE>" |
| 43 | print "<H1>This is my first CGI script</H1>" |
| 44 | print "Hello, world!" |
| 45 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 46 | (It may not be fully legal HTML according to the letter of the |
| 47 | standard, but any browser will understand it.) |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | Using the cgi module |
| 51 | -------------------- |
| 52 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 53 | Begin by writing "import cgi". Don't use "from cgi import *" -- the |
| 54 | module defines all sorts of names for its own use that you don't want |
| 55 | in your namespace. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 57 | If you have a standard form, it's best to use the SvFormContentDict |
| 58 | class. Instantiate the SvFormContentDict class exactly once: it |
| 59 | consumes any input on standard input, which can't be wound back (it's |
| 60 | a network connection, not a disk file). |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 62 | The SvFormContentDict instance can be accessed as if it were a Python |
| 63 | dictionary. For instance, the following code checks that the fields |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 64 | "name" and "addr" are both set to a non-empty string: |
| 65 | |
| 66 | form = SvFormContentDict() |
| 67 | form_ok = 0 |
| 68 | if form.has_key("name") and form.has_key("addr"): |
| 69 | if form["name"] != "" and form["addr"] != "": |
| 70 | form_ok = 1 |
| 71 | if not form_ok: |
| 72 | print "<H1>Error</H1>" |
| 73 | print "Please fill in the name and addr fields." |
| 74 | return |
| 75 | ...actual form processing here... |
| 76 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 77 | If you have an input item of type "file" in your form and the client |
| 78 | supports file uploads, the value for that field, if present in the |
| 79 | form, is not a string but a tuple of (filename, content-type, data). |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | Overview of classes |
| 83 | ------------------- |
| 84 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 85 | SvFormContentDict: single value form content as dictionary; described |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 86 | above. |
| 87 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 88 | FormContentDict: multiple value form content as dictionary (the form |
| 89 | items are lists of values). Useful if your form contains multiple |
| 90 | fields with the same name. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 92 | Other classes (FormContent, InterpFormContentDict) are present for |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 93 | backwards compatibility only. |
| 94 | |
| 95 | |
| 96 | Overview of functions |
| 97 | --------------------- |
| 98 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 99 | These are useful if you want more control, or if you want to employ |
| 100 | some of the algorithms implemented in this module in other |
| 101 | circumstances. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 102 | |
| 103 | parse(): parse a form into a Python dictionary. |
| 104 | |
| 105 | parse_qs(qs): parse a query string. |
| 106 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 107 | parse_multipart(...): parse input of type multipart/form-data (for |
| 108 | file uploads). |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 110 | parse_header(string): parse a header like Content-type into a main |
| 111 | value and a dictionary of parameters. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 112 | |
| 113 | test(): complete test program. |
| 114 | |
| 115 | print_environ(): format the shell environment in HTML. |
| 116 | |
| 117 | print_form(form): format a form in HTML. |
| 118 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 119 | print_environ_usage(): print a list of useful environment variables in |
| 120 | HTML. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 122 | escape(): convert the characters "&", "<" and ">" to HTML-safe |
| 123 | sequences. Use this if you need to display text that might contain |
| 124 | such characters in HTML. To translate URLs for inclusion in the HREF |
| 125 | attribute of an <A> tag, use urllib.quote(). |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 126 | |
| 127 | |
| 128 | Caring about security |
| 129 | --------------------- |
| 130 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 131 | There's one important rule: if you invoke an external program (e.g. |
| 132 | via the os.system() or os.popen() functions), make very sure you don't |
| 133 | pass arbitrary strings received from the client to the shell. This is |
| 134 | a well-known security hole whereby clever hackers anywhere on the web |
| 135 | can exploit a gullible CGI script to invoke arbitrary shell commands. |
| 136 | Even parts of the URL or field names cannot be trusted, since the |
| 137 | request doesn't have to come from your form! |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 139 | To be on the safe side, if you must pass a string gotten from a form |
| 140 | to a shell command, you should make sure the string contains only |
| 141 | alphanumeric characters, dashes, underscores, and periods. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 142 | |
| 143 | |
| 144 | Installing your CGI script on a Unix system |
| 145 | ------------------------------------------- |
| 146 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 147 | Read the documentation for your HTTP server and check with your local |
| 148 | system administrator to find the directory where CGI scripts should be |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 149 | installed; usually this is in a directory cgi-bin in the server tree. |
| 150 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 151 | Make sure that your script is readable and executable by "others"; the |
| 152 | Unix file mode should be 755 (use "chmod 755 filename"). Make sure |
| 153 | that the first line of the script contains "#!" starting in column 1 |
| 154 | followed by the pathname of the Python interpreter, for instance: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 155 | |
| 156 | #!/usr/local/bin/python |
| 157 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 158 | Make sure the Python interpreter exists and is executable by "others". |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 160 | Make sure that any files your script needs to read or write are |
| 161 | readable or writable, respectively, by "others" -- their mode should |
| 162 | be 644 for readable and 666 for writable. This is because, for |
| 163 | security reasons, the HTTP server executes your script as user |
| 164 | "nobody", without any special privileges. It can only read (write, |
| 165 | execute) files that everybody can read (write, execute). The current |
| 166 | directory at execution time is also different (it is usually the |
| 167 | server's cgi-bin directory) and the set of environment variables is |
| 168 | also different from what you get at login. in particular, don't count |
| 169 | on the shell's search path for executables ($PATH) or the Python |
| 170 | module search path ($PYTHONPATH) to be set to anything interesting. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 172 | If you need to load modules from a directory which is not on Python's |
| 173 | default module search path, you can change the path in your script, |
| 174 | before importing other modules, e.g.: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 175 | |
| 176 | import sys |
| 177 | sys.path.insert(0, "/usr/home/joe/lib/python") |
| 178 | sys.path.insert(0, "/usr/local/lib/python") |
| 179 | |
| 180 | (This way, the directory inserted last will be searched first!) |
| 181 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 182 | Instructions for non-Unix systems will vary; check your HTTP server's |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 183 | documentation (it will usually have a section on CGI scripts). |
| 184 | |
| 185 | |
| 186 | Testing your CGI script |
| 187 | ----------------------- |
| 188 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 189 | Unfortunately, a CGI script will generally not run when you try it |
| 190 | from the command line, and a script that works perfectly from the |
| 191 | command line may fail mysteriously when run from the server. There's |
| 192 | one reason why you should still test your script from the command |
| 193 | line: if it contains a syntax error, the python interpreter won't |
| 194 | execute it at all, and the HTTP server will most likely send a cryptic |
| 195 | error to the client. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 197 | Assuming your script has no syntax errors, yet it does not work, you |
| 198 | have no choice but to read the next section: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 199 | |
| 200 | |
| 201 | Debugging CGI scripts |
| 202 | --------------------- |
| 203 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 204 | First of all, check for trivial installation errors -- reading the |
| 205 | section above on installing your CGI script carefully can save you a |
| 206 | lot of time. If you wonder whether you have understood the |
| 207 | installation procedure correctly, try installing a copy of this module |
| 208 | file (cgi.py) as a CGI script. When invoked as a script, the file |
| 209 | will dump its environment and the contents of the form in HTML form. |
| 210 | Give it the right mode etc, and send it a request. If it's installed |
| 211 | in the standard cgi-bin directory, it should be possible to send it a |
| 212 | request by entering a URL into your browser of the form: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 213 | |
| 214 | http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home |
| 215 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 216 | If this gives an error of type 404, the server cannot find the script |
| 217 | -- perhaps you need to install it in a different directory. If it |
| 218 | gives another error (e.g. 500), there's an installation problem that |
| 219 | you should fix before trying to go any further. If you get a nicely |
| 220 | formatted listing of the environment and form content (in this |
| 221 | example, the fields should be listed as "addr" with value "At Home" |
| 222 | and "name" with value "Joe Blow"), the cgi.py script has been |
| 223 | installed correctly. If you follow the same procedure for your own |
| 224 | script, you should now be able to debug it. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 226 | The next step could be to call the cgi module's test() function from |
| 227 | your script: replace its main code with the single statement |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 228 | |
| 229 | cgi.test() |
| 230 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 231 | This should produce the same results as those gotten from installing |
| 232 | the cgi.py file itself. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 234 | When an ordinary Python script raises an unhandled exception |
| 235 | (e.g. because of a typo in a module name, a file that can't be opened, |
| 236 | etc.), the Python interpreter prints a nice traceback and exits. |
| 237 | While the Python interpreter will still do this when your CGI script |
| 238 | raises an exception, most likely the traceback will end up in one of |
| 239 | the HTTP server's log file, or be discarded altogether. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 241 | Fortunately, once you have managed to get your script to execute |
| 242 | *some* code, it is easy to catch exceptions and cause a traceback to |
| 243 | be printed. The test() function below in this module is an example. |
| 244 | Here are the rules: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 246 | 1. Import the traceback module (before entering the |
| 247 | try-except!) |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 248 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 249 | 2. Make sure you finish printing the headers and the blank |
| 250 | line early |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 251 | |
| 252 | 3. Assign sys.stderr to sys.stdout |
| 253 | |
| 254 | 3. Wrap all remaining code in a try-except statement |
| 255 | |
| 256 | 4. In the except clause, call traceback.print_exc() |
| 257 | |
| 258 | For example: |
| 259 | |
| 260 | import sys |
| 261 | import traceback |
| 262 | print "Content-type: text/html" |
| 263 | print |
| 264 | sys.stderr = sys.stdout |
| 265 | try: |
| 266 | ...your code here... |
| 267 | except: |
| 268 | print "\n\n<PRE>" |
| 269 | traceback.print_exc() |
| 270 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 271 | Notes: The assignment to sys.stderr is needed because the traceback |
| 272 | prints to sys.stderr. The print "\n\n<PRE>" statement is necessary to |
| 273 | disable the word wrapping in HTML. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 275 | If you suspect that there may be a problem in importing the traceback |
| 276 | module, you can use an even more robust approach (which only uses |
| 277 | built-in modules): |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 278 | |
| 279 | import sys |
| 280 | sys.stderr = sys.stdout |
| 281 | print "Content-type: text/plain" |
| 282 | print |
| 283 | ...your code here... |
| 284 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 285 | This relies on the Python interpreter to print the traceback. The |
| 286 | content type of the output is set to plain text, which disables all |
| 287 | HTML processing. If your script works, the raw HTML will be displayed |
| 288 | by your client. If it raises an exception, most likely after the |
| 289 | first two lines have been printed, a traceback will be displayed. |
| 290 | Because no HTML interpretation is going on, the traceback will |
| 291 | readable. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 292 | |
| 293 | Good luck! |
| 294 | |
| 295 | |
| 296 | Common problems and solutions |
| 297 | ----------------------------- |
| 298 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 299 | - Most HTTP servers buffer the output from CGI scripts until the |
| 300 | script is completed. This means that it is not possible to display a |
| 301 | progress report on the client's display while the script is running. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 302 | |
| 303 | - Check the installation instructions above. |
| 304 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 305 | - Check the HTTP server's log files. ("tail -f logfile" in a separate |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 306 | window may be useful!) |
| 307 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 308 | - Always check a script for syntax errors first, by doing something |
| 309 | like "python script.py". |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 310 | |
| 311 | - When using any of the debugging techniques, don't forget to add |
| 312 | "import sys" to the top of the script. |
| 313 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 314 | - When invoking external programs, make sure they can be found. |
| 315 | Usually, this means using absolute path names -- $PATH is usually not |
| 316 | set to a very useful value in a CGI script. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 318 | - When reading or writing external files, make sure they can be read |
| 319 | or written by every user on the system. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 321 | - Don't try to give a CGI script a set-uid mode. This doesn't work on |
| 322 | most systems, and is a security liability as well. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 323 | |
| 324 | |
| 325 | History |
| 326 | ------- |
| 327 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 328 | Michael McLay started this module. Steve Majewski changed the |
| 329 | interface to SvFormContentDict and FormContentDict. The multipart |
| 330 | parsing was inspired by code submitted by Andreas Paepcke. Guido van |
| 331 | Rossum rewrote, reformatted and documented the module and is currently |
| 332 | responsible for its maintenance. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 333 | |
| 334 | """ |
| 335 | |
| 336 | |
| 337 | # Imports |
| 338 | # ======= |
| 339 | |
| 340 | import string |
| 341 | import regsub |
| 342 | import sys |
| 343 | import os |
| 344 | import urllib |
| 345 | |
| 346 | |
| 347 | # A shorthand for os.environ |
| 348 | environ = os.environ |
| 349 | |
| 350 | |
| 351 | # Parsing functions |
| 352 | # ================= |
| 353 | |
| 354 | def parse(fp=None): |
| 355 | """Parse a query in the environment or from a file (default stdin)""" |
| 356 | if not fp: |
| 357 | fp = sys.stdin |
| 358 | if not environ.has_key('REQUEST_METHOD'): |
| 359 | environ['REQUEST_METHOD'] = 'GET' # For testing |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 360 | if environ['REQUEST_METHOD'] == 'POST': |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 361 | ctype, pdict = parse_header(environ['CONTENT_TYPE']) |
| 362 | if ctype == 'multipart/form-data': |
| 363 | return parse_multipart(fp, ctype, pdict) |
| 364 | elif ctype == 'application/x-www-form-urlencoded': |
| 365 | clength = string.atoi(environ['CONTENT_LENGTH']) |
| 366 | qs = fp.read(clength) |
| 367 | else: |
| 368 | qs = '' # Bad content-type |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 369 | environ['QUERY_STRING'] = qs |
Guido van Rossum | 1c9daa8 | 1995-09-18 21:52:37 +0000 | [diff] [blame] | 370 | elif environ.has_key('QUERY_STRING'): |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 371 | qs = environ['QUERY_STRING'] |
Guido van Rossum | 1c9daa8 | 1995-09-18 21:52:37 +0000 | [diff] [blame] | 372 | else: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 373 | if sys.argv[1:]: |
| 374 | qs = sys.argv[1] |
| 375 | else: |
| 376 | qs = "" |
| 377 | environ['QUERY_STRING'] = qs |
Guido van Rossum | e780877 | 1995-08-07 20:12:09 +0000 | [diff] [blame] | 378 | return parse_qs(qs) |
| 379 | |
| 380 | |
| 381 | def parse_qs(qs): |
| 382 | """Parse a query given as a string argument""" |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 383 | name_value_pairs = string.splitfields(qs, '&') |
| 384 | dict = {} |
| 385 | for name_value in name_value_pairs: |
| 386 | nv = string.splitfields(name_value, '=') |
| 387 | if len(nv) != 2: |
| 388 | continue |
| 389 | name = nv[0] |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 390 | value = urllib.unquote(regsub.gsub('+', ' ', nv[1])) |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 391 | if len(value): |
| 392 | if dict.has_key (name): |
| 393 | dict[name].append(value) |
| 394 | else: |
| 395 | dict[name] = [value] |
| 396 | return dict |
| 397 | |
| 398 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 399 | def parse_multipart(fp, ctype, pdict): |
| 400 | """Parse multipart input. |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 402 | Arguments: |
| 403 | fp : input file |
| 404 | ctype: content-type |
| 405 | pdict: dictionary containing other parameters of conten-type header |
| 406 | |
| 407 | Returns a dictionary just like parse_qs() (keys are the field |
| 408 | names, each value is a list of values for that field) except |
| 409 | that if the value was an uploaded file, it is a tuple of the |
| 410 | form (filename, content-type, data). Note that content-type |
| 411 | is the raw, unparsed contents of the content-type header. |
| 412 | |
| 413 | XXX Should we parse further when the content-type is |
| 414 | multipart/*? |
| 415 | |
| 416 | """ |
| 417 | import mimetools |
| 418 | if pdict.has_key('boundary'): |
| 419 | boundary = pdict['boundary'] |
| 420 | else: |
| 421 | boundary = "" |
| 422 | nextpart = "--" + boundary |
| 423 | lastpart = "--" + boundary + "--" |
| 424 | partdict = {} |
| 425 | terminator = "" |
| 426 | |
| 427 | while terminator != lastpart: |
| 428 | bytes = -1 |
| 429 | data = None |
| 430 | if terminator: |
| 431 | # At start of next part. Read headers first. |
| 432 | headers = mimetools.Message(fp) |
| 433 | clength = headers.getheader('content-length') |
| 434 | if clength: |
| 435 | try: |
| 436 | bytes = string.atoi(clength) |
| 437 | except string.atoi_error: |
| 438 | pass |
| 439 | if bytes > 0: |
| 440 | data = fp.read(bytes) |
| 441 | else: |
| 442 | data = "" |
| 443 | # Read lines until end of part. |
| 444 | lines = [] |
| 445 | while 1: |
| 446 | line = fp.readline() |
| 447 | if not line: |
| 448 | terminator = lastpart # End outer loop |
| 449 | break |
| 450 | if line[:2] == "--": |
| 451 | terminator = string.strip(line) |
| 452 | if terminator in (nextpart, lastpart): |
| 453 | break |
| 454 | if line[-2:] == '\r\n': |
| 455 | line = line[:-2] |
| 456 | elif line[-1:] == '\n': |
| 457 | line = line[:-1] |
| 458 | lines.append(line) |
| 459 | # Done with part. |
| 460 | if data is None: |
| 461 | continue |
| 462 | if bytes < 0: |
| 463 | data = string.joinfields(lines, "\n") |
| 464 | line = headers['content-disposition'] |
| 465 | if not line: |
| 466 | continue |
| 467 | key, params = parse_header(line) |
| 468 | if key != 'form-data': |
| 469 | continue |
| 470 | if params.has_key('name'): |
| 471 | name = params['name'] |
| 472 | else: |
| 473 | continue |
| 474 | if params.has_key('filename'): |
| 475 | data = (params['filename'], |
| 476 | headers.getheader('content-type'), data) |
| 477 | if partdict.has_key(name): |
| 478 | partdict[name].append(data) |
| 479 | else: |
| 480 | partdict[name] = [data] |
| 481 | |
| 482 | return partdict |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 483 | |
| 484 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 485 | def parse_header(line): |
| 486 | """Parse a Content-type like header. |
| 487 | |
| 488 | Return the main content-type and a dictionary of options. |
| 489 | |
| 490 | """ |
| 491 | plist = map(string.strip, string.splitfields(line, ';')) |
| 492 | key = string.lower(plist[0]) |
| 493 | del plist[0] |
| 494 | pdict = {} |
| 495 | for p in plist: |
| 496 | i = string.find(p, '=') |
| 497 | if i >= 0: |
| 498 | name = string.lower(string.strip(p[:i])) |
| 499 | value = string.strip(p[i+1:]) |
| 500 | if len(value) >= 2 and value[0] == value[-1] == '"': |
| 501 | value = value[1:-1] |
| 502 | pdict[name] = value |
| 503 | return key, pdict |
| 504 | |
| 505 | |
| 506 | # Main classes |
| 507 | # ============ |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 508 | |
| 509 | class FormContentDict: |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 510 | """Basic (multiple values per field) form content as dictionary. |
| 511 | |
| 512 | form = FormContentDict() |
| 513 | |
| 514 | form[key] -> [value, value, ...] |
| 515 | form.has_key(key) -> Boolean |
| 516 | form.keys() -> [key, key, ...] |
| 517 | form.values() -> [[val, val, ...], [val, val, ...], ...] |
| 518 | form.items() -> [(key, [val, val, ...]), (key, [val, val, ...]), ...] |
| 519 | form.dict == {key: [val, val, ...], ...} |
| 520 | |
| 521 | """ |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 522 | def __init__( self ): |
| 523 | self.dict = parse() |
| 524 | self.query_string = environ['QUERY_STRING'] |
| 525 | def __getitem__(self,key): |
| 526 | return self.dict[key] |
| 527 | def keys(self): |
| 528 | return self.dict.keys() |
| 529 | def has_key(self, key): |
| 530 | return self.dict.has_key(key) |
| 531 | def values(self): |
| 532 | return self.dict.values() |
| 533 | def items(self): |
| 534 | return self.dict.items() |
| 535 | def __len__( self ): |
| 536 | return len(self.dict) |
| 537 | |
| 538 | |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 539 | class SvFormContentDict(FormContentDict): |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 540 | """Strict single-value expecting form content as dictionary. |
| 541 | |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 542 | IF you only expect a single value for each field, then |
| 543 | form[key] will return that single value. It will raise an |
| 544 | IndexError if that expectation is not true. IF you expect a |
| 545 | field to have possible multiple values, than you can use |
| 546 | form.getlist(key) to get all of the values. values() and |
| 547 | items() are a compromise: they return single strings where |
| 548 | there is a single value, and lists of strings otherwise. |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 549 | |
| 550 | """ |
| 551 | def __getitem__(self, key): |
| 552 | if len(self.dict[key]) > 1: |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 553 | raise IndexError, 'expecting a single value' |
| 554 | return self.dict[key][0] |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 555 | def getlist(self, key): |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 556 | return self.dict[key] |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 557 | def values(self): |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 558 | lis = [] |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 559 | for each in self.dict.values(): |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 560 | if len( each ) == 1 : |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 561 | lis.append(each[0]) |
| 562 | else: lis.append(each) |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 563 | return lis |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 564 | def items(self): |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 565 | lis = [] |
| 566 | for key,value in self.dict.items(): |
| 567 | if len(value) == 1 : |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 568 | lis.append((key, value[0])) |
| 569 | else: lis.append((key, value)) |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 570 | return lis |
| 571 | |
| 572 | |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 573 | class InterpFormContentDict(SvFormContentDict): |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 574 | """This class is present for backwards compatibility only.""" |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 575 | def __getitem__( self, key ): |
| 576 | v = SvFormContentDict.__getitem__( self, key ) |
| 577 | if v[0] in string.digits+'+-.' : |
| 578 | try: return string.atoi( v ) |
| 579 | except ValueError: |
| 580 | try: return string.atof( v ) |
| 581 | except ValueError: pass |
| 582 | return string.strip(v) |
| 583 | def values( self ): |
| 584 | lis = [] |
| 585 | for key in self.keys(): |
| 586 | try: |
| 587 | lis.append( self[key] ) |
| 588 | except IndexError: |
| 589 | lis.append( self.dict[key] ) |
| 590 | return lis |
| 591 | def items( self ): |
| 592 | lis = [] |
| 593 | for key in self.keys(): |
| 594 | try: |
| 595 | lis.append( (key, self[key]) ) |
| 596 | except IndexError: |
| 597 | lis.append( (key, self.dict[key]) ) |
| 598 | return lis |
| 599 | |
| 600 | |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 601 | class FormContent(FormContentDict): |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 602 | """This class is present for backwards compatibility only.""" |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 603 | def values(self,key): |
| 604 | if self.dict.has_key(key):return self.dict[key] |
| 605 | else: return None |
| 606 | def indexed_value(self,key, location): |
| 607 | if self.dict.has_key(key): |
| 608 | if len (self.dict[key]) > location: |
| 609 | return self.dict[key][location] |
| 610 | else: return None |
| 611 | else: return None |
| 612 | def value(self,key): |
| 613 | if self.dict.has_key(key):return self.dict[key][0] |
| 614 | else: return None |
| 615 | def length(self,key): |
| 616 | return len (self.dict[key]) |
| 617 | def stripped(self,key): |
| 618 | if self.dict.has_key(key):return string.strip(self.dict[key][0]) |
| 619 | else: return None |
| 620 | def pars(self): |
| 621 | return self.dict |
| 622 | |
| 623 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 624 | # Test/debug code |
| 625 | # =============== |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 626 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 627 | def test(): |
| 628 | """Robust test CGI script. |
| 629 | |
| 630 | Dump all information provided to the script in HTML form. |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 632 | """ |
| 633 | import traceback |
| 634 | print "Content-type: text/html" |
| 635 | print |
| 636 | sys.stderr = sys.stdout |
| 637 | try: |
| 638 | print_environ() |
| 639 | print_form(FormContentDict()) |
| 640 | print |
Guido van Rossum | 391b4e6 | 1996-03-06 19:11:33 +0000 | [diff] [blame] | 641 | print "<H3>Current Working Directory:</H3>" |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 642 | try: |
| 643 | pwd = os.getcwd() |
| 644 | except os.error, msg: |
| 645 | print "os.error:", escape(str(msg)) |
| 646 | else: |
| 647 | print escape(pwd) |
| 648 | print |
| 649 | except: |
| 650 | print "\n\n<PRE>" # Turn of word wrap |
| 651 | traceback.print_exc() |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 652 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 653 | def print_environ(): |
| 654 | """Dump the shell environment in HTML form.""" |
| 655 | keys = environ.keys() |
| 656 | keys.sort() |
| 657 | print |
| 658 | print "<H3>Shell environment:</H3>" |
| 659 | print "<DL>" |
| 660 | for key in keys: |
| 661 | print "<DT>", escape(key), "<DD>", escape(environ[key]) |
| 662 | print "</DL>" |
| 663 | print |
| 664 | |
| 665 | def print_form(form): |
| 666 | """Dump the contents of a form in HTML form.""" |
| 667 | keys = form.keys() |
| 668 | keys.sort() |
| 669 | print |
| 670 | print "<H3>Form contents:</H3>" |
| 671 | print "<DL>" |
| 672 | for key in keys: |
| 673 | print "<DT>" + escape(key) + ":", |
| 674 | print "<i>" + escape(`type(form[key])`) + "</i>" |
| 675 | print "<DD>" + escape(`form[key]`) |
| 676 | print "</DL>" |
| 677 | print |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 678 | |
| 679 | def print_environ_usage(): |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 680 | """Print a list of environment variables used by the CGI protocol.""" |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 681 | print """ |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 682 | <H3>These environment variables could have been set:</H3> |
| 683 | <UL> |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 684 | <LI>AUTH_TYPE |
| 685 | <LI>CONTENT_LENGTH |
| 686 | <LI>CONTENT_TYPE |
| 687 | <LI>DATE_GMT |
| 688 | <LI>DATE_LOCAL |
| 689 | <LI>DOCUMENT_NAME |
| 690 | <LI>DOCUMENT_ROOT |
| 691 | <LI>DOCUMENT_URI |
| 692 | <LI>GATEWAY_INTERFACE |
| 693 | <LI>LAST_MODIFIED |
| 694 | <LI>PATH |
| 695 | <LI>PATH_INFO |
| 696 | <LI>PATH_TRANSLATED |
| 697 | <LI>QUERY_STRING |
| 698 | <LI>REMOTE_ADDR |
| 699 | <LI>REMOTE_HOST |
| 700 | <LI>REMOTE_IDENT |
| 701 | <LI>REMOTE_USER |
| 702 | <LI>REQUEST_METHOD |
| 703 | <LI>SCRIPT_NAME |
| 704 | <LI>SERVER_NAME |
| 705 | <LI>SERVER_PORT |
| 706 | <LI>SERVER_PROTOCOL |
| 707 | <LI>SERVER_ROOT |
| 708 | <LI>SERVER_SOFTWARE |
| 709 | </UL> |
| 710 | """ |
| 711 | |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 712 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 713 | # Utilities |
| 714 | # ========= |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 715 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 716 | def escape(s): |
| 717 | """Replace special characters '&', '<' and '>' by SGML entities.""" |
| 718 | s = regsub.gsub("&", "&", s) # Must be done first! |
| 719 | s = regsub.gsub("<", "<", s) |
| 720 | s = regsub.gsub(">", ">", s) |
Guido van Rossum | eb9e9d2 | 1995-02-27 13:16:11 +0000 | [diff] [blame] | 721 | return s |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 722 | |
Guido van Rossum | 9a22de1 | 1995-01-12 12:29:47 +0000 | [diff] [blame] | 723 | |
Guido van Rossum | 7275561 | 1996-03-06 07:20:06 +0000 | [diff] [blame] | 724 | # Invoke mainline |
| 725 | # =============== |
| 726 | |
| 727 | # Call test() when this file is run as a script (not imported as a module) |
| 728 | if __name__ == '__main__': |
| 729 | test() |