gatenet.client package¶
Submodules¶
gatenet.client.base module¶
- class gatenet.client.base.BaseClient[source]¶
Bases:
ABCAbstract base class for network clients.
All client implementations must provide methods to send messages and close the connection.
Examples
Subclassing:
class MyClient(BaseClient): def send(self, message: str, **kwargs) -> str: return "response" def close(self): pass
Usage:
client = MyClient() response = client.send("hello") client.close()
- abstractmethod close()[source]¶
Close the client connection.
Example
>>> client = MyClient() >>> client.close()
This should release any resources and close the underlying socket or connection.
- abstractmethod send(message, **kwargs)[source]¶
Send a message to the server and return a response.
Example
>>> class MyClient(BaseClient): ... def send(self, message: str, **kwargs) -> str: ... return "response" ... def close(self): ... pass >>> client = MyClient() >>> response = client.send("hello")
- Returns:
The response received from the server.
- Return type:
str
gatenet.client.tcp module¶
- class gatenet.client.tcp.TCPClient(host, port, timeout=5.0)[source]¶
Bases:
BaseClientTCP client for connecting to a server, sending messages, and receiving responses.
Supports context manager usage for automatic connection management.
Examples
Basic usage:
from gatenet.client.tcp import TCPClient client = TCPClient(host="127.0.0.1", port=12345) client.connect() response = client.send("ping") client.close()
With context manager:
with TCPClient(host="127.0.0.1", port=12345) as client: response = client.send("ping")
- close()[source]¶
Close the client connection and release resources.
Example
>>> client = TCPClient(host="127.0.0.1", port=12345) >>> client.connect() >>> client.close()
gatenet.client.udp module¶
- class gatenet.client.udp.UDPClient(host, port, timeout=2.0)[source]¶
Bases:
BaseClientUDP client for sending messages to a server and receiving responses.
Supports context manager usage for automatic resource management.
Examples
Basic usage:
from gatenet.client.udp import UDPClient client = UDPClient(host="127.0.0.1", port=12345) response = client.send("ping") client.close()
With context manager:
with UDPClient(host="127.0.0.1", port=12345) as client: response = client.send("ping")
- close()[source]¶
Close the UDP client socket.
Example
>>> client = UDPClient(host="127.0.0.1", port=12345) >>> client.close()
- send(message, retries=3, buffsize=1024, **kwargs)[source]¶
Send a message to the server and receive the response.
Example
>>> client = UDPClient(host="127.0.0.1", port=12345) >>> response = client.send("ping")
- Parameters:
message (str) – The message to send to the server.
retries (int, optional) – Number of retries for receiving a response (default is 3).
buffsize (int, optional) – Buffer size for receiving the response (default is 1024).
**kwargs (dict) – Additional keyword arguments (ignored).
- Returns:
The response received from the server.
- Return type:
str
- Raises:
TimeoutError – If no response is received after the specified number of retries.
Module contents¶
- class gatenet.client.BaseClient[source]¶
Bases:
ABCAbstract base class for network clients.
All client implementations must provide methods to send messages and close the connection.
Examples
Subclassing:
class MyClient(BaseClient): def send(self, message: str, **kwargs) -> str: return "response" def close(self): pass
Usage:
client = MyClient() response = client.send("hello") client.close()
- abstractmethod close()[source]¶
Close the client connection.
Example
>>> client = MyClient() >>> client.close()
This should release any resources and close the underlying socket or connection.
- abstractmethod send(message, **kwargs)[source]¶
Send a message to the server and return a response.
Example
>>> class MyClient(BaseClient): ... def send(self, message: str, **kwargs) -> str: ... return "response" ... def close(self): ... pass >>> client = MyClient() >>> response = client.send("hello")
- Returns:
The response received from the server.
- Return type:
str
- class gatenet.client.TCPClient(host, port, timeout=5.0)[source]¶
Bases:
BaseClientTCP client for connecting to a server, sending messages, and receiving responses.
Supports context manager usage for automatic connection management.
Examples
Basic usage:
from gatenet.client.tcp import TCPClient client = TCPClient(host="127.0.0.1", port=12345) client.connect() response = client.send("ping") client.close()
With context manager:
with TCPClient(host="127.0.0.1", port=12345) as client: response = client.send("ping")
- close()[source]¶
Close the client connection and release resources.
Example
>>> client = TCPClient(host="127.0.0.1", port=12345) >>> client.connect() >>> client.close()
- class gatenet.client.UDPClient(host, port, timeout=2.0)[source]¶
Bases:
BaseClientUDP client for sending messages to a server and receiving responses.
Supports context manager usage for automatic resource management.
Examples
Basic usage:
from gatenet.client.udp import UDPClient client = UDPClient(host="127.0.0.1", port=12345) response = client.send("ping") client.close()
With context manager:
with UDPClient(host="127.0.0.1", port=12345) as client: response = client.send("ping")
- close()[source]¶
Close the UDP client socket.
Example
>>> client = UDPClient(host="127.0.0.1", port=12345) >>> client.close()
- send(message, retries=3, buffsize=1024, **kwargs)[source]¶
Send a message to the server and receive the response.
Example
>>> client = UDPClient(host="127.0.0.1", port=12345) >>> response = client.send("ping")
- Parameters:
message (str) – The message to send to the server.
retries (int, optional) – Number of retries for receiving a response (default is 3).
buffsize (int, optional) – Buffer size for receiving the response (default is 1024).
**kwargs (dict) – Additional keyword arguments (ignored).
- Returns:
The response received from the server.
- Return type:
str
- Raises:
TimeoutError – If no response is received after the specified number of retries.