WhakerPy 0.8

https://sourceforge.net/projects/whakerpy/

Module whakerpy.httpd

Class BaseHTTPDServer

Description

A base class for any custom HTTPD server.

It adds a dictionary of the HTML page's bakery this server can handle and the name of the default page.

Example
 >>> s = BaseHTTPDServer(server_address, app_handler)
 >>> s.create_pages()

Constructor

Create the server instance and add custom members.

View Source
def __init__(self, *args, **kwargs):
    """Create the server instance and add custom members.

    """
    super(BaseHTTPDServer, self).__init__(*args, **kwargs)
    self._pages = dict()
    self._default = 'index.html'

Public functions

default

View Source
def default(self):
    return self._default

create_pages

To be overridden. Add bakeries for dynamic HTML pages.

The created pages are instances of the BaseResponseRecipe class. Below is an example on how to override this method:

Example
 > if app == "main":
 > self._pages["index.html"] = BaseResponseRecipe("index.html", HTMLTree("Index"))
 > self._pages["foo.html"] = WebResponseRecipe("foo.html", HTMLTree("Foo"))
 > elif app == "test":
 > self._pages["test.html"] = BaseResponseRecipe("test.html", HTMLTree("test"))
Parameters
  • app: (str) Any string definition for custom use
View Source
def create_pages(self, app: str='app'):
    """To be overridden. Add bakeries for dynamic HTML pages.

        The created pages are instances of the BaseResponseRecipe class.
        Below is an example on how to override this method:

        :example:
        if app == "main":
            self._pages["index.html"] = BaseResponseRecipe("index.html", HTMLTree("Index"))
            self._pages["foo.html"] = WebResponseRecipe("foo.html", HTMLTree("Foo"))
        elif app == "test":
            self._pages["test.html"] = BaseResponseRecipe("test.html", HTMLTree("test"))

        :param app: (str) Any string definition for custom use

        """
    raise NotImplementedError

page_bakery

Return the page content and response status.

This method should be invoked after a POST request in order to take the events into account when baking the HTML page content.

Parameters
  • page_name: (str) Requested page name
  • headers: (dict) The headers ot the http request
  • events: (dict) key=event name, value=event value
  • hastoreturn_data: (bool) False by default - Boolean to know if the server return data or html page
Returns
  • tuple(bytes, HTTPDStatus)
View Source
def page_bakery(self, page_name: str, headers: dict, events: dict, has_to_return_data: bool=False) -> tuple:
    """Return the page content and response status.

        This method should be invoked after a POST request in order to
        take the events into account when baking the HTML page content.

        :param page_name: (str) Requested page name
        :param headers: (dict) The headers ot the http request
        :param events: (dict) key=event name, value=event value
        :param has_to_return_data: (bool) False by default - Boolean to know if the server return data or html page

        :return: tuple(bytes, HTTPDStatus)

        """
    return HTTPDHandlerUtils.bakery(self._pages, page_name, headers, events, has_to_return_data)