academy.socket¶
SimpleSocket
¶
SimpleSocket(
reader: StreamReader,
writer: StreamWriter,
*,
timeout: float | None = None
)
Simple socket wrapper.
Configures a client connection using a non-blocking TCP socket over IPv4. The send and recv methods handle byte encoding, message delimiters, and partial message buffering.
Note
This class can be used as an async context manager.
Parameters:
-
reader
(StreamReader
) –Socket reader interface.
-
writer
(StreamWriter
) –Socket writer interface.
-
timeout
(float | None
, default:None
) –Optional timeout for socket operations.
Source code in academy/socket.py
connect
async
classmethod
¶
Establish a new TCP connection.
Parameters:
-
host
(str
) –Host address to connect to.
-
port
(int
) –Port to connect to.
-
timeout
(float | None
, default:None
) –Connection establish timeout.
Raises:
-
SocketOpenError
–If creating the socket fails. The
__cause__
of the exception will be set to the underlyingOSError
.
Source code in academy/socket.py
close
async
¶
close(shutdown: bool = True) -> None
Close the socket.
Parameters:
-
shutdown
(bool
, default:True
) –Write EOF to the socket.
Source code in academy/socket.py
send
async
¶
send(message: bytes) -> None
Send bytes to the socket.
Note
This is a noop if the message is empty.
Parameters:
-
message
(bytes
) –Message to send.
Raises:
-
SocketClosedError
–If the socket was closed.
-
OSError
–If an error occurred.
Source code in academy/socket.py
send_string
async
¶
send_string(message: str) -> None
Send a string to the socket.
Strings are encoded with UTF-8.
Parameters:
-
message
(str
) –Message to send.
Raises:
-
SocketClosedError
–if the socket was closed.
-
OSError
–if an error occurred.
Source code in academy/socket.py
recv
async
¶
recv() -> bytes
Receive the next message from the socket.
Returns:
-
bytes
–Bytes containing the message.
Raises:
-
SocketClosedError
–if the socket was closed.
-
OSError
–if an error occurred.
Source code in academy/socket.py
recv_string
async
¶
recv_string() -> str
Receive the next message from the socket.
Returns:
-
str
–Message decoded as a UTF-8 string.
Raises:
-
SocketClosedError
–if the socket was closed.
-
OSError
–if an error occurred.
Source code in academy/socket.py
SocketPool
¶
Simple socket pool.
Source code in academy/socket.py
close
async
¶
get_socket
async
¶
get_socket(address: str) -> SimpleSocket
Get or create a socket for a given address.
Source code in academy/socket.py
send
async
¶
Send a message to a given address.
Source code in academy/socket.py
SimpleSocketServer
¶
SimpleSocketServer(
handler: Callable[[bytes], Awaitable[bytes | None]],
*,
host: str = "0.0.0.0",
port: int | None = None
)
Simple asyncio TCP socket server.
Parameters:
-
handler
(Callable[[bytes], Awaitable[bytes | None]]
) –Callback that handles a message and returns the response string. The handler is called synchronously within the client handler so it should not perform any heavy/blocking operations.
-
host
(str
, default:'0.0.0.0'
) –Host to bind to.
-
port
(int | None
, default:None
) –Port to bind to. If
None
, a random port is bound to.
Source code in academy/socket.py
serve
async
¶
serve() -> AsyncGenerator[Self]
Serve in a context manager.
Source code in academy/socket.py
address_by_interface
¶
Get the IP address of the given interface.
Source: https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python#24196955
Parameters:
-
ifname
(str
) –Name of the interface whose address is to be returned.
Source code in academy/socket.py
open_port
¶
open_port() -> int
Return open port.
Source: https://stackoverflow.com/questions/2838244
Source code in academy/socket.py
wait_connection
¶
wait_connection(
host: str,
port: int,
*,
sleep: float = 0.01,
timeout: float | None = None
) -> None
Wait for a socket connection to be established.
Repeatedly tries to open and close a socket connection to host:port
.
If successful, the function returns. If unsuccessful before the timeout,
a TimeoutError
is raised. The function will sleep for sleep
seconds
in between successive connection attempts.
Parameters:
-
host
(str
) –Host address to connect to.
-
port
(int
) –Host port to connect to.
-
sleep
(float
, default:0.01
) –Seconds to sleep after unsuccessful connections.
-
timeout
(float | None
, default:None
) –Maximum number of seconds to wait for successful connections.