socktools.json_mixin - JSON formatted messages

This module implements a mixin that implements the parse_msg() method using JSON. Messages are assumed to be JSON lists of (msg_type,msg_data).

You may send JSON messages using send_msg().

class socktools.json_mixin.JSONParseMixin[source]

Bases: object

A mixin for parsing JSON messages

This class is most suited to the websocket server:
class MyClass(JSONParseMixIn,WSGIWebsocketSock):
      pass
parse_msg(data)[source]

Parse a raw message in JSON format

This method assumes the message is encoded as a JSON list object of (msg_type,msg_data).

Warning

Catching exceptions for malformed packets is left up to caller - ultimately this means your application code.

Parameters:data (str) – The raw packet to parse
Returns:a tuple of (msg_type,msg_data)
Return type:tuple
serialise_msg(msg_type, msg_data)[source]

Serialise a message in JSON format

This method serialises msg_data (which should be a dict or sequence) into a JSON list object of (msg_type,msg_data).

Parameters:
  • msg_type (int) – the message type as an integer
  • msg_data (dict) – usually a dict, the actual message contents