Flexi-Socket
Note: This project is under ongoing development, with additional features planned for future releases.
Overview
Flexi-Socket is a Python library for developing robust server and client socket applications. It supports TCP protocol
and is capable of handling connections, messages, and disconnections in both server and client modes. The library is
equipped with message classification and string message handling, suitable for a range of network programming needs.
Installation
pip install flexi-socket
General Setup
Importing the Library
from flexi_socket import FlexiSocket, Connection, Protocol, Mode
Creating a Socket
Create a socket instance for server or client:
socket = FlexiSocket(mode=Mode.SERVER, protocol=Protocol.TCP, port=8081, read_buffer_size=1024) # Server
# or
socket = FlexiSocket(mode=Mode.CLIENT, protocol=Protocol.TCP, host="0.0.0.0", port=8080) # Client
Event Handlers
Implement event handlers for operations like connect, message handling, and disconnect:
On Connect
@socket.on_connect()
async def on_connect(connection: Connection):
print(f"Connected to {connection}")
# Additional logic
On Disconnect
@socket.on_disconnect()
async def on_disconnect(connection: Connection):
print(f"Disconnected from {connection}")
# Additional logic
On Message
@socket.on_message()
async def on_message(connection: Connection, message: str):
print(f"Received message from {connection}: {message}")
# Additional logic
After Receive and Before Send
This can be used to implement start/end bytes, checksums, message cleanup or any other logic that needs to be
applied to the message but without cluster the on_message handler.
A future implementation will combine these two handlers into one using something like yield.
@socket.after_receive()
async def after_receive(connection: Connection, message: str):
print(f"Received message from {connection}: {message}")
# Additional logic
@socket.before_send()
async def before_send(connection: Connection, message: str):
print(f"Sending message to {connection}: {message}")
# Additional logic
Sending Messages
To send a message:
await socket.send("Your message here")
Starting the Socket
socket.start()
# or
await socket.start_async()
Using Message Classification
Message classification allows handling different message types based on first message received from a client. This is useful because a lot of protocols include a handshake message, which can be used to identify the type of client (very popular in alarm systems).
This is an attempt to make something similar to http routing, but for classic socket programming.
class ClientTypes(ClientClassifier):
client_types = ["001", "002", "003"]
def classify(self, first_message):
if first_message.startswith("!!"):
return ClientTypes.client_types[0]
elif first_message.startswith("##"):
return ClientTypes.client_types[1]
else:
return ClientClassifier.DEFAULT
Using the Classifier
@socket.on_message(ClientTypes.client_types[0])
async def handle_client_001(client: Connection, message: str):
if client.is_first_message_from_client:
print("First message from client")
print(f"Client {client} sent {message}")
await client.send("Hello from server! You are type 001")
Planned Features
- UDP Protocol Support: Future implementation to handle UDP connections.
- Binary Message Support: Upgrade to allow sending and receiving binary data, in addition to string messages.
Metadata
Release files for flexi-socket 0.0.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flexi-socket-0.0.10.tar.gz | 7.5 kB | Without provenance | Details for flexi-socket-0.0.10.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flexi_socket-0.0.10-py3-none-any.whl | Python 3 | none | any | Details for flexi_socket-0.0.10-py3-none-any.whl |
Total release size: 15.2 kB
Release files / flexi-socket-0.0.10.tar.gz
| Download URL | flexi-socket-0.0.10.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
132314a99ddc142424cc0c8ab85924dc2b8a9be76322670fdce5e1adf1da4051
|
|
BLAKE2b-256 checksum How to use checksums |
de40bc7acab346679039af6626a23ffd6de9f78a07cedcb341a35f4a217e807e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Release files / flexi_socket-0.0.10-py3-none-any.whl
| Download URL | flexi_socket-0.0.10-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6fd66180e368b3e04292c34cb80b7dc32033dd3127e42614753eed1200cdddf4
|
|
BLAKE2b-256 checksum How to use checksums |
b8433f2c816f11240ade2344fe0e450aa31119b01eb0ec539388ebc0e8ca457c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Security for flexi-socket 0.0.10
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.