Server

Description

class lab_utils.socket_comm.Server(config_file: str = None, pid_file_name: str = None)[source]

Daemon-like TCP server. The server connects to the specified host and port and locks a PID file to ensure it is the only instance running.

If successful, the server will then listen indefinitely, waiting for a client to connect. Upon connection, a message is received and passed to the parser. If the message is valid, the parser will call the respective method. The base class provides only the quit() method; users should create new methods suitable for their needs. These methods should always set an appropriate reply, which will be then sent back to the client.

If a message is not valid (i.e. the parser does not support it), an error message and a complete help string is sent back to the client. The help string by the argparse library is not complete and hence is overridden by the ArgumentParser.full_help() method.

TODO: Create some examples of message parsing.

Attributes

Server.address

TCP binding address.

Server.host

Host address.

Server.lock

LockFile object.

Server.logger

Single logger for the whole class.

Server.max_backlog

TCP connection queue.

Server.message

Message from the client.

Server.namespace

Container to hold message options.

Server.parser

Argument parser.

Server.pid_file_name

The PID file name

Server.port

Connection port.

Server.quit_flag

Internal flag to stop the daemon.

Server.reply

Reply to the client.

Server.sock

Connection socket.

Server.socket_timeout

Socket time-out, used for Ctrl+C handling

Server.sp

Argument subparser

Methods

Server.__init__

Initializes and runs the Server object.

Server.config

Loads the server configuration from a file.

Server.create_parser

Configures the message parser, which will call the appropriate method upon reception of a message.

Server.daemonize

Locks a PID file to ensure that a single instance of the server is running.

Server.quit

User-defined task example.

Server.signal_handler

Server.start_daemon

Starts the server.