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: 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:
- construct it
- connect
- send and/or receive
- close
-
address
¶ Returns the host and port information of 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
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
- timeout (
-
close_connection
(conn_idx)[source]¶ Unregister, close and remove connection with given connection index
Parameters: conn_idx ( int
) – Connection index of connection to be removedReturns: 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
- size (
-
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
- msg (
-
socket
¶ Returns the underlying
socket
object
- host (
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
andSimpleTLSConfig
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
andSimpleTLSConfig
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
andSimpleTLSConfig
Parameters: purpose – Either host or client certificate Returns: should return the configured SSLContext
-
Module contents¶
Socket server client utils.