WhakerPy 1.0

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

Module whakerpy.httpd

Class HTTPDStatus

Description

A status code value of an HTTPD server.

HTTPD status codes are issued by a server in response to a client's request made to the server. All HTTP response status codes are separated into five classes or categories. The first digit of the status code defines the class of response, while the last two digits do not have any classifying or categorization role. There are five classes defined by the standard:

  • 1xx informational response – the request was received, continuing process
  • 2xx successful – the request was successfully received, understood, and accepted
  • 3xx redirection – further action needs to be taken in order to complete the request
  • 4xx client error – the request contains bad syntax or cannot be fulfilled
  • 5xx server error – the server failed to fulfil an apparently valid request

Constructor

Create the private member for the status code.

Default status code is 200 for an "OK" httpd response.

Parameters
  • code
View Source
def __init__(self, code: int=200):
    """Create the private member for the status code.

    Default status code is 200 for an "OK" httpd response.

    """
    self.__scode = self.check(code)

Public functions

check

Raise an exception if given status value is invalid.

Parameters
  • value: (int) A response status.
Raises

sppasHTTPDValueError

Returns
  • (int) value
View Source
@staticmethod
def check(value):
    """Raise an exception if given status value is invalid.

        :param value: (int) A response status.
        :raises: sppasHTTPDValueError
        :return: (int) value

        """
    try:
        value = int(value)
    except ValueError:
        raise HTTPDValueError(value)
    if value not in HTTPDStatus.HTTPD_STATUS.keys():
        raise HTTPDValueError(value)
    return value

get

Return the status code value (int).

View Source
def get(self):
    """Return the status code value (int)."""
    return self.__scode

set

Set a new value to the status code.

Parameters
  • value: (int) HTTPD status code value.
Raises

sppasHTTPDValueError

View Source
def set(self, value):
    """Set a new value to the status code.

        :param value: (int) HTTPD status code value.
        :raises: sppasHTTPDValueError

        """
    value = self.check(value)
    self.__scode = value

to_html

Create an error HTML page for the instance of status error and return the tree instance (or serialize).

Parameters
  • encode: (bool) Optional, False by default, Boolean to know if we serialize the return or not
  • msg_error: (str) Optional, an error message for more information for the user
Returns
  • (HTMLTreeError | bytes) the tree error generated, encoded in bytes for response or object instance
View Source
def to_html(self, encode: bool=False, msg_error: str=None) -> HTMLTreeError | bytes:
    """Create an error HTML page for the instance of status error and return the tree instance (or serialize).

        :param encode: (bool) Optional, False by default, Boolean to know if we serialize the return or not
        :param msg_error: (str) Optional, an error message for more information for the user

        :return: (HTMLTreeError | bytes) the tree error generated, encoded in bytes for response or object instance

        """
    tree = HTMLTreeError(self, msg_error)
    if encode is True:
        return tree.serialize().encode('utf-8')
    else:
        return tree

Overloads

__str__

View Source
def __str__(self):
    return str(self.__scode)

__repr__

View Source
def __repr__(self):
    return '{:d} {:s}'.format(self.__scode, HTTPDStatus.HTTPD_STATUS[self.__scode])

__eq__

View Source
def __eq__(self, other):
    return self.__scode == other