KnutTCPRequestHandler

class knut.server.tcpserver.KnutTCPRequestHandler(request, client_address, server)

Request handler for the KnutTCPServer.

The request handler is instantiated once per connection to the server and kept open until either the client closes the connection or the server is shutdown.

ENCODING = 'utf-8'

The encoding in which messages are received and send.

HEARTBEAT_FREQUENCY = 1

The frequency in which the heartbeat is send measured in Hz. See heartbeat() for more about the heartbeat.

heartbeat()None

Sends frequently a heartbeat.

The heartbeat is a frequently send message, where only a null byte b'\x00' is send.

msg_builder(apiid: int, msgid: int, msg: dict, encoding: str)bytearray

Return a Knut message.

Build a Knut message for the API apiid and the message msg of type msgid. The Knut message is terminated by an null byte b'\x00'.

For example:

>>> msg_builder(2, 2, {}, 'utf-8')
bytearray(b'{"apiid": 2, "msgId": 2, "msg": {}}\x00')
request_service(apiid: int, msgid: int, msg: dict) → Tuple[int, dict]

Returns a service’s response.

This method calls the request_handler() of the API with the apiid. The API’s request handler handles then the msg of type msgid. The returned tuple holds the responses msgid and msg. If no API for apiid is found in the servers API dictionary, the msg and msgid is returned.

send_heartbeat = None

If true, heartbeat() is called with a frequency of HEARTBEAT_FREQUENCY.

send_queued(msg: bytearray)None

Sends a queued message.

Puts the msg to the message queue and sends all messages form the queue until its empty.

send_queued_knutmsg(apiid: int, msgid: int, msg: dict)None

Sends a queued Knut message.

Sends the msg of type msgid from the apiid as a queued Knut message.