# 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.
[View on PyPI](https://pypi.org/project/flexi-socket/)
## Installation
```shell
pip install flexi-socket
```
## General Setup
### Importing the Library
```python
from flexi_socket import FlexiSocket, Connection, Protocol, Mode
```
### Creating a Socket
Create a socket instance for server or client:
```python
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
```python
@socket.on_connect()
async def on_connect(connection: Connection):
print(f"Connected to {connection}")
# Additional logic
```
#### On Disconnect
```python
@socket.on_disconnect()
async def on_disconnect(connection: Connection):
print(f"Disconnected from {connection}")
# Additional logic
```
#### On Message
```python
@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.
```python
@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:
```python
await socket.send("Your message here")
```
### Starting the Socket
```python
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.
```python
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
```python
@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.
Raw data
{
"_id": null,
"home_page": "https://github.com/panos-stavrianos/flexi-socket",
"name": "flexi-socket",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "flexi_socket,socket,asyncio,async,tcp,udp,socket wrapper",
"author": "Panos Stavrianos",
"author_email": "panos@orbitsystems.gr",
"download_url": "https://files.pythonhosted.org/packages/78/89/4bcf7c946af520762ca82f67aef8d13519ae7bc3285450b912064fb55ccd/flexi-socket-0.0.8.tar.gz",
"platform": null,
"description": "# Flexi-Socket\n\n**Note:** This project is under ongoing development, with additional features planned for future releases.\n\n## Overview\n\n`Flexi-Socket` is a Python library for developing robust server and client socket applications. It supports TCP protocol\nand is capable of handling connections, messages, and disconnections in both server and client modes. The library is\nequipped with message classification and string message handling, suitable for a range of network programming needs.\n\n[View on PyPI](https://pypi.org/project/flexi-socket/)\n\n## Installation\n\n```shell\npip install flexi-socket\n```\n\n## General Setup\n\n### Importing the Library\n\n```python\nfrom flexi_socket import FlexiSocket, Connection, Protocol, Mode\n```\n\n### Creating a Socket\n\nCreate a socket instance for server or client:\n\n```python\nsocket = FlexiSocket(mode=Mode.SERVER, protocol=Protocol.TCP, port=8081, read_buffer_size=1024) # Server\n# or\nsocket = FlexiSocket(mode=Mode.CLIENT, protocol=Protocol.TCP, host=\"0.0.0.0\", port=8080) # Client\n```\n\n### Event Handlers\n\nImplement event handlers for operations like connect, message handling, and disconnect:\n\n#### On Connect\n\n```python\n@socket.on_connect()\nasync def on_connect(connection: Connection):\n print(f\"Connected to {connection}\")\n # Additional logic\n```\n\n#### On Disconnect\n\n```python\n@socket.on_disconnect()\nasync def on_disconnect(connection: Connection):\n print(f\"Disconnected from {connection}\")\n # Additional logic\n```\n\n#### On Message\n\n```python\n@socket.on_message()\nasync def on_message(connection: Connection, message: str):\n print(f\"Received message from {connection}: {message}\")\n # Additional logic\n```\n\n#### After Receive and Before Send\n\nThis can be used to implement start/end bytes, checksums, message cleanup or any other logic that needs to be\napplied to the message but without cluster the `on_message` handler. \nA future implementation will combine these two handlers into one using something like yield.\n\n```python\n@socket.after_receive()\nasync def after_receive(connection: Connection, message: str):\n print(f\"Received message from {connection}: {message}\")\n # Additional logic\n\n\n@socket.before_send()\nasync def before_send(connection: Connection, message: str):\n print(f\"Sending message to {connection}: {message}\")\n # Additional logic\n```\n\n### Sending Messages\n\nTo send a message:\n\n```python\nawait socket.send(\"Your message here\")\n```\n\n### Starting the Socket\n\n```python\nsocket.start()\n# or \nawait socket.start_async()\n```\n\n## Using Message Classification\n\nMessage classification allows handling different message types based on first message received from a client.\nThis is useful because a lot of protocols include a handshake message, which can be used to identify the type of client\n(very popular in alarm systems).\n\n> This is an attempt to make something similar to http routing, but for classic socket programming.\n\n```python\nclass ClientTypes(ClientClassifier):\n client_types = [\"001\", \"002\", \"003\"]\n\n def classify(self, first_message):\n if first_message.startswith(\"!!\"):\n return ClientTypes.client_types[0]\n elif first_message.startswith(\"##\"):\n return ClientTypes.client_types[1]\n else:\n return ClientClassifier.DEFAULT\n```\n\n### Using the Classifier\n\n```python\n@socket.on_message(ClientTypes.client_types[0])\nasync def handle_client_001(client: Connection, message: str):\n if client.is_first_message_from_client:\n print(\"First message from client\")\n print(f\"Client {client} sent {message}\")\n await client.send(\"Hello from server! You are type 001\")\n```\n\n## Planned Features\n\n- **UDP Protocol Support:** Future implementation to handle UDP connections.\n- **Binary Message Support:** Upgrade to allow sending and receiving binary data, in addition to string messages.\n",
"bugtrack_url": null,
"license": "MIT license",
"summary": "Socket wrapper for asyncio",
"version": "0.0.8",
"project_urls": {
"Homepage": "https://github.com/panos-stavrianos/flexi-socket"
},
"split_keywords": [
"flexi_socket",
"socket",
"asyncio",
"async",
"tcp",
"udp",
"socket wrapper"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e287a986aaad207c196d82798d145a39609c82d50defbbe4d3e33073f83ca12b",
"md5": "cb733432a15fee2f1094870327ec78fe",
"sha256": "804ec3af6b947fd97834980058c60680318d795d80c1203c041553a1650a97ad"
},
"downloads": -1,
"filename": "flexi_socket-0.0.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cb733432a15fee2f1094870327ec78fe",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 7512,
"upload_time": "2023-12-23T17:40:06",
"upload_time_iso_8601": "2023-12-23T17:40:06.197520Z",
"url": "https://files.pythonhosted.org/packages/e2/87/a986aaad207c196d82798d145a39609c82d50defbbe4d3e33073f83ca12b/flexi_socket-0.0.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "78894bcf7c946af520762ca82f67aef8d13519ae7bc3285450b912064fb55ccd",
"md5": "93fcaf0974360dcc9ffb86f1ba7f4cd1",
"sha256": "c82cb4bb36ed8f5068705a144c16b3f57564f66a455b89f8a57d8358372dd7ae"
},
"downloads": -1,
"filename": "flexi-socket-0.0.8.tar.gz",
"has_sig": false,
"md5_digest": "93fcaf0974360dcc9ffb86f1ba7f4cd1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 7402,
"upload_time": "2023-12-23T17:40:07",
"upload_time_iso_8601": "2023-12-23T17:40:07.724971Z",
"url": "https://files.pythonhosted.org/packages/78/89/4bcf7c946af520762ca82f67aef8d13519ae7bc3285450b912064fb55ccd/flexi-socket-0.0.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-23 17:40:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "panos-stavrianos",
"github_project": "flexi-socket",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "flexi-socket"
}