{"id":23048823,"url":"https://github.com/francis-clairicia/easynetwork","last_synced_at":"2025-06-23T01:40:47.818Z","repository":{"id":64985618,"uuid":"580345567","full_name":"francis-clairicia/EasyNetwork","owner":"francis-clairicia","description":"The easiest way to use sockets in Python","archived":false,"fork":false,"pushed_at":"2024-04-13T14:01:05.000Z","size":2742,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-14T00:46:55.280Z","etag":null,"topics":["network","network-programming","python","python-async","python-asynchronous","python-asynchronous-server","python-asyncio","python3"],"latest_commit_sha":null,"homepage":"https://easynetwork.readthedocs.io/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/francis-clairicia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-12-20T10:33:11.000Z","updated_at":"2024-04-18T08:26:44.215Z","dependencies_parsed_at":"2023-11-18T12:25:21.406Z","dependency_job_id":"387e30ad-1e96-4398-aa11-ed412ea56568","html_url":"https://github.com/francis-clairicia/EasyNetwork","commit_stats":{"total_commits":99,"total_committers":3,"mean_commits":33.0,"dds":0.04040404040404044,"last_synced_commit":"cb842cdff305e1f367c15a9779f7d5ff4d3e8107"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francis-clairicia%2FEasyNetwork","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francis-clairicia%2FEasyNetwork/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francis-clairicia%2FEasyNetwork/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/francis-clairicia%2FEasyNetwork/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/francis-clairicia","download_url":"https://codeload.github.com/francis-clairicia/EasyNetwork/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232154295,"owners_count":18480245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["network","network-programming","python","python-async","python-asynchronous","python-asynchronous-server","python-asyncio","python3"],"created_at":"2024-12-15T22:56:31.179Z","updated_at":"2025-01-02T04:55:36.961Z","avatar_url":"https://github.com/francis-clairicia.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EasyNetwork\n\nThe easiest way to use sockets in Python!\n\n[![PyPI](https://img.shields.io/pypi/v/easynetwork)](https://pypi.org/project/easynetwork/)\n[![PyPI - License](https://img.shields.io/pypi/l/easynetwork)](https://github.com/francis-clairicia/EasyNetwork/blob/main/LICENSE)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/easynetwork)\n\n[![Test](https://github.com/francis-clairicia/EasyNetwork/actions/workflows/test.yml/badge.svg)](https://github.com/francis-clairicia/EasyNetwork/actions/workflows/test.yml)\n[![Documentation Status](https://readthedocs.org/projects/easynetwork/badge/?version=latest)](https://easynetwork.readthedocs.io/en/latest/?badge=latest)\n[![Codecov](https://img.shields.io/codecov/c/github/francis-clairicia/EasyNetwork)](https://codecov.io/gh/francis-clairicia/EasyNetwork)\n[![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/francis-clairicia/EasyNetwork)](https://www.codefactor.io/repository/github/francis-clairicia/easynetwork)\n\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/francis-clairicia/EasyNetwork/main.svg)](https://results.pre-commit.ci/latest/github/francis-clairicia/EasyNetwork/main)\n\n[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat\u0026labelColor=ef8336)](https://pycqa.github.io/isort/)\n[![security: bandit](https://img.shields.io/badge/security-bandit-yellow.svg)](https://github.com/PyCQA/bandit)\n\n[![Hatch project](https://img.shields.io/badge/%F0%9F%A5%9A-Hatch-4051b5.svg)](https://github.com/pypa/hatch)\n[![pdm-managed](https://img.shields.io/badge/pdm-managed-blueviolet)](https://pdm.fming.dev)\n\n## Installation\n### From PyPI repository\n```sh\npip install --user easynetwork\n```\n\n### From source\n```sh\ngit clone https://github.com/francis-clairicia/EasyNetwork.git\ncd EasyNetwork\npip install --user .\n```\n\n## Overview\nEasyNetwork completely encapsulates the socket handling, providing you with a higher level interface\nthat allows an application/software to completely handle the logic part with Python objects,\nwithout worrying about how to process, send or receive data over the network.\n\nThe communication protocol can be whatever you want, be it JSON, Pickle, ASCII, structure, base64 encoded,\ncompressed, or any other format that is not part of the standard library.\nYou choose the data format and the library takes care of the rest.\n\nThis project is especially useful for simple **message** exchange between clients and servers.\n\nWorks with TCP and UDP.\n\nInterested ? Here is the documentation : https://easynetwork.readthedocs.io/\n\n## Usage\n### TCP Echo server with JSON data\n```py\nimport asyncio\nimport logging\nfrom collections.abc import AsyncGenerator\nfrom typing import Any, TypeAlias\n\nfrom easynetwork.protocol import StreamProtocol\nfrom easynetwork.serializers import JSONSerializer\nfrom easynetwork.servers import AsyncTCPNetworkServer\nfrom easynetwork.servers.handlers import AsyncStreamClient, AsyncStreamRequestHandler\n\n# These TypeAliases are there to help you understand\n# where requests and responses are used in the code\nRequestType: TypeAlias = Any\nResponseType: TypeAlias = Any\n\n\nclass JSONProtocol(StreamProtocol[ResponseType, RequestType]):\n    def __init__(self) -\u003e None:\n        super().__init__(JSONSerializer())\n\n\nclass EchoRequestHandler(AsyncStreamRequestHandler[RequestType, ResponseType]):\n    def __init__(self) -\u003e None:\n        self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)\n\n    async def handle(\n        self,\n        client: AsyncStreamClient[ResponseType],\n    ) -\u003e AsyncGenerator[None, RequestType]:\n        # A JSON request has been sent by this client\n        data: Any = yield\n\n        self.logger.info(f\"{client!r} sent {data!r}\")\n\n        # As a good echo handler, the request is sent back to the client\n        await client.send_packet(data)\n\n        # Leaving the generator will NOT close the connection,\n        # a new generator will be created afterwards.\n        # You may manually close the connection if you want to:\n        # await client.aclose()\n\n\nasync def main() -\u003e None:\n    host = None  # Bind on all interfaces\n    port = 9000\n    protocol = JSONProtocol()\n    handler = EchoRequestHandler()\n\n    logging.basicConfig(\n        level=logging.INFO,\n        format=\"[ %(levelname)s ] [ %(name)s ] %(message)s\",\n    )\n\n    async with AsyncTCPNetworkServer(host, port, protocol, handler) as server:\n        await server.serve_forever()\n\n\nif __name__ == \"__main__\":\n    try:\n        asyncio.run(main())\n    except* KeyboardInterrupt:\n        pass\n```\n\n### TCP Echo client with JSON data\n```py\nfrom typing import Any, TypeAlias\n\nfrom easynetwork.clients import TCPNetworkClient\nfrom easynetwork.protocol import StreamProtocol\nfrom easynetwork.serializers import JSONSerializer\n\nRequestType: TypeAlias = Any\nResponseType: TypeAlias = Any\n\n\nclass JSONProtocol(StreamProtocol[RequestType, ResponseType]):\n    def __init__(self) -\u003e None:\n        super().__init__(JSONSerializer())\n\n\ndef main() -\u003e None:\n    with TCPNetworkClient((\"localhost\", 9000), JSONProtocol()) as client:\n        client.send_packet({\"data\": {\"my_body\": [\"as json\"]}})\n        response = client.recv_packet()  # response should be the sent dictionary\n        print(response)  # prints {'data': {'my_body': ['as json']}}\n\n\nif __name__ == \"__main__\":\n    main()\n```\n\n#### Asynchronous version ( with `async def` )\n\n```py\nimport asyncio\n\nfrom easynetwork.clients import AsyncTCPNetworkClient\n\n...\n\nasync def main() -\u003e None:\n    async with AsyncTCPNetworkClient((\"localhost\", 9000), JSONProtocol()) as client:\n        await client.send_packet({\"data\": {\"my_body\": [\"as json\"]}})\n        response = await client.recv_packet()\n        print(response)  # prints {'data': {'my_body': ['as json']}}\n\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n```\n\n\u003c/details\u003e\n\n## License\nThis project is licensed under the terms of the [Apache Software License 2.0](https://github.com/francis-clairicia/EasyNetwork/blob/main/LICENSE).\n\n### AnyIO's typed attributes\n\nAnyIO's typed attributes is incorporated in `easynetwork.lowlevel.typed_attr` from [anyio 4.2](https://github.com/agronholm/anyio/tree/4.2.0), which is distributed under the [MIT license](https://github.com/agronholm/anyio/blob/4.2.0/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrancis-clairicia%2Feasynetwork","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrancis-clairicia%2Feasynetwork","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrancis-clairicia%2Feasynetwork/lists"}