testplan.common.utils.sockets package
Subpackages
Submodules
testplan.common.utils.sockets.client module
TCP Client module.
- class testplan.common.utils.sockets.client.Client(host: str, port: str | int, interface: Tuple[str, int] | None = None)[source]
Bases:
object
A Basic TCP Client that connects to a server via socket interface.
To use this type:
construct it
connect
send and/or receive
close
- property address: Tuple[str, int]
Returns the host and port information of socket.
- property port: str | int
- 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
testplan.common.utils.sockets.codec module
Codec utils.
testplan.common.utils.sockets.message module
Server/Client communication message.
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
orint
) – 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
- 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
- property host
Input host provided.
- property ip
IP retrieved from socket.
- property 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 receiveconn_idx (
int
) – Index of connection to receive fromtimeout (
int
) – timeout in secondswait_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 sentconn_idx (
int
) – Index of connection to send totimeout (
int
) – Timeout in seconds for sending all bytes
- Returns:
Number of bytes sent
- Return type:
int
- property socket
Returns the underlying
socket
object
testplan.common.utils.sockets.tls module
- class testplan.common.utils.sockets.tls.DefaultTLSConfig(cacert: PathLike | str | None = None)[source]
Bases:
TLSConfig
This TLSConfig create a default SSLContext as defined in ssl lib
- Parameters:
cacert – Optional Root CA certificate
- get_context(purpose: Purpose) SSLContext [source]
The implementation of this function need to return a configured
SSLContext
, example implementations:DefaultTLSConfig
andSimpleTLSConfig
- Parameters:
purpose – Either host or client certificate
- Returns:
should return the configured SSLContext
- class testplan.common.utils.sockets.tls.SimpleTLSConfig(key: PathLike | str, cert: PathLike | str, cacert: PathLike | str | None)[source]
Bases:
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: Purpose) SSLContext [source]
The implementation of this function need to return a configured
SSLContext
, example implementations:DefaultTLSConfig
andSimpleTLSConfig
- Parameters:
purpose – Either host or client certificate
- Returns:
should return the configured SSLContext
- class testplan.common.utils.sockets.tls.TLSConfig[source]
Bases:
ABC
Defines the Protocol a TLSConfig need to have
- abstractmethod get_context(purpose: Purpose) SSLContext [source]
The implementation of this function need to return a configured
SSLContext
, example implementations:DefaultTLSConfig
andSimpleTLSConfig
- Parameters:
purpose – Either host or client certificate
- Returns:
should return the configured SSLContext
Module contents
Socket server client utils.