simplesockets module
Server_Client
dataclass
This client contains a socket and an address, optional also a key. This class is used for Servers
disconnect(self)
closes the socket
recv(self)
function collects incoming data.
Returns:
Type | Description |
---|---|
Socket_Response |
returns received data as bytes |
send(self, data)
tries to send data to the Server
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes |
data that should be send |
required |
Exceptions:
Type | Description |
---|---|
ConnectionError |
if the sending failed |
Socket_Response
dataclass
This class contains information's about a response from a socket. It contains the response itself, a datetime object and optionally a Server_Client in the from_ variable, if it was received by a Server,default None.
equals(self, information)
checks if the response is equal to the given information
Parameters:
Name | Type | Description | Default |
---|---|---|---|
information |
Union[bytes, Tuple[bytes], Socket_Response] |
information which should be compared to the response |
required |
Returns:
Type | Description |
---|---|
bool |
Returns if the response and the information are equal |
Exceptions:
Type | Description |
---|---|
TypeError |
information isn't a Socket_Response object or bytes object or a tuple |
TCPClient
This class contains functions for connecting and keeping connections alive
Attributes:
Name | Type | Description |
---|---|---|
self.EVENT_EXCEPTION |
str |
Returned by |
self.EVENT_RECEIVED |
str |
Returned by |
self.EVENT_TIMEOUT |
str |
Returned by |
self.EVENT_DISCONNECT |
str |
Returned by |
self.EVENT_CONNECTED |
str |
Returned by |
self.event.new_data |
bool |
Is True if the Client received new data |
self.event.disconnected |
bool |
Is True if the Client disconnected |
self.event.is_connected |
bool |
Is True if the Client is connected to the Server |
self.event.connected |
bool |
Is True if the Client connected |
self.event.exception.occurred |
bool |
Is True if an exception got caught |
self.event.exception.list |
list |
contains all caught exceptions |
self.recved_data |
list |
contains all received data |
Address: tuple
property
readonly
Address of the Client, containing it's ip and port
autorecv(self)
function which activates the auto-receiving thread, automatically saving all incoming data
await_event(self, timeout=0, disable_on_functions=False)
waits till an event occurs
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout |
Optional[int] |
time till timeout in milliseconds |
0 |
disable_on_functions |
Optional[bool] |
If True, will remove every EVENT_CONNECTED and EVENT_DISCONNECT Event from the Event_System |
False |
Returns:
Type | Description |
---|---|
Union[Tuple[simplesockets._support_files.Events.Event, List[simplesockets.simple_sockets.Socket_Response]], Tuple[simplesockets._support_files.Events.Event, dict], Tuple[simplesockets._support_files.Events.Event]] |
returns event and its value(s) |
close(self)
Closes the socket
connect(self)
tries to connect to the Server
Returns:
Type | Description |
---|---|
bool |
returns a bool if the connecting was successful |
disconnect(self)
tries to disconnect from the Server
Returns:
Type | Description |
---|---|
bool |
returns true if the client disconnected without an exception |
reconnect(self)
tries to reconnect to the Server
Returns:
Type | Description |
---|---|
bool |
returns a bool if the connecting was successful |
recv_data(self)
function collects incoming data. If you want to collect all incoming data automatically, use Client.autorecv()
Returns:
Type | Description |
---|---|
Socket_Response |
returns received data as bytes |
return_exceptions(self, delete=True, reset_exceptions=True)
this function returns all collected exceptions. Key is the time and value the Exception
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delete |
Optional[bool] |
If the list which collected the exceptions should be cleared |
True |
reset_exceptions |
Optional[bool] |
If the exception occurred variable should be reset (set to False) |
True |
Returns:
Type | Description |
---|---|
dict |
returns a list of all collected exceptions |
return_recved_data(self, clear_event=True)
returns received data
Returns:
Type | Description |
---|---|
List[simplesockets.simple_sockets.Socket_Response] |
returns a list of the received data |
send_data(self, data)
tries to send data to the Server, returns True if it was succesful
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes |
data that should be send |
required |
Returns:
Type | Description |
---|---|
bool |
returns True if the sending was successful |
setup(self, target_ip, target_port=25567, recv_buffer=2048, on_connect=None, on_disconnect=None, on_receive=None)
function sets up the Client
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_ip |
str |
IP the Client should connect to |
required |
target_port |
Optional[int] |
PORT the Client should connect to |
25567 |
recv_buffer |
Optional[int] |
The receive buffer used for |
2048 |
on_connect |
Optional[Callable] |
Function that will be executed on connection, it takes not arguments |
None |
on_disconnect |
Optional[Callable] |
Function that will be executed on disconnection, it takes no arguments |
None |
on_receive |
Optional[Callable] |
Function that will be executed on receive, it takes the received data as an argument |
None |
TCPServer
This class contains functions for accepting connections and keeping connections alive
Attributes:
Name | Type | Description |
---|---|---|
self.EVENT_EXCEPTION |
str |
Returned by |
self.EVENT_RECEIVED |
str |
Returned by |
self.EVENT_TIMEOUT |
str |
Returned by |
self.event.new_data |
bool |
Is True if the Client received new data |
self.event.exception.occurred |
bool |
Is True if an exception got caught |
self.event.exception.list |
list |
contains all caught exceptions |
self.recved_data |
list |
contains all received data |
self.socket |
socket.socket |
is the Server Socket |
self.clients |
dict |
contains the address as the key and the client thread and socket as a list as the values |
killed: bool
property
readonly
Returns: returns if the accepting thread got killed or not
__init__(self, max_connections=None)
special
Initializes the Server
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_connections |
Optional[int] |
how many Clients can connect to the Server |
None |
await_event(self, timeout=0)
waits till an event occurs
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout |
Optional[int] |
time till timeout in milliseconds. Zero means no timeout. |
0 |
Returns:
Type | Description |
---|---|
Union[Tuple[simplesockets._support_files.Events.Event, dict], Tuple[simplesockets._support_files.Events.Event, List[simplesockets.simple_sockets.Socket_Response]], Tuple[simplesockets._support_files.Events.Event]] |
Union[Tuple[str, list], Tuple[str, None]]: returns event and its value(s) |
close(self)
Closes the socket
disconnect(self, address)
disconnects a socket from the Server
Parameters:
Name | Type | Description | Default |
---|---|---|---|
address |
tuple |
the address of the client which you want to disconnect |
required |
exit_accept(self)
stops and kills the accepting thread
recv_data(self, client)
function collects incoming data
Returns:
Type | Description |
---|---|
Socket_Response |
returns received data as Socket_Response |
restart(self)
function tries to restart the accepting thread
return_exceptions(self, delete=True, reset_exception=True)
Returns the collected exceptions as a dict. Key is the time and value the Exception
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delete |
Optional[bool] |
If the list which collected the exceptions should be cleared |
True |
reset_exception |
Optional[bool] |
If the exception occurred variable should be reset (set to False) |
True |
Returns:
Type | Description |
---|---|
dict |
returns a list of all collected exceptions |
return_recved_data(self)
Returns received data. They are returned as Socket_Response objects
Returns:
Type | Description |
---|---|
List[simplesockets.simple_sockets.Socket_Response] |
returns a list of Socket_Response objects |
send_data(self, data, client)
function for sending data to a client,
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[bytes, simplesockets.simple_sockets.Socket_Response] |
data which should be send |
required |
client |
Server_Client |
the client to which the data should be send to |
required |
Returns:
Type | Description |
---|---|
bool |
returns True if the operation was successful without an exception |
Exceptions:
Type | Description |
---|---|
TypeError |
if data is a Socket_Response object and the type of data.response isn't bytes or tuple of bytes or a string |
setup(self, ip='127.0.0.1', port=25567, listen=5, recv_buffer=2048, handle_client=None, on_connect=None, on_disconnect=None, on_receive=None)
function prepares the Server
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ip |
Optional[str] |
IP of the Server |
'127.0.0.1' |
port |
Optional[int] |
PORT the Server should listen on |
25567 |
listen |
Optional[int] |
parameter for |
5 |
recv_buffer |
Optional[int] |
the receive buffer used for |
2048 |
handle_client |
Optional[Callable] |
the function for handling the Clients, should be left as None |
None |
on_connect |
Optional[Callable] |
function that will be executed on connection, it takes the address(tuple) as an argument |
None |
on_disconnect |
Optional[Callable] |
function that will be executed on disconnection, it takes the address(tuple) as an argument |
None |
on_receive |
Optional[Callable] |
function that will be executed on receive, it takes the clientsocket, address, received data as an argument |
None |
start(self)
starts the accepting thread
Exceptions:
Type | Description |
---|---|
SetupError |
If |
stop(self)
stops the accepting thread