academy.socket¶
SimpleSocket
¶
Simple socket wrapper.
Configures a client connection using a 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 a context manager.
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
send
¶
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
¶
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
¶
Receive the next message from the socket.
Returns:
Raises:
-
SocketClosedError
–if the socket was closed.
-
OSError
–if an error occurred.
Source code in academy/socket.py
recv_string
¶
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
SimpleSocketServer
¶
SimpleSocketServer(
handler: Callable[[bytes], bytes | None],
*,
host: str = "0.0.0.0",
port: int | None = None,
timeout: float | None = 5
)
Simple asyncio TCP socket server.
Parameters:
-
handler
(Callable[[bytes], 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. -
timeout
(float | None
, default:5
) –Seconds to wait for the server to startup and shutdown.
Source code in academy/socket.py
serve_forever
async
¶
serve_forever(stop: Future[None]) -> None
Accept and handles connections forever.
Parameters:
-
stop
(Future[None]
) –An asyncio future that this method blocks on. Can be used to signal externally that the coroutine should exit.
Source code in academy/socket.py
start_server_thread
¶
Start the server in a new thread.
Source code in academy/socket.py
stop_server_thread
¶
Stop the server thread.
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.