testplan.common.utils.sockets package

Submodules

testplan.common.utils.sockets.client module

TCP Client module.

class testplan.common.utils.sockets.client.Client(host: str, port: Union[str, int], interface: Optional[Tuple[str, int]] = None)[source]

Bases: object

A Basic TCP Client that connects to a server via socket interface.

To use this type:

  1. construct it
  2. connect
  3. send and/or receive
  4. close
address

Returns the host and port information of socket.

close() → None[source]

Close the connection.

connect() → None[source]

Connect client to socket.

port
receive(size: int, timeout: int = 30) → bytes[source]

Receive a message.

Parameters:
  • size – Number of bytes to receive.
  • timeout – Timeout in seconds.
Returns:

message received

recv(bufsize: int, flags: int = 0) → bytes[source]

Proxy for Python’s socket.recv().

Parameters:
  • bufsize – Maximum amount of data to be received at once.
  • flags – Defaults to zero.
Returns:

message received

send(msg: bytes) → Tuple[float, int][source]

Send the given message.

Parameters:msg – Message to be sent.
Returns:Timestamp when msg sent (in microseconds from epoch) and number of bytes sent

testplan.common.utils.sockets.codec module

Codec utils.

class testplan.common.utils.sockets.codec.Codec[source]

Bases: object

Codec to serialize/deserialize a message.

parse(buffer)[source]

Creates a string message from buffer.

serialize(msg)[source]

Serialize string message to bytes.

testplan.common.utils.sockets.message module

Server/Client communication message.

class testplan.common.utils.sockets.message.Message(data=None, codec=None)[source]

Bases: object

Message object with its codec to communicate data in a server/client connection.

classmethod from_buffer(data, codec)[source]

Creates new message from buffer.

to_buffer()[source]

Serialize message data.

testplan.common.utils.sockets.server module

TCP Server module.

class testplan.common.utils.sockets.server.Server(host='localhost', port=0, listen=1)[source]

Bases: object

A server that can send and receive messages based on socket interface. Supports multiple connections.

Parameters:
  • host (str) – The host address the server is bound to.
  • port (str or int) – The port the server is bound to.
  • listen (int) – Socket listen argument.
accept_connection(timeout=10, accept_connection_sleep=0.1)[source]

Accepts a connection in the order in which they were received. Return the index of the connection, which can be used to send and receive messages using that connection. If no connection is already available or becomes available in the given timeout, then the method returns -1.

Parameters:
  • timeout (int) – Timeout to wait for receiving connection.
  • accept_connection_sleep (float) – Sleep time to retry accept connection.
Returns:

Index of connection

Return type:

int

bind()[source]

Bind to a socket.

close()[source]

Closes the server and listen thread.

close_connection(conn_idx)[source]

Unregister, close and remove connection with given connection index

Parameters:conn_idx (int) – Connection index of connection to be removed
Returns:None
Return type:NoneType
host

Input host provided.

ip

IP retrieved from socket.

port

Port retrieved after binding.

receive(size=1024, conn_idx=None, timeout=30, wait_full_size=True)[source]

Receive a message of given size (number of bytes) from the given connection.

Parameters:
  • size (int) – Number of bytes to receive
  • conn_idx (int) – Index of connection to receive from
  • timeout (int) – timeout in seconds
  • wait_full_size (bool) – Wait until full size is received.
Returns:

message received

Return type:

bytes

send(msg, conn_idx=None, timeout=30)[source]

Send the given message through the given connection.

Parameters:
  • msg (bytes) – message to be sent
  • conn_idx (int) – Index of connection to send to
  • timeout (int) – Timeout in seconds for sending all bytes
Returns:

Number of bytes sent

Return type:

int

serve(loop_sleep=0.005, listening_timeout=5)[source]

Start serving connections.

socket

Returns the underlying socket object

testplan.common.utils.sockets.tls module

class testplan.common.utils.sockets.tls.DefaultTLSConfig(cacert: Union[str, os.PathLike, None] = None)[source]

Bases: testplan.common.utils.sockets.tls.TLSConfig

This TLSConfig create a default SSLContext as defined in ssl lib

Parameters:cacert – Optional Root CA certificate
get_context(purpose: <unknown>.Purpose) → ssl.SSLContext[source]

The implementation of this function need to return a configured SSLContext, example implementations: DefaultTLSConfig and SimpleTLSConfig

Parameters:purpose – Either host or client certificate
Returns:should return the configured SSLContext
class testplan.common.utils.sockets.tls.SimpleTLSConfig(key: Union[os.PathLike, str], cert: Union[os.PathLike, str], cacert: Union[str, os.PathLike, None])[source]

Bases: testplan.common.utils.sockets.tls.TLSConfig

This TLSConfig create SSLContext for host or user auth with the private key and certificate provided

Parameters:
  • key – path to private key file
  • cert – path to the certificate path (host or user)
  • cacert – optional path to the root CA certificate
get_context(purpose: <unknown>.Purpose) → ssl.SSLContext[source]

The implementation of this function need to return a configured SSLContext, example implementations: DefaultTLSConfig and SimpleTLSConfig

Parameters:purpose – Either host or client certificate
Returns:should return the configured SSLContext
class testplan.common.utils.sockets.tls.TLSConfig[source]

Bases: abc.ABC

Defines the Protocol a TLSConfig need to have

get_context(purpose: <unknown>.Purpose) → ssl.SSLContext[source]

The implementation of this function need to return a configured SSLContext, example implementations: DefaultTLSConfig and SimpleTLSConfig

Parameters:purpose – Either host or client certificate
Returns:should return the configured SSLContext

Module contents

Socket server client utils.