Wrapper around built-in python sockets library
Project description
Socket Server Tools
Use sockets easily
Quickstart
server.py:
import network
class TransferProtocol(network.Protocol):
@classmethod
def encode(cls, data):
# This function gets data that the program wants to send and encodes it to bytearray or bytes to send to socket
return data
@classmethod
def decode(cls, data : bytearray):
# This function is called every tome some data is recieved
# returns CONTINUE if data should be continued to be recieved and data param is continued to be built up
# If the function returns data: data param is reset and resu't is returned
return data
class ClientHandler(network.ClientHandler):
protocol = TransferProtocol # This protocol only allows sending and recieving binary data
def __init__(self, conn, addr, server):
# This function is called it the main thread when a client connects
# conn - socket object of the client connection
# addr - client address
# server - Server which created the client hander
# You sould call the original init for the handler to work
super().__init__(conn, addr, server)
def serve():
# This function is called in a separate thread after the init
self.transport.run_background() # Activates events
self.event.register(self.event.PACKET_RECIEVED, self.on_recieved)
def on_recieved(self, data):
self.transport.send(data)
echo_server = network.Server(host="0.0.0.0", port=8085, handler=ClientHandler)
echo_server.run()
client.py:
import network
class TransferProtocol(same as in server.py)
class Client(network.Client):
protocol = TransferProtocol
# Same as ClientHandler
def serve():
...
self.transport.send(b"Echo test")
def on_recieved(self, data):
print(data)
client = Client.conect("127.0.0.1", 8085)
client.run()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sockettools-0.1.0.tar.gz
(16.2 kB
view hashes)
Built Distribution
Close
Hashes for sockettools-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc35de59bab8f8f076ab4178b23c3d666082233f6a793de65f84d9347835eee9 |
|
MD5 | 68171e3d3eeb5e79ec47077177bf4994 |
|
BLAKE2b-256 | b1378a5915d79ea4b162f1cc6ba491a82a8413602c8e0e4631b3befa4a4c5ae7 |