socktools.examples.simple_chat

This module implements a toy multiuser chat server, clients can connect using standard tools like netcat and telnet and chat to other clients.

If you want to test it, simply run it as a normal python script and then connect to localhost on port 31337.

Note: this is provided as an example only, it is highly recommended that you do NOT try to run a production server on this code as-is.

class socktools.examples.simple_chat.ChatProtocol(bind=None, connect=None, handlers={}, timeout=10, tick_interval=0.25, sock=None, meta_sock=None, logger=None, no_async=False)[source]

Bases: socktools.tcp_linemode_mixin.TCPLinemodeMixin, socktools.tcp_sock.TCPSock

Multiuser chat implementation

Protocols in socktools are either classes that inherit from a socket or mixins that can be added to another application-specific class later along with the socket.

For the multiuser chat protocol we only want TCP, messages are simply newline terminated.

Homework exercise: implement message types and use them to specify IRC-style channels, or outright implement IRC. Hint: you need handlers and a modified parser.

get_default_handlers()[source]

We setup handlers here

In this example, there’s only a single message type (0) - also known as the default message type

Returns:the message handlers dict, updated to configure message type 0 so it calls handle_msg
Return type:dict
handle_all(from_addr, msg_type, msg_data)[source]

All messages get transformed to show other users where they came from.

See base_sock.py for information on this method. Note that it is generally not good style to use this method for doing actual application logic, it should only transform.

Homework exercise: make users pseudo-anonymous by hashing their IP address and add in IRC-like ops with /kick and /ban functionality

handle_msg(from_addr, msg_type, msg_data)[source]

Handler for message type 0

By default, this is the only message handler used, others can of course be setup and should be done in get_default_handlers().

This method is also gloriously simple - it just broadcasts msg_data to all other connected clients.