wsgikit.HttpRequest

class wsgikit.HttpRequest(environ = None, encoding = 'utf-8', files_upload_on = True, uploaded_files_dir = '/tmp', max_uploaded_files = 20, max_filesize = 2097152, max_content_length = 8388608, uploaded_file_prefix = 'http-upload-', read_block_size = 8192, max_body_header_size = 4096)
Parameters:
  • environ – WSGI environment to parse
  • encoding – str - [optional] HTTP request encoding, default - ‘utf-8’
  • files_upload_on – bool - [optional] turns on/off file upload with HTTP requests, default - True
  • uploaded_files_dir – str - [optional] path to directory, which stores temporary uploaded files, default - ‘/tmp’
  • max_uploaded_files – int - [optional] max number of allowed files to upload, default - 20
  • max_filesize – int - [optional] max allowed uploaded file size in bytes, default - 2097152 (2MB)
  • max_content_length – int - [optional] max allowed size in bytes for request body, excluding the attached files. Default - 8388608 (8MB)
  • uploaded_file_prefix – str - [optional] prefix to use for temporary uploaded files, default - ‘http-upload-‘
  • read_block_size – int - [optional] size of data read buffer, default - 8192 (8KB)
  • max_body_header_size – int - [optional] max size in bytes for each part header section in multipart requests

HttpRequest object handles an HTTP request, parses and provides a request data in a suitable format for further processing.

Actually it is designed to handle a web environment in WSGI application.

Benefit of using the wsgikit.HttpRequest:

  • significantly faster than standard cgi module (savings grow exponentially to the amount of data in request)
  • Protection to the WSGI application from flooding (provides configurable limitations to the amount of allowed data in request body, limits a number of attached files and file size limit)
  • Comfortable PHP-like representation of the request parameters

Like:

foo[][bar]=1&foo[][baz]=2&foo[xyz]=777

will be parsed to the following dictionary:

{
        foo : {
                0 : {
                        "bar" : 1
                },
                1 : {
                        "baz" : 2
                },
                "xyz" : 777
        }
}
  • Provides clean and tiny interface to work with request parameters, web-server environment, uploaded files, cookies, etc.

Basic usage example:

import wsgikit

def my_wsgi_app( environ, start_response):
        status = '200 OK'
        response_headers = [('Content-type','text/plain')]
        start_response( status, response_headers)
        
        request = wsgikit.HttpRequest( environ)
        
        return str( request)

Public Properties:

Variables:
  • QUERY – dict - Storage for handling QUERY_STRING parsed parameters (equivalent to PHP’s $_GET)
  • BODY – dict - Storage for handling request body parsed parameters (equivalent to PHP’s $_POST)
  • COOKIE – dict - Storage for handling cookie parsed variables (equivalent to PHP’s $_COOKIE)
  • HEADERS – dict - Storage for handling parsed request headers
  • SERVER – dict - Storage for handling all server environment. It stores all WSGI environ, except ‘wsgi.input’ (equivalent to PHP’s $_SERVER)
  • FileUploader – wsgikit.FileUploader - Instance of FileUploader object associated with request
  • method – str - Shortcut to wsgikit.HttpRequest.server(‘REQUEST_METHOD’)
  • FILES – dict - Storage for handling uploaded files (equivalent to PHP’s $_FILES, but the structure is different)

File element structure is:

{
        "filename" : str - uploaded file name,
        "headers"  : dict - headers retrieved from the attachment part of te file in request body
        "tmp_name" : str - path to temporary file location
        "length"   : int - file size in bytes
        "mime"     : str - file mime-type
}

Public Methods:

body(self, name=None, default_value=None)

Returns the content of BODY storage if none name passed or returns a value for body parameter with given name

Example:

request = wsgikit.HttpRequest( environ)
# returns dict of all request body parameters
request.body()

# returns value of passed_param or None if param not found
request.body('passed_param')
Parameters:
  • name – [optional] name of a body parameter to get the value
  • default_value – [optional] value to return if no body parameter with given name found
Return type:

mixed - dict of body params or param value

cookie(self, name=None, default_value=None)

Returns the content of COOKIE storage if none name passed or returns a value for cookie parameter with given name

Example:

request = wsgikit.HttpRequest( environ)
# returns dict of all cookie variables passed from client
request.cookie()

# returns value of 'varname' from cookie or None if variable not found
request.cookie('varname')
Parameters:
  • name – [optional] name of a cookie parameter to get the value
  • default_value – [optional] value to return if no cookie parameter with given name found
Return type:

mixed - dict of cookie params or param value

file(self, name=None, default_value=None)

Returns the content of FILES storage if none name passed or returns a value for uploaded files with given name

Example:

request = wsgikit.HttpRequest( environ)
# returns dict of all uploaded files within the request
request.file()

# returns file(s) description object(s) having name 'file_param_name', or None if not found
request.file('file_param_name')
Parameters:
  • name – [optional] name of a uploaded files to get the value
  • default_value – [optional] value to return if no uploaded files with given name found
Return type:

mixed - dict of uploaded files or its part by a given name

getenv(self, storage, key=None, default_value=None)

Returns an entire storage or a value stored in this storage retrieved by a given key. Known storages are:

  • SERVER - server environment variables (everything which is inside WSGI environ except ‘wsgi.input’)
  • HEADERS - all HTTP headers in current request
  • QUERY - dictionary of parsed QUERY_STRING parameters
  • BODY - dictionary of parsed parameters found in request’s body
  • COOKIE - dictionary of parsed COOKIE parameters
  • FILES - dictionary of uploaded files
Parameters:
  • storage – name of a storage
  • key – [optional] key of the value to retrieve from tha given storage
  • default_value – [optional] if no value is present in storage by a given key this will be returned (default is None)
Return type:

mixed - storage dict or value or default_value

has_body(self)

Returns True if HTTP request body contains any params, False otherwise

Return type:bool

Returns True if HTTP request pass any cookie variables, False otherwise

Return type:bool
has_files(self)

Returns True if HTTP request contains any files attached and uploaded, False otherwise

Return type:bool
has_query(self)

Returns True if HTTP request contains any params passed with QUERY_STRING, False otherwise

Return type:bool
header(self, name=None, default_value=None)

Returns the content of HEADER storage if none name passed or returns a value for header with given name

Example:

request = wsgikit.HttpRequest( environ)
# returns dict of all headers
request.header()

# returns value of Content-Type header
request.header('Content-Type')

# returns value of X-MyHeader or False if header not found
request.header('X-My-Header', False)
Parameters:
  • name – [optional] name of a header to get the value
  • default_value – [optional] value to return if no header with given name found
Return type:

dict or str - headers dict or string header value

classmethod parse_query(this, query_string)

Static method providing an ability to parse QUERY_STRING-like parameters in the same way as this object does.

Usage example:

import wsgikit

my_params = 'f[]=2&f[]=3&f[][]=4&f[][]=5&f[][]=6'
print( wsgikit.HttpRequest.parse_query( my_params))
Parameters:query_string – str - key=value params delimited with &
Return type:dict - dictionary of parsed parameters
query(self, name=None, default_value=None)

Returns the content of QUERY storage if none name passed or returns a value for QYERY_STRING parameter with given name

Example:

request = wsgikit.HttpRequest( environ)
# returns dict of all QUERY_STRING parameters
request.query()

# returns value of passed_param in QUERY_STRING or None if param not found
request.query('passed_param')
Parameters:
  • name – [optional] name of a QUERY_STRING parameter to get the value
  • default_value – [optional] value to return if no QUERY_STRING parameter with given name found
Return type:

mixed - dict of QUERY_STRING params or param value

server(self, name=None, default_value=None)

Returns the content of SERVER environment or the value of server environment variable.

Example:

request = wsgikit.HttpRequest( environ)
# returns dict of all server environment variables
request.server()

# returns value of 'REMOTE_ADDR' server environment variable
request.header('REMOTE_ADDR')
Parameters:
  • name – [optional] name of a server environment variable to get the value
  • default_value – [optional] value to return if no server environment variable with given name found
Return type:

mixed - dict of body params or param value

to_dict(self)

Converts HttpRequest to dictionary presentation

Return type:dict - dictionary representation of HttpRequest object

Previous topic

Welcome to WSGIkit’s documentation!

Next topic

wsgikit.FileUploader

This Page