Python API
Sandwich Python API.
This API provides a way to use Sandwich from Python.
It wraps Sandwich primitives into classes for convenience. It also provides the protobuf API for building Sandwich Contexts and Tunnels.
The following classes are defined
- Sandwich: general handle responsible for ffi using ctypes.CDLL.
- IO: abstract class to define an I/O interface.
To be able to use this API, the user has to define its own I/O interface.
See io.py
for more information.
Sandwich
Top-level Sandwich context library.
Source code in pysandwich/sandwich.py
TunnelConfigurationSerialized
Bases: Structure
The struct SandwichTunnelConfigurationSerialized
.
Source code in pysandwich/sandwich.py
TunnelContextConfigurationSerialized
Bases: Structure
The struct SandwichContextTunnelConfigurationSerialized
.
Source code in pysandwich/sandwich.py
sandwich()
Returns the global handler to _SandwichCLib. This function performs a lazy initialization of the Sandwich handler.
Source code in pysandwich/sandwich.py
Sandwich I/O API.
This API provides an I/O interface in Python, to use with Sandwich.
It wraps a struct SandwichIO
.
An I/O interface is an object requiring the following methods: * read(n: int) -> bytes * write(buf: bytes) -> int
All methods should either return the corresponding value (the read bytes
for read
and the amount of successfully written bytes for write
), or
raise an exception of type IOException
.
The user has to define a class extending IO
.
An example can be found in tunnel_test.py
.
IO
Bases: ABC
Abstraction of a struct SandwichIO
handle.
The C handle is built by the tunnel, using the methods from this class.
Source code in pysandwich/io.py
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
Settings
Bases: Structure
The struct SandwichIO
.
Source code in pysandwich/io.py
flush()
abstractmethod
read(n)
abstractmethod
Read function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
Amount of bytes to read. |
required |
Raises:
Type | Description |
---|---|
IOException
|
an error occured during reading |
Returns:
Type | Description |
---|---|
bytes
|
Bytes successfully read. |
Source code in pysandwich/io.py
write(buf)
abstractmethod
Write function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
buf |
bytes
|
Buffer to write. |
required |
Raises:
Type | Description |
---|---|
IOException
|
an error occured during writing |
Returns:
Type | Description |
---|---|
int
|
Amount of successfully written bytes. |
Source code in pysandwich/io.py
IOClosedException
IOException
Bases: SandwichException
Exception base class for I/O interface.
Errors are defined in the protobuf io.proto
, enum IOError
.
This exception handles the following cases:
* IOERROR_IN_PROGRESS
* IOERROR_WOULD_BLOCK
* IOERROR_REFUSED
* IOERROR_CLOSED
* IOERROR_INVALID
* IOERROR_UNKNOWN
Source code in pysandwich/io.py
ERROR_OK = SandwichIOProto.IOERROR_OK
class-attribute
instance-attribute
Map from the protobuf enum IOError
to error string and subclass exception.
IOInProgressException
IOInvalidException
IORefusedException
IOUnknownException
IOWouldBlockException
OwnedIO
Bases: Structure
The struct SandwichIO
.
Source code in pysandwich/io.py
TunnelIO
Bases: IO
Abstraction of a struct SandwichTunnelIO
handle.
The C handle is built by the tunnel, using the methods from this class and
SandwichIO
.
Source code in pysandwich/io.py
CTunnelIO
Bases: Structure
The struct SandwichTunnelIO
.
Source code in pysandwich/io.py
set_state(tunnel_state)
abstractmethod
Set the state of the tunnel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tunnel_state |
State
|
Current state of the tunnel. |
required |
It is guaranteed that the state of the tunnel will not change between two calls to set_state.
Source code in pysandwich/io.py
Sandwich Error API.
This API provides error types by inheriting the Exception
class.
Error codes come from the .proto
file.
All sandwich exceptions are based on SandwichException
.
This file defines the following exception families
SandwichGlobalException
: exceptions that can happen all across the library.HandshakeException
: exceptions happening during the handshake stage (fromTunnel.handshake()
).HandshakeError
: exceptions relating to errors encountered by the implementationRecordPlaneException
: exceptions happening inTunnel.read
orTunnel.write
.IOException
: exceptions happening in the I/O interface (seeio.py
).
All exceptions are based on the error codes defined by the following protobuf:
* errors.proto
: SandwichGlobalException
* tunnel.proto
: HandshakeException
and RecordPlaneException
* io.proto
: IOException
.
SandwichException
exposes a code
method to get the corresponding error code.
This error code is compatible with the C++ library.
HandshakeErrorStateException
HandshakeException
Bases: SandwichException
Exception base class for the handshake state. This exception handles the following cases: * HANDSHAKESTATE_IN_PROGRESS * HANDSHAKESTATE_WANT_READ * HANDSHAKESTATE_WANT_WRITE * HANDSHAKESTATE_ERROR
Source code in pysandwich/errors.py
ERROR_OK = SandwichTunnelProto.HANDSHAKESTATE_DONE
class-attribute
instance-attribute
Map from the protobuf enum 'HandshakeState" to error string.
HandshakeInProgressException
HandshakeWantReadException
HandshakeWantWriteException
RecordPlaneBeingShutdownException
RecordPlaneClosedException
RecordPlaneException
Bases: SandwichException
Exception base class for the record plane.
Errors are defined in the protobuf, enum RecordError
.
This exception handles the following cases:
* HANDSHAKESTATE_IN_PROGRESS
* HANDSHAKESTATE_WANT_READ
* HANDSHAKESTATE_WANT_WRITE
* HANDSHAKESTATE_ERROR
Source code in pysandwich/errors.py
ERROR_OK = SandwichTunnelProto.RECORDERROR_OK
class-attribute
instance-attribute
Map from the protobuf enum 'RecordError" to error string and subclass exception.