gsocket


Namegsocket JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/Leviathangk/gsocket
Summary一个 socket 模块
upload_time2022-12-06 08:35:23
maintainer
docs_urlNone
author郭一会儿
requires_python
licenseMIT Licence
keywords socket
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # gsocket

一个仅 Python 使用的基于 socket 协议的模块

- SocketServer:连接服务端
- SocketClient:连接客户端
- SocketReceiver:自定义 on_open 等

注意:

- SocketServer、SocketClient、SocketReceiver 自带 send、sendall、sendfile 方法
- 本模块未匹配 websocket 协议,所有通信基于 struct 防止粘包,所以不适用

# 安装
```
pip install gsocket
```

# SocketServer

```
from gsocket import SocketServer

SocketServer().start_server()
```

# SocketClient

推荐使用 with 上下文,不然需要手动关闭

```
from gsocket import SocketClient

with SocketClient() as s:
    s.send(msg="你好")
```

# SocketReceiver

SocketServer、SocketClient 都具备此参数,可以自由定义其接收方法
下面以 SocketServer 示例

```
from typing import Generator
from gsocket import SocketServer, SocketReceiver


class MySocketReceiver(SocketReceiver):
    def on_open(self):
        pass

    def on_message(self, msg: bytes):
        pass

    def on_file(self, filename: str, file_iterator: Generator):
        pass

    def on_close(self):
        pass


SocketServer(receiver=MySocketReceiver).start_server()
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Leviathangk/gsocket",
    "name": "gsocket",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "socket",
    "author": "\u90ed\u4e00\u4f1a\u513f",
    "author_email": "1015295213@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/5e/1c/69531c5256b0b542a0e11ac66ba3e2eac040277f3086f9f9567f89ab34dd/gsocket-0.0.2.tar.gz",
    "platform": "any",
    "description": "# gsocket\r\n\r\n\u4e00\u4e2a\u4ec5 Python \u4f7f\u7528\u7684\u57fa\u4e8e socket \u534f\u8bae\u7684\u6a21\u5757\r\n\r\n- SocketServer\uff1a\u8fde\u63a5\u670d\u52a1\u7aef\r\n- SocketClient\uff1a\u8fde\u63a5\u5ba2\u6237\u7aef\r\n- SocketReceiver\uff1a\u81ea\u5b9a\u4e49 on_open \u7b49\r\n\r\n\u6ce8\u610f\uff1a\r\n\r\n- SocketServer\u3001SocketClient\u3001SocketReceiver \u81ea\u5e26 send\u3001sendall\u3001sendfile \u65b9\u6cd5\r\n- \u672c\u6a21\u5757\u672a\u5339\u914d websocket \u534f\u8bae\uff0c\u6240\u6709\u901a\u4fe1\u57fa\u4e8e struct \u9632\u6b62\u7c98\u5305\uff0c\u6240\u4ee5\u4e0d\u9002\u7528\r\n\r\n# \u5b89\u88c5\r\n```\r\npip install gsocket\r\n```\r\n\r\n# SocketServer\r\n\r\n```\r\nfrom gsocket import SocketServer\r\n\r\nSocketServer().start_server()\r\n```\r\n\r\n# SocketClient\r\n\r\n\u63a8\u8350\u4f7f\u7528 with \u4e0a\u4e0b\u6587\uff0c\u4e0d\u7136\u9700\u8981\u624b\u52a8\u5173\u95ed\r\n\r\n```\r\nfrom gsocket import SocketClient\r\n\r\nwith SocketClient() as s:\r\n    s.send(msg=\"\u4f60\u597d\")\r\n```\r\n\r\n# SocketReceiver\r\n\r\nSocketServer\u3001SocketClient \u90fd\u5177\u5907\u6b64\u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u7531\u5b9a\u4e49\u5176\u63a5\u6536\u65b9\u6cd5\r\n\u4e0b\u9762\u4ee5 SocketServer \u793a\u4f8b\r\n\r\n```\r\nfrom typing import Generator\r\nfrom gsocket import SocketServer, SocketReceiver\r\n\r\n\r\nclass MySocketReceiver(SocketReceiver):\r\n    def on_open(self):\r\n        pass\r\n\r\n    def on_message(self, msg: bytes):\r\n        pass\r\n\r\n    def on_file(self, filename: str, file_iterator: Generator):\r\n        pass\r\n\r\n    def on_close(self):\r\n        pass\r\n\r\n\r\nSocketServer(receiver=MySocketReceiver).start_server()\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT Licence",
    "summary": "\u4e00\u4e2a socket \u6a21\u5757",
    "version": "0.0.2",
    "split_keywords": [
        "socket"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "3bb5b297d93ede86d1be253dc165e820",
                "sha256": "ace69800f0b0e1bc801f5faf3a7554dd0acb5bf15ab690e7c9e8aa45388dca8f"
            },
            "downloads": -1,
            "filename": "gsocket-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "3bb5b297d93ede86d1be253dc165e820",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6457,
            "upload_time": "2022-12-06T08:35:23",
            "upload_time_iso_8601": "2022-12-06T08:35:23.319107Z",
            "url": "https://files.pythonhosted.org/packages/5e/1c/69531c5256b0b542a0e11ac66ba3e2eac040277f3086f9f9567f89ab34dd/gsocket-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-06 08:35:23",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Leviathangk",
    "github_project": "gsocket",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "gsocket"
}
        
Elapsed time: 0.01881s