sshmitm.plugins.session.tcpserver module

A Python module for a TCP server that runs as a thread and accepts incoming connections.

This module contains the TCPServerThread class, which implements a basic TCP server that listens for incoming connections and launches a new thread for each connection request.

Example usage:

from sshmitm.plugins.session.tcpserver import TCPServerThread

def handle_request(server_address, client_socket, client_address):
    print("Received connection from", client_address)
    client_socket.sendall(b"Hello, client!")
    client_socket.close()

server = TCPServerThread(request_handler=handle_request, port=1234)
server.start()
class sshmitm.plugins.session.tcpserver.TCPServerThread(request_handler: Optional[Callable[[Tuple[str, int], Union[socket, Channel], Tuple[str, int]], None]] = None, network: str = '127.0.0.1', port: int = 0, run_status: bool = True, daemon: bool = False)

Bases: Thread

A TCP server thread that accepts incoming connections and launches a new thread for each connection.

Parameters:
  • request_handler (Optional[Callable[Tuple[str, int], Union[socket.socket, paramiko.Channel], Tuple[str, int]], None]) – a function to be called for each connection request

  • network (str) – network address for the server to bind to

  • port (int) – port number for the server to listen on

  • run_status (bool) – whether the server should run or not

  • daemon (bool) – whether the server should run as a daemon

Returns:

None

close() None

Join all the active threads and close the server socket.

Returns:

None

handle_request(client: Union[socket, Channel], addr: Tuple[str, int]) None

Call the handle request callback for a new connection.

Parameters:
  • client (Union[socket.socket, paramiko.Channel]) – The client’s socket or paramiko channel.

  • addr (Tuple[str, int]) – Tuple containing the address information of the client.

Returns:

None

run() None

Start the server thread and continuously check for incoming connections.

Returns:

None