tlslp.protocol
Shared protocol helpers for the TLS client and server.
The challenge protocol is newline-delimited UTF-8:
Every outbound message is a
strthat is UTF-8 encoded and ends with"\n".Every inbound message is read until a trailing newline is seen, then decoded as UTF-8 and returned without the newline.
Multiline payloads are intentionally unsupported: a peer must send exactly one logical command per line.
This module raises:
- ProtocolError when the peer violates message framing/encoding rules
(non-bytes from socket, invalid UTF-8, line too long, etc.).
- TransportError for network/TLS failures (timeouts, disconnects, OS errors).
Functions
|
Receive one newline-delimited UTF-8 message. |
|
Send one newline-delimited UTF-8 message. |
Exceptions
Peer violated the newline-delimited UTF-8 protocol. |
|
Network/TLS error while sending/receiving. |
- exception tlslp.protocol.ProtocolError
Bases:
ValueErrorPeer violated the newline-delimited UTF-8 protocol.
- exception tlslp.protocol.TransportError
Bases:
RuntimeErrorNetwork/TLS error while sending/receiving.
- tlslp.protocol.receive_message(secure_sock: socket) str
Receive one newline-delimited UTF-8 message.
Reads from the socket until a newline byte is observed, then decodes as UTF-8 and returns the string without the trailing newline.
- Parameters:
secure_sock (socket.socket) – Connected socket (plain or TLS-wrapped) to read from.
- Returns:
The decoded message with the trailing newline removed.
- Return type:
(str)
- Raises:
ProtocolError – If the peer sends non-bytes, invalid UTF-8, or a line that exceeds
MAX_LINE_LENGTH.TransportError – If the peer closes the connection, a timeout occurs, or another network/TLS/OS error happens.
Examples
>>> import socket >>> a, b = socket.socketpair() >>> try: ... a.sendall(b"HELLOBACK\n") ... receive_message(b) ... finally: ... a.close(); b.close() 'HELLOBACK'
- tlslp.protocol.send_message(string_to_send: str, secure_sock: socket) None
Send one newline-delimited UTF-8 message.
The function guarantees: - the payload is a
str(otherwiseProtocolError), - it ends with a newline (adds one if missing), - it can be encoded as UTF-8, - it is fully transmitted usingsendall.- Parameters:
string_to_send (str) – Message to send (with or without a trailing
"\n").secure_sock (socket.socket) – Connected socket (plain or TLS-wrapped) to send on.
- Raises:
ProtocolError – If the payload is not a
stror cannot be UTF-8 encoded.TransportError – If the underlying socket/TLS layer fails while sending.
Examples
>>> import socket >>> a, b = socket.socketpair() >>> try: ... send_message("HELLO", a) ... b.recv(16) ... finally: ... a.close(); b.close() b'HELLO\n'