spyce
home
license
community
download
examples
resources
wishlist
contrib (@sf)
documentation
intro
lang
runtime
modules
tags
install
exits
sourceforge
statistics
freshmeat

Documentation - Modules
[[ Spyce ]]
Python Server Pages
by Rimon Barr

Prev: 4 - Modules Up: 4 - Modules Next: 4.2 - Response

4.1. Request

The request module is loaded implicitly into every Spyce environment. It provides the following methods:

  • uri( [component] ):
    Returns the request URI, or some component thereof. If the optional component parameter is specified, it should be one of the following strings: 'scheme', 'location', 'path', 'parameters', 'query' or 'fragment'.
  • method():
    Returns request method type (GET, POST, ...)
  • query():
    Returns the request query string
  • get( [name], [default], [ignoreCase] ):
    Returns request GET information. If name is specified then a single list of values is returned if the parameter exists, or default, which defaults to None, if the parameter does not exist. Parameters without values are skipped, though empty string values are allowed. If name is omitted, then a dictionary of lists is returned. If ignoreCase is true, then the above behaviour is performed in a case insensitive manner (all parameters are treated as lowercase).
  • get1( [name], [default], [ignoreCase] ):
    Returns request GET information, similarly to (though slightly differently from) the function above. If name is specified then a single string is returned if the parameter exists, or default, which default to None, if the parameter does not exist. If there is more than one value for a parameter, then only one is returned. Parameters without values are skipped, though empty string values are allowed. If name is omitted, then a dictionary of strings is returned. If the optional ignoreCase flag is true, then the above behaviour is performed in a case insensitive manner (all parameters are treated as lowercase).
  • post( [name], [default], [ignoreCase] ):
    Returns request POST information. If name is specified then a single list of values is returned if the parameter exists, or default, which defaults to None, if the parameter does not exist. Parameters without values are skipped, though empty string values are allowed. If name is omitted, then a dictionary of lists is returned. If ignoreCase is true, then the above behaviour is performed in a case insensitive manner (all parameters are treated as lowercase). This function understands form information encoded either as 'application/x-www-form-urlencoded' or 'multipart/form-data'. Uploaded file parameters are not included in this dictionary; they can be accessed via the file method.
  • post1( [name], [default], [ignoreCase] ):
    Returns request POST information, similarly to (though slightly differently from) the function above. If name is specified then a single string is returned if the parameter exists, or default, which defaults to None, if the parameter does not exist. If there is more than one value for a parameter, then only one is returned. Parameters without values are skipped, though empty string values are allowed. If name is omitted, then a dictionary of strings is returned. If the optional ignoreCase flag is true, then the above behaviour is performed in a case insensitive manner (all parameters are treated as lowercase). This function understands form information encoded either as 'application/x-www-form-urlencoded' or 'multipart/form-data'. Uploaded file parameters are not included in this dictionary; they can be accessed via the file method.
  • file( [name], [ignoreCase] ):
    Returns files POSTed in the request. If name is specified then a single cgi.FieldStorage class is returned if such a file parameter exists, otherwise None. If name is omitted, then a dictionary of file entries is returned. If the optional ignoreCase flag is true, then the above behaviour is performed in a case insensitive manner (all parameters are treated as lowercase). The interesting fields of the FieldStorage class are:

    • name: the field name, if specified; otherwise None
    • filename: the filename, if specified; otherwise None; this is the client-side filename, not the filename in which the content is stored - a temporary file you don't deal with
    • value: the value as a string; for file uploads, this transparently reads the file every time you request the value
    • file: the file(-like) object from which you can read the data; None if the data is stored a simple string
    • type: the content-type, or None if not specified
    • type_options: dictionary of options specified on the content-type line
    • disposition: content-disposition, or None if not specified
    • disposition_options: dictionary of corresponding options
    • headers: a dictionary(-like) object (sometimes rfc822.Message or a subclass thereof) containing *all* headers

  • __getitem__( key ):
    The request module can be used as a dictionary: i.e. request['foo']. This method first calls the get1() method, then the post1() method and lastly the file() method trying to find the first non-None value to return. If no value is found, then this method returns None. Note: Throwing an exception seemed too strong a semantics, and so this is a break from Python. One can also iterate over the request object, as if over a dictionary of field names in the get1 and post1 dictionaries. In the case of overlap, the get1() dictionary takes precedence.
  • getpost( [name], [default], [ignoreCase] ):
    Using given parameters, return get() result if not None, otherwise return post() result if not None, otherwise default.
  • getpost1( [name], [default], [ignoreCase] ):
    Using given parameters, return get1() result if not None, otherwise return post1() result if not None, otherwise default.
  • postget( [name], [default], [ignoreCase] ):
    Using given parameters, return post() result if not None, otherwise return get() result if not None, otherwise default.
  • postget1( [name], [default], [ignoreCase] ):
    Using given parameters, return post1() result if not None, otherwise return get1() result if not None, otherwise default.
  • env( [name], [default] ):
    Returns a dictionary with CGI-like environment information of this request. If name is specified then a single entry is returned if the parameter exists, otherwise default, which defaults to None, if omitted.
  • getHeader( [type] ):
    Return a specific header sent by the browser. If optional type is omitted, a dictionary of all headers is returned.
  • filename( [path] ):
    Return the Spyce filename of the request currently being processed. If an optional path parameter is provided, then that path is made relative to the Spyce filename of the request currently being processed.
  • default( value, value2 ):
    (convenience method) Return value if it is not None, otherwise return value2.
Dynamic web pages frequently need to access GET and POST information sent by the browser. Here is an example that shows this is done.
examples/getpost.spy
<html><body>
  Getting GET or POST request information is easy. <br>
  Use the following forms to submit GET or POST info.<br>
  <hr>
  [[-- input forms --]]
  <form action="[[=request.uri('path')]]" method=get>
    get: <input type=text name=Name>
    <input type=submit value=ok>
  </form>
  <form action="[[=request.uri('path')]]" method=post>
    post: <input type=text name=Name>
    <input type=submit value=ok>
  </form>
  <hr>
  [[-- display GET and POST information from request object --]]
  <table><tr>
      <td>request.method()</td>
      <td>[[=request.method()]]</td>
    </tr><tr>
      <td>request.query()</td>
      <td>[[=request.query()]]</td>
    </tr><tr>
      <td>request.get1('name')</td>
      <td>[[=request.get1('name')]]</td>
    </tr><tr>
      <td>request.post1('name')</td>
      <td>[[=request.post1('name')]]</td>
    </tr><tr>
      <td>request.get1('name', 1)</td>
      <td>[[=request.get1('name', 1)]]</td>
    </tr><tr>
      <td>request.post1('name', 1)</td>
      <td>[[=request.post1('name', 1)]]</td>
    </tr><tr>
      <td>request['Name']</td>
      <td>[[=request['Name'] ]]</td>
    </tr><tr>
      <td>request['name']</td>
      <td>[[=request['name'] ]]</td>
    </tr><tr>
      <td>request.get()</td>
      <td>[[=request.get()]]</td>
    </tr><tr>
      <td>request.get1()</td>
      <td>[[=request.get1()]]</td>
    </tr><tr>
      <td>request.post()</td>
      <td>[[=request.post()]]</td>
    </tr><tr>
      <td>request.post1()</td>
      <td>[[=request.post1()]]</td>
    </tr><tr>
      <td>request.getpost()</td>
      <td>[[=request.getpost()]]</td>
    </tr><tr>
      <td>request.getpost1()</td>
      <td>[[=request.getpost1()]]</td>
    </tr><tr>
      <td>request.postget()</td>
      <td>[[=request.postget()]]</td>
    </tr><tr>
      <td>request.postget1()</td>
      <td>[[=request.postget1()]]</td>
    </tr><tr>
      <td>request.get(ignoreCase=1)</td>
      <td>[[=request.get(ignoreCase=1)]]</td>
    </tr><tr>
      <td>request.get1(ignoreCase=1)</td>
      <td>[[=request.get1(ignoreCase=1)]]</td>
    </tr><tr>
      <td>request.post(ignoreCase=1)</td>
      <td>[[=request.post(ignoreCase=1)]]</td>
    </tr><tr>
      <td>request.post1(ignoreCase=1)</td>
      <td>[[=request.post1(ignoreCase=1)]]</td>
    </tr><tr>
      <td>request.getpost(ignoreCase=1)</td>
      <td>[[=request.getpost(ignoreCase=1)]]</td>
    </tr><tr>
      <td>request.getpost1(ignoreCase=1)</td>
      <td>[[=request.getpost1(ignoreCase=1)]]</td>
    </tr><tr>
      <td>request.postget(ignoreCase=1)</td>
      <td>[[=request.postget(ignoreCase=1)]]</td>
    </tr><tr>
      <td>request.postget1(ignoreCase=1)</td>
      <td>[[=request.postget1(ignoreCase=1)]]</td>
  </tr></table>
</body></html>
Run this code.
(requires Spyce-enabled web server)

The example below presents the results of all the method calls list above. Run it to understand the information available.

examples/request.spy
<html><body>
  Using the Spyce request object, we can obtain 
  information sent along with the request. The 
  table below shows some request methods and their 
  return values. Use the form below to post form 
  data via GET or POST. <br>
  <hr>
  [[-- input forms --]]
  <form action="[[=request.uri('path')]]" method=get>
    get: <input type=text name=name>
    <input type=submit value=ok>
  </form>
  <form action="[[=request.uri('path')]]" method=post>
    post: <input type=text name=name>
    <input type=submit value=ok>
  </form>
  <hr>
  [[-- tabulate response information --]]
  <table border=1>
    <tr>
      <td><b>Method</b></td>
      <td><b>Return value</b></td>
    </tr>
    [[ for method in ['uri()', 'uri("path")',
      'uri("query")', 'method()','query()',
      'get()','get1()', 'post()','post1()',
      'getHeader()','env()', 'filename()']: { 
    ]]
      <tr>
        <td valign=top>request.[[=method]]</td>
        <td>[[=eval('request.%s' % method)]]</td>
      </tr>
    [[ } ]]
  </table>
</body></html>
Run this code.
(requires Spyce-enabled web server)

A more complicated form...

examples/form.spy
<html><body>
Form handling. <br><hr>
<b>Form:</b><br>
<form method=post action="[[=request.uri('path')]]">
<table>
  <tr>
    <td valign=top>Text field:</td>
    <td><input name="textField" type=text value="" size=15></td>
  </tr><tr>
    <td valign=top>Textarea field:</td>
    <td><textarea name="textareaField" rows="5" cols="30"></textarea></td>
  </tr><tr>
    <td valign=top>Select field:</td>
    <td><select name="selectField">
      <option>Option 1</option>
      <option>Option 2</option>
      <option>Option 3</option>
    </select></td>
  </tr><tr>
    <td valign=top>Radio field:</td>
    <td>
      <input type="radio" name="radioField" value="Option 1">Option 1<br>
      <input type="radio" name="radioField" value="Option 2">Option 2<br>
      <input type="radio" name="radioField" value="Option 3">Option 3<br>
    </td>
  </tr><tr>
    <td valign=top>Checkbox field:</td>
    <td>
      <input type="checkbox" name="checkField" value="Option 1">Option 1<br>
      <input type="checkbox" name="checkField" value="Option 2">Option 2<br>
      <input type="checkbox" name="checkField" value="Option 3">Option 3<br>
    </td>
  </tr><tr>
    <td valign=top></td>
    <td><input type=submit value="Submit"></td>
  </tr>
</table>
</form><hr>

[[p=request.post1]]
<b>Results:</b><br>
<table>
  <tr><td>Text field:</td><td>[[=p('textField')]]</td></tr>
  <tr><td>Textarea field:</td><td>[[=p('textareaField')]]</td></tr>
  <tr><td>Select field:</td><td>[[=p('selectField')]]</td></tr>
  <tr><td>Radio field:</td><td>[[=p('radioField')]]</td></tr>
  <tr><td>Checkbox field:</td><td>[[=request.post('checkField')]]</td></tr>
</table>
</body></html>
Run this code.
(requires Spyce-enabled web server)

Lastly, the following example shows how to deal with uploaded files.

examples/fileupload.spy
[[\ 
if request.post('ct'):
  response.setContentType(request.post1('ct'))
  if request.file('upfile')!=None:
    response.write(request.file('upfile').value)
  else:
    print 'file not properly uploaded'
  raise spyceDone
]]
<html><body>
  Upload a file and it will be sent back to you.<br>
  [[-- input forms --]]
  <hr>
  <table>
    <form action="[[=request.uri('path')]]" method=post 
        enctype="multipart/form-data">
      <tr>
        <td>file:</td>
        <td><input type=file name=upfile></td>
      </tr><tr>
        <td>content-type:</td>
        <td><input type=text name=ct value="text/html"></td>
      </tr><tr>
        <td><input type=submit value=ok></td>
      </tr>
    </form>
  </table>
</body></html>
Run this code.
(requires Spyce-enabled web server)


Prev: 4 - Modules Up: 4 - Modules Next: 4.2 - Response


© 2002-06 Rimon Barr
email: rimon@acm.org
Spyce Powered SourceForge Logo [[ Spyce ]]
Python Server Pages
version 1.3.13