karen.shared

Shared library of functions used throughout Karen's various modules

KHTTPRequestHandler

Class to parse an HTTP request into its parts for GET and POST variables, paths, etc. and can handle multipart/form-data requests.

__init__(self, request_text) special

Request Handler Initialization

Parameters:
  • request_text (str) – The full RFC-compliant HTTP request.

parse_GET(self)

Parses the variables from the query string of the HTTP message.

Returns:
  • (dict) – name/value pairs representing the query string values

parse_POST(self)

Parses the variables from the body of the HTTP message.

Returns:
  • (dict) – name/value pairs representing the POST values

send_error(self, code, message)

Sets the error code and message for errors.

Parameters:
  • code (int) – Error code for message.

  • message (str) – Message text for error

KJSONRequest

Helper class for storing the portions of an inbound JSON request.

__init__(self, inContainer, inSocket, inPath, inPayload) special

JSON Request Initialization

Parameters:
  • inContainer (object) – The containing object on which the request was received such as the Brain or DeviceContainer.

  • inSocket (socket) – The client socket on which to send any appropriate responses.

  • inPath (str) – The relative path of the request (e.g. "control").

  • inPayload (object) – The JSON-parsed payload to store for referencing

sendResponse(self, error=False, message='', data=None, httpStatusCode=200, httpStatusMessage='OK')

Sends an HTTP response to the requesting client to close the connection.

Parameters:
  • error (bool) – Indicator sent on payload to indicate success/failure e.g. { error: False, message: "", data: None }.

  • message (str) – The message portion of the payload to be sent e.g. { error: False, message: "", data: None }.

  • data (str) – The data portion of the payload to be sent e.g. { error: False, message: "", data: None }.

  • httpStatusCode (int) – HTTP status code of response. Default is 200.

  • httpStatusMessage (str) – HTTP status response of response. Default is "OK".

Returns:
  • (bool) – True on success and False on failure.

dayPart()

Returns the part of the day based on the system time based on generally acceptable breakpoints.

Returns:
  • (str) – The part of the day for the current moment (night, morning, evening, etc.).

sendHTTPResponse(socketConn, responseType='text/html', responseBody='', httpStatusCode=200, httpStatusMessage='OK')

Sends a HTTP response to an open socket connection.

Parameters:
  • socketConn (socket) – Open TCP socket from inbound HTTP request.

  • responseType (str) – The MIME type of the response message.

  • responseBody (str) – The body of the response message.

  • httpStatusCode (int) – HTTP status code of response. Default is 200.

  • httpStatusMessage (str) – HTTP status response of response. Default is "OK".

Returns:
  • (bool) – True on success and False on failure.

sendJSONRequest(url, payLoad)

Sends a JSON request to a specified URL using the POST method.

Parameters:
  • url (str) – URL for which to delivery the POST message.

  • payLoad (object) – Object to be converted to JSON format and sent as the body of the message.

Returns:
  • (bool, str) – Returns a tuple as (bool, str) indicating if the message was successful or failed and any related message.

sendJSONResponse(socketConn, error=False, message=None, data=None, httpStatusCode=200, httpStatusMessage='OK')

Sends a JSON package as an HTTP response to an open socket connection. Sends all data as "application/json".

Parameters:
  • socketConn (socket) – Open TCP socket from inbound HTTP request.

  • error (bool) – Indicator sent on payload to indicate success/failure e.g. { error: False, message: "", data: None }.

  • message (str) – The message portion of the payload to be sent e.g. { error: False, message: "", data: None }.

  • data (str) – The data portion of the payload to be sent e.g. { error: False, message: "", data: None }.

  • httpStatusCode (int) – HTTP status code of response. Default is 200.

  • httpStatusMessage (str) – HTTP status response of response. Default is "OK".

Returns:
  • (bool) – True on success and False on failure.

threaded(fn)

Thread wrapper shortcut using @threaded prefix

Parameters:
  • fn (function) – The function to executed on a new thread.

Returns:
  • (thread) – New thread for executing function.