Client
TCP client for tensor transport.
This class provides a thin wrapper around Python's socket for sending
and receiving messages that follow the tannic tensor protocol:
[Header][Payload]
- Header is defined in
pytannic.header.Header. It includes: - magic number (
MAGIC) for validation - protocol version
- checksum (not validated yet)
- payload size in bytes
- Payload is application-dependent:
- Serialized tensor (see
pytannic.torch.tensor) - Serialized metadata (see
pytannic.torch.parameters) - Or other binary blobs
The client ensures full reception of both the header and payload.
Client
TCP client for communicating with a tannic tensor server.
Supports context-manager usage:
with Client("localhost", 9000) as client: ... client.send(b"hello") ... reply = client.receive()
Attributes
host : str
Server hostname or IP.
port : int
Server TCP port.
socket : socket.socket or None
Underlying socket object. Created in begin().
Source code in pytannic/client.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
__enter__()
Enable with Client(...) as client: usage.
Source code in pytannic/client.py
62 63 64 65 | |
__exit__(exc_type, exc_val, exc_tb)
Close the socket when leaving context manager scope.
Source code in pytannic/client.py
67 68 69 | |
begin()
Establish a TCP connection to the server.
Raises
OSError If the socket cannot connect to the given host/port.
Source code in pytannic/client.py
71 72 73 74 75 76 77 78 79 80 81 | |
close()
Close the socket connection if open.
Source code in pytannic/client.py
83 84 85 86 87 88 89 | |
receive()
Receive a complete tannic message (header + payload).
Returns
bytes The raw message received, including the header and payload.
Raises
ValueError If the received header has an invalid magic number. ConnectionError If the socket closes before the expected number of bytes is received.
Source code in pytannic/client.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
send(data)
Send raw bytes to the server.
Parameters
data : bytes A complete tannic message, including header and payload.
Raises
RuntimeError If the socket is not connected.
Source code in pytannic/client.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |