{"id":14970923,"url":"https://github.com/abhijithaj/iotsocket","last_synced_at":"2025-10-26T14:30:29.113Z","repository":{"id":62571444,"uuid":"228392036","full_name":"AbhijithAJ/IOTSocket","owner":"AbhijithAJ","description":"A python-based secured IoT WebSocket to handle multiple clients simultaneously.","archived":false,"fork":false,"pushed_at":"2021-12-17T19:25:29.000Z","size":99,"stargazers_count":44,"open_issues_count":1,"forks_count":11,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-31T20:14:02.855Z","etag":null,"topics":["arduino","client-socket","easy","easy-to-use","esp","iot","iot-application","iot-device","iot-devices","iot-websocket","iotsocket","python","raspberry-pi","socket","ssl","sync","tcp","websocket"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AbhijithAJ.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}},"created_at":"2019-12-16T13:20:44.000Z","updated_at":"2024-05-23T07:21:11.000Z","dependencies_parsed_at":"2022-11-03T18:26:20.152Z","dependency_job_id":null,"html_url":"https://github.com/AbhijithAJ/IOTSocket","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbhijithAJ%2FIOTSocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbhijithAJ%2FIOTSocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbhijithAJ%2FIOTSocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AbhijithAJ%2FIOTSocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AbhijithAJ","download_url":"https://codeload.github.com/AbhijithAJ/IOTSocket/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238337756,"owners_count":19455366,"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":["arduino","client-socket","easy","easy-to-use","esp","iot","iot-application","iot-device","iot-devices","iot-websocket","iotsocket","python","raspberry-pi","socket","ssl","sync","tcp","websocket"],"created_at":"2024-09-24T13:44:21.805Z","updated_at":"2025-10-26T14:30:28.807Z","avatar_url":"https://github.com/AbhijithAJ.png","language":"Python","funding_links":["https://www.paypal.me/abhijithboppes","https://www.buymeacoffee.com/abhijithboppe"],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  IoTSocket v1.0\n\u003cdiv align=\"center\"\u003e\n\n[![Generic badge](https://img.shields.io/badge/Made_By-ABHIJITH_BOPPE-BLUE.svg)](https://www.linkedin.com/in/abhijith-boppe/)  \n[![made-with-python](https://img.shields.io/badge/Made%20with-Python-1f425f.svg)](https://www.python.org/) [![Generic badge](https://img.shields.io/badge/pypi_package-1.0-DARKGREEN.svg)](https://pypi.org/project/IOTSocket/) [![PyPI license](https://img.shields.io/pypi/l/ansicolortags.svg)](https://github.com/AbhijithAJ/IOTSocket/blob/master/LICENSE) [![PayPal](https://img.shields.io/badge/donate-PayPal-blue.svg)](https://www.paypal.me/abhijithboppes)     \n\u003c/div\u003e\n\n\n\u003c/h1\u003e\n\n - Time based verification\n - TLS/SSL \n - TCP/IP\n - Device Validation\n---\n## ABOUT\n\nIOT Socket Server handle multiple clients (with unique deviceID and its key) simultaneously for bidirectional communication.\nIt is similar to WebSocket. Here we can handle each client individually from there device id.\n\nIOT devices like Raspberry can also use this module as client with IOTSocketClient module.\n\n\n**How is it secured ?**\n\nTo prevent Replay attacks and device cloning attacks.\n\nFor every data transmission from client to server or vice versa there is a time stamp which is compared with server/client present time. If the time doesn't match (tolerance of -2 sec) or if there is any reused time then socket is closed.\n\nBy using these headers for every transmission, IOT devices over WiFi can also be secured.\n\n*NOTE:*\n- Make sure the device time is in sync with server time (use RTC)\n- Re-establish client socket connection every 24 hours\n- SSL key pinning on client-side.\n- Verify device id and key from database\n- Client socket will be closed if there is no data for 90 sec\n\n### Installation\nYou can install IOTSocket by running the following command\n```\npip install IOTSocket\n```\n### Example Server\n```python\n'''\nDeveloped by Abhijith Boppe - linkedin.com/in/abhijith-boppe/\n'''\nfrom IOTSocket import IOTSocketServer, IOTSocketServerSSL, IOTSocket\nimport time\nfrom clrprint import *\n\nhost = \"127.0.0.1\"\nport = 9000\n\n# give certificate path and key path\ncertfile_path = \"/user/cert.pem\"\nkeyfile_path = \"/user/cert.key\"\ndelimiter = '\\r\\n#*\\r\\n'\n\n# give some insecure data te be filtered and sanitized to ''\nlst_of_data_to_remove = [delimiter]\nprev_call = 0\n\n# this function will be called recursively to check if server want to push any data\ndef from_server_to_client():\n    '''\n    create a FIFO named pipe, make your backend application like PHP\n    to write into it and you return a list. Like: ['id1 data1', 'id2 data2', 'id3 data3', .....]\n\n    Ex: ['23234 ON SWITCH 1','23235 OFF LIGHT','232365 GET ALL SENSOR VALUES']\n    (id must be numaric values only)\n    \n    '''\n    return []\n\nclass handleEachClientHere(IOTSocket):\n    def DeviceVerify(self, id_, key):          # 'id_' - int , 'key' - string\n        '''\n        This method is called when a new client is connected.\n        Verify whether device id and key matches in database records\n        and check if it is activated.\n        (Check from DB)\n        '''        \n        return 1    #return True if verified successfully else false\n\n    def handleMessage(self, id_, data):\n        '''\n        handle client id and data for further processing.\n        create a fifo named pipe and pass the data to your\n        backed application\n\n        (make sure u remove delimiters and other vulnerable strings which effect the backend application)\n        '''\n        for i in lst_of_data_to_remove:         # remove delimiters/data, if any are present in client data to prevent clashes\n            data.replace(i, '')\n        clrprint(id_, data,clr='b')\n\n    def handleClose(self, error_repo=''):\n        '''\n        handle error if any during socket handling\n        error start with \"ERROR: \"\n        and normal socket close will end with normal message\n        '''\n        if \"ERROR:\" in str(error_repo):\n            clrprint(error_repo,clr='r')\n        else:\n            pass\n\nclrprint(f\"Server started listening on socket {host}:{port}\", clr='g')\nserver = IOTSocketServer(host, port, from_server_to_client,handleEachClientHere)        # without ssl\n# server = IOTSocketServerSSL(host, port, from_server_to_client, handleEachClientHere, certfile = certfile_path, keyfile = keyfile_path)\nserver.serveforever()\n```\n### Example client\n```python\n\n'''\nDeveloped by Abhijith Boppe - linkedin.com/in/abhijith-boppe/\n\nclient example for raspberry \n'''\n\nfrom IOTSocket import IOTSocketClient as sock\nimport time\nfrom clrprint import *\n\nhost = '127.0.0.1'\nport = 9000\ndevice_id = '1234567890'\ndevice_key ='1432qrzd23'\ncertfile_path = \"/user/cert.pem\"      # for key pinning (certificate pinning)\nprev_call = 0\n\ndef someThingtoSend():\n    '''\n    this function is called recursively.\n    read data from sensor and return data\n    '''\n    global prev_call\n    time_now = time.time()\n    if (abs(time_now - prev_call) \u003e 10) or prev_call == 0: # send sensor data every 10 seconds\n        example = 'temp=33.5\u0026humid=40%'\n        prev_call = time_now\n        return example\n    else:\n        return ''\n\ndef handleCmdsFromServer(data):\n    '''\n    This function is called when ever there is \n    data/command from the server.\n    '''\n    clrprint(data,clr='b')\n\nwhile 1: # reconnect if socket is closed\n    try:\n        clrprint(f\"\\nEstablishing socket connection to {host}:{port}\",clr='y')\n        sock.connectionSet(host,port,device_id,device_key,Encrypt=False, cert_path= certfile_path)  # set IOT Socket connection with valid Device ID and Key.\n        # Continuously check for receiving / transmitting of data\n        clrprint(f\"Connection established successfully\",clr='g')\n        while 1:\n            data = someThingtoSend()\n            if data != '':\n                sock.sendData(data)     # send data to server if data is available to send\n            rcv_data = sock.recvData()  # receive data from server if available\n            if len(rcv_data) \u003e 5:\n                handleCmdsFromServer(rcv_data)   # handle your data here\n\n    except Exception as n:\n        clr = 'r' if \"ERROR:\" in str(n) else 'y'\n        clrprint(n,clr='r')\n        clrprint('closing socket',clr='y')      \n        try:\n            sock.sock.close()\n        except:\n            pass\n        time.sleep(10)\n        \n\n```\n\n### Additional Information\n\nThe connection is established directly on 1st request from client. Make sure the client and server time are in sync.\n\nRead/Write to your application from using fifo named pipe recursively without closing. \n\nPlease go through the code for better understanding of the protocol.\n\n\u003cbr\u003e\n\u003ca href=\"https://www.buymeacoffee.com/abhijithboppe\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/v2/default-orange.png\" alt=\"Buy Me A Coffee\" width=\"33%\" \u003e\u003c/a\u003e\n\n---\n## License \u0026 copyright\n© Abhijith Boppe, Security analyst\n\nlinkedin.com/in/abhijith-boppe\n\nLicensed under the [MIT License](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabhijithaj%2Fiotsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabhijithaj%2Fiotsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabhijithaj%2Fiotsocket/lists"}