Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Note: This project is under ongoing development, with additional features planned for future releases.
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.
pip install flexi-socket
from flexi_socket import FlexiSocket, Connection, Protocol, Mode
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
Implement event handlers for operations like connect, message handling, and disconnect:
@socket.on_connect()
async def on_connect(connection: Connection):
print(f"Connected to {connection}")
# Additional logic
@socket.on_disconnect()
async def on_disconnect(connection: Connection):
print(f"Disconnected from {connection}")
# Additional logic
@socket.on_message()
async def on_message(connection: Connection, message: str):
print(f"Received message from {connection}: {message}")
# Additional logic
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
To send a message:
await socket.send("Your message here")
socket.start()
# or
await socket.start_async()
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
@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")
FAQs
Socket wrapper for asyncio
We found that flexi-socket demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.