<h1 align="center">
cushy-socket
</h1>
<p align="center">
<strong>A lightweight python socket library. You can create a TCP/UDP connection easily.</strong>
</p>
<p align="center">
<a target="_blank" href="">
<img src="https://img.shields.io/badge/License-Apache%202.0-blue.svg?label=license" />
</a>
<a target="_blank" href=''>
<img src="https://static.pepy.tech/personalized-badge/broadcast-service?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Downloads/Total"/>
</a>
<a target="_blank" href=''>
<img src="https://static.pepy.tech/personalized-badge/broadcast-service?period=month&units=international_system&left_color=grey&right_color=blue&left_text=Downloads/Week"/>
</a>
</p>
# Features
- send socket message easily
- listen socket message and support ballback
- support sending group messages to clients
- support decorator version listening
- listen topic message and callback
# Usage
```bash
pip install cushy-socket --upgrade
```
Here are some minimal example programs using the `cushy-socket`: a server that echoes all data that it receives back(servicing only one client), and a client using it.
- Now let's build a easy echo demo.The first example support IPv4 only.
```python
# echo tcp server program
import socket
from cushy_socket.tcp import CushyTCPServer
cushy_tcp_server = CushyTCPServer(host='localhost', port=7777)
cushy_tcp_server.run()
@cushy_tcp_server.on_connected()
def handle_on_connected(sock: socket.socket):
print(f"[server decorator callback] new client connected.")
print(sock)
@cushy_tcp_server.on_disconnected()
def handle_on_disconnected(sock: socket.socket):
print(f"[server decorator callback] a client disconnected.")
print(sock)
@cushy_tcp_server.on_message()
def handle_msg_from_client(msg: str, socket: socket.socket):
print(f"[server decorator callback] cushy_tcp_server rec msg: {msg}")
cushy_tcp_server.send("hello, I am server")
```
```python
# echo tcp client program
from cushy_socket.tcp import CushyTCPClient
cushy_tcp_client = CushyTCPClient(host='localhost', port=7777)
cushy_tcp_client.run()
@cushy_tcp_client.on_connected()
def handle_on_connected():
print(f"[client decorator callback] connect to server.")
@cushy_tcp_client.on_disconnected()
def handle_on_disconnected():
print(f"[client decorator callback] server disconnected.")
@cushy_tcp_client.on_message()
def handle_msg_from_server(msg: str):
print(f"[client decorator callback] cushy_tcp_client rec msg: {msg}")
cushy_tcp_client.send("hello, here is CSTCP client")
cushy_tcp_client.close()
```
# TODO
- [ ] support for more lifecycle callbacks
- [ ] optimize the handle of socket closing
- [ ] optimize syntax expressions
- [ ] add UDP server/client support
- [ ] provide more solutions
- [ ] provide more async supports
- [ ] provide more decorator support
- [ ] optimize unittest
- [ ] send and listen topic message
# Contribution
If you want to contribute to this project, you can submit pr or issue. I am glad to see more people involved and optimize it.
Raw data
{
"_id": null,
"home_page": "https://github.com/Undertone0809/cushy-socket",
"name": "cushy-socket",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "socket,tcp,udp,cushy-socket",
"author": "Zeeland",
"author_email": "zeeland@foxmail.com",
"download_url": "https://files.pythonhosted.org/packages/d3/dc/5a0719f6364f9c87a2eb45ec687069a0577834ec05bad0466a678af9c730/cushy-socket-1.2.2.tar.gz",
"platform": null,
"description": "<h1 align=\"center\">\r\n cushy-socket\r\n</h1>\r\n<p align=\"center\">\r\n <strong>A lightweight python socket library. You can create a TCP/UDP connection easily.</strong>\r\n</p>\r\n\r\n<p align=\"center\">\r\n <a target=\"_blank\" href=\"\">\r\n <img src=\"https://img.shields.io/badge/License-Apache%202.0-blue.svg?label=license\" />\r\n </a>\r\n <a target=\"_blank\" href=''>\r\n <img src=\"https://static.pepy.tech/personalized-badge/broadcast-service?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Downloads/Total\"/>\r\n </a>\r\n <a target=\"_blank\" href=''>\r\n <img src=\"https://static.pepy.tech/personalized-badge/broadcast-service?period=month&units=international_system&left_color=grey&right_color=blue&left_text=Downloads/Week\"/>\r\n </a>\r\n</p>\r\n\r\n\r\n# Features\r\n- send socket message easily\r\n- listen socket message and support ballback\r\n- support sending group messages to clients\r\n- support decorator version listening\r\n- listen topic message and callback\r\n\r\n\r\n# Usage\r\n\r\n```bash\r\npip install cushy-socket --upgrade \r\n```\r\n\r\nHere are some minimal example programs using the `cushy-socket`: a server that echoes all data that it receives back(servicing only one client), and a client using it.\r\n\r\n- Now let's build a easy echo demo.The first example support IPv4 only.\r\n\r\n```python\r\n# echo tcp server program\r\nimport socket\r\nfrom cushy_socket.tcp import CushyTCPServer\r\n\r\ncushy_tcp_server = CushyTCPServer(host='localhost', port=7777)\r\ncushy_tcp_server.run()\r\n\r\n\r\n@cushy_tcp_server.on_connected()\r\ndef handle_on_connected(sock: socket.socket):\r\n print(f\"[server decorator callback] new client connected.\")\r\n print(sock)\r\n\r\n\r\n@cushy_tcp_server.on_disconnected()\r\ndef handle_on_disconnected(sock: socket.socket):\r\n print(f\"[server decorator callback] a client disconnected.\")\r\n print(sock)\r\n\r\n\r\n@cushy_tcp_server.on_message()\r\ndef handle_msg_from_client(msg: str, socket: socket.socket):\r\n print(f\"[server decorator callback] cushy_tcp_server rec msg: {msg}\")\r\n cushy_tcp_server.send(\"hello, I am server\")\r\n\r\n```\r\n\r\n```python\r\n# echo tcp client program\r\nfrom cushy_socket.tcp import CushyTCPClient\r\n\r\ncushy_tcp_client = CushyTCPClient(host='localhost', port=7777)\r\ncushy_tcp_client.run()\r\n\r\n\r\n@cushy_tcp_client.on_connected()\r\ndef handle_on_connected():\r\n print(f\"[client decorator callback] connect to server.\")\r\n\r\n\r\n@cushy_tcp_client.on_disconnected()\r\ndef handle_on_disconnected():\r\n print(f\"[client decorator callback] server disconnected.\")\r\n\r\n\r\n@cushy_tcp_client.on_message()\r\ndef handle_msg_from_server(msg: str):\r\n print(f\"[client decorator callback] cushy_tcp_client rec msg: {msg}\")\r\n\r\n\r\ncushy_tcp_client.send(\"hello, here is CSTCP client\")\r\ncushy_tcp_client.close()\r\n\r\n```\r\n\r\n\r\n\r\n# TODO\r\n- [ ] support for more lifecycle callbacks\r\n- [ ] optimize the handle of socket closing\r\n- [ ] optimize syntax expressions\r\n- [ ] add UDP server/client support\r\n- [ ] provide more solutions\r\n- [ ] provide more async supports\r\n- [ ] provide more decorator support\r\n- [ ] optimize unittest\r\n- [ ] send and listen topic message\r\n\r\n# Contribution\r\nIf you want to contribute to this project, you can submit pr or issue. I am glad to see more people involved and optimize it.\r\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "A lightweight socket library",
"version": "1.2.2",
"split_keywords": [
"socket",
"tcp",
"udp",
"cushy-socket"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d3dc5a0719f6364f9c87a2eb45ec687069a0577834ec05bad0466a678af9c730",
"md5": "648342226c20af01e8e3c63b9007696b",
"sha256": "85de3658757bf18a87be727e2d9992e782757050d59bcd823d1965e832897b3f"
},
"downloads": -1,
"filename": "cushy-socket-1.2.2.tar.gz",
"has_sig": false,
"md5_digest": "648342226c20af01e8e3c63b9007696b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8868,
"upload_time": "2023-03-10T12:20:14",
"upload_time_iso_8601": "2023-03-10T12:20:14.383737Z",
"url": "https://files.pythonhosted.org/packages/d3/dc/5a0719f6364f9c87a2eb45ec687069a0577834ec05bad0466a678af9c730/cushy-socket-1.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-10 12:20:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "Undertone0809",
"github_project": "cushy-socket",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "cushy-socket"
}