{"version":"https://jsonfeed.org/version/1.1","title":"Test Channel","home_page_url":"https://mf-drktravis-dpdns-org.pages.dev","feed_url":"https://mf.drktravis.dpdns.org/json/","description":"<p>a test channel</p>","icon":"https://mf.drktravis.dpdns.org/assets/default/channel-image.png","favicon":"https://mf.drktravis.dpdns.org/assets/default/favicon.png","authors":[{"name":"lx"}],"language":"en-us","items":[{"id":"-Y-nUqC3Ni1","title":"socket","url":"https://mf.drktravis.dpdns.org/i/socket--Y-nUqC3Ni1/","content_html":"<h3>socket 服务端</h3><pre class=\"ql-syntax\" spellcheck=\"false\"># threaded_server.py\nimport Lib.debug.log\nimport socket, threading\nimport json\nimport struct\n\n\nclass SocketServer:\n&nbsp; &nbsp; HOST, PORT, BUFSIZE = '0.0.0.0', 5000, 1024 * 32\n&nbsp; &nbsp; \n&nbsp; &nbsp; def __init__(self) -&gt; None:\n&nbsp; &nbsp; &nbsp; &nbsp; self.handle = socket.socket()\n&nbsp; &nbsp; &nbsp; &nbsp; self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n&nbsp; &nbsp; &nbsp; &nbsp; self.handle.bind((self.HOST, self.PORT))\n&nbsp; &nbsp; &nbsp; &nbsp; self.handle.listen(5)\n&nbsp; &nbsp; \n&nbsp; &nbsp; def start(self):\n&nbsp; &nbsp; &nbsp; &nbsp; print(r'[*] Listening %s:%s' % (self.HOST, self.PORT))\n&nbsp; &nbsp; &nbsp; &nbsp; while True:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('wait connection...')\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection, (ip, port) = self.handle.accept()\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threading.Thread(target=self.handle_client, args=(connection, ip, port), daemon=True).start()\n\n\n&nbsp; &nbsp; def handle_client(self, connection: socket.socket, ip: str, port: int):\n&nbsp; &nbsp; &nbsp; &nbsp; ip_port = '%s:%s' % (ip, port)\n&nbsp; &nbsp; &nbsp; &nbsp; \n&nbsp; &nbsp; &nbsp; &nbsp; with connection:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('[-] Connected: (%s, %s)' % (ip, port))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while True:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('[%s] wait message...' % (ip_port))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n, = struct.unpack('!I', connection.recv(4))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # data = connection.recv(n)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print(n)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a, b = n / self.BUFSIZE, n % self.BUFSIZE\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print(a, b)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a, b = int(a), int(b)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = b''\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i in range(a):\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print('%s/%s' % (i+1, a))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data += connection.recv(self.BUFSIZE)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data += connection.recv(b)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # data = connection.recv(self.BUFSIZE)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('[%s] get message!' % (ip_port))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(data)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print('[%s] %s' % (ip_port, data))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # hex_str = ''.join([r'\\x%02x' % v for v in data])\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print('[%s] %s' % (ip_port, hex_str))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # obj = json.loads(data.decode('utf-8'))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print(obj)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # print(len(data))\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # with open(r'E:\\work\\202502\\20250206-07\\fm58-li-gz05\\fm58-li-gz05-far\\RotarySys\\Ctrl\\sd.iso', 'wb') as f:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # &nbsp; &nbsp; f.write(data)\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not data:\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.sendall(data) &nbsp; # 回显\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('[-] Disconnected: (%s, %s)' % (ip, port))\n\n\nif __name__ == '__main__':\n&nbsp; &nbsp; SocketServer().start()\n\n</pre><h3>socket 客户端</h3><p><br></p><pre class=\"ql-syntax\" spellcheck=\"false\">import socket\nimport struct\nclient = socket.socket()\nclient.connect(('localhost', 5000))\n\n\n\ndef send_all(soc: socket.socket, data: bytes):\n&nbsp; &nbsp; \"\"\"\n&nbsp; &nbsp; 传输的数据格式定义为：数据头(4字节) + 数据本体\n&nbsp; &nbsp; - 数据头：表示数据本体的长度(单位为字节)，如：b`\\x00\\x00\\x00\\x07f` 表示数据本体长度是 127字节 (总共就需要发送 131字节)\n&nbsp; &nbsp; - 数据本体：需要发送的数据\n&nbsp; &nbsp; 注：4字节能表示 2^32个无符号整数，即上述定义能表示约 4TB 的传输数据\n&nbsp; &nbsp; \"\"\"\n&nbsp; &nbsp; len_bytes = struct.pack('!I', len(data))\n&nbsp; &nbsp; # print(data)\n&nbsp; &nbsp; print(len(data))\n&nbsp; &nbsp; soc.sendall(len_bytes + data)\n\n\n\n# send_all(client, open(r'E:\\work\\202502\\20250206-07\\fm58-li-gz05\\fm58-li-gz05-far\\RotarySys\\Ctrl\\fileviewer.exe', 'rb').read())\n\n\nsend_all(client, b'hello world')\nsend_all(client, '你干嘛'.encode('utf-8'))\n\n\n\nimport time; time.sleep(1)\n</pre>","content_text":"SOCKET 服务端\n\n# threaded_server.py\nimport Lib.debug.log\nimport socket, threading\nimport json\nimport struct\n\n\nclass SocketServer:\n    HOST, PORT, BUFSIZE = '0.0.0.0', 5000, 1024 * 32\n    \n    def __init__(self) -> None:\n        self.handle = socket.socket()\n        self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n        self.handle.bind((self.HOST, self.PORT))\n        self.handle.listen(5)\n    \n    def start(self):\n        print(r'[*] Listening %s:%s' % (self.HOST, self.PORT))\n        while True:\n            print('wait connection...')\n            connection, (ip, port) = self.handle.accept()\n            threading.Thread(target=self.handle_client, args=(connection, ip, port), daemon=True).start()\n\n\n    def handle_client(self, connection: socket.socket, ip: str, port: int):\n        ip_port = '%s:%s' % (ip, port)\n        \n        with connection:\n            print('[-] Connected: (%s, %s)' % (ip, port))\n            while True:\n                print('[%s] wait message...' % (ip_port))\n                n, = struct.unpack('!I', connection.recv(4))\n                # data = connection.recv(n)\n                # print(n)\n                \n                a, b = n / self.BUFSIZE, n % self.BUFSIZE\n                # print(a, b)\n                a, b = int(a), int(b)\n                data = b''\n                for i in range(a):\n                    # print('%s/%s' % (i+1, a))\n                    data += connection.recv(self.BUFSIZE)\n                data += connection.recv(b)\n                \n                # data = connection.recv(self.BUFSIZE)\n                print('[%s] get message!' % (ip_port))\n                \n                print(data)\n                \n                # print('[%s] %s' % (ip_port, data))\n                # hex_str = ''.join([r'\\x%02x' % v for v in data])\n                # print('[%s] %s' % (ip_port, hex_str))\n                \n                \n                # obj = json.loads(data.decode('utf-8'))\n                # print(obj)\n                # print(len(data))\n                # with open(r'E:\\work\\202502\\20250206-07\\fm58-li-gz05\\fm58-li-gz05-far\\RotarySys\\Ctrl\\sd.iso', 'wb') as f:\n                #     f.write(data)\n                \n                if not data:\n                    break\n                connection.sendall(data)   # 回显\n            print('[-] Disconnected: (%s, %s)' % (ip, port))\n\n\nif __name__ == '__main__':\n    SocketServer().start()\n\n\n\n\nSOCKET 客户端\n\n\n\n\nimport socket\nimport struct\nclient = socket.socket()\nclient.connect(('localhost', 5000))\n\n\n\ndef send_all(soc: socket.socket, data: bytes):\n    \"\"\"\n    传输的数据格式定义为：数据头(4字节) + 数据本体\n    - 数据头：表示数据本体的长度(单位为字节)，如：b`\\x00\\x00\\x00\\x07f` 表示数据本体长度是 127字节 (总共就需要发送 131字节)\n    - 数据本体：需要发送的数据\n    注：4字节能表示 2^32个无符号整数，即上述定义能表示约 4TB 的传输数据\n    \"\"\"\n    len_bytes = struct.pack('!I', len(data))\n    # print(data)\n    print(len(data))\n    soc.sendall(len_bytes + data)\n\n\n\n# send_all(client, open(r'E:\\work\\202502\\20250206-07\\fm58-li-gz05\\fm58-li-gz05-far\\RotarySys\\Ctrl\\fileviewer.exe', 'rb').read())\n\n\nsend_all(client, b'hello world')\nsend_all(client, '你干嘛'.encode('utf-8'))\n\n\n\nimport time; time.sleep(1)\n","date_published":"2025-10-27T01:15:49.864Z","_microfeed":{"web_url":"https://mf.drktravis.dpdns.org/i/socket--Y-nUqC3Ni1/","json_url":"https://mf.drktravis.dpdns.org/i/-Y-nUqC3Ni1/json/","rss_url":"https://mf.drktravis.dpdns.org/i/-Y-nUqC3Ni1/rss/","guid":"-Y-nUqC3Ni1","status":"published","itunes:episodeType":"full","date_published_short":"Sun Oct 26 2025","date_published_ms":1761527749864}}],"_microfeed":{"microfeed_version":"0.1.5","base_url":"https://mf.drktravis.dpdns.org","categories":[],"subscribe_methods":[{"name":"RSS","type":"rss","url":"https://mf.drktravis.dpdns.org/rss/","image":"https://mf.drktravis.dpdns.org/assets/brands/subscribe/rss.png","enabled":true,"editable":false,"id":"Agja4bk5F3s"},{"name":"JSON","type":"json","url":"https://mf.drktravis.dpdns.org/json/","image":"https://mf.drktravis.dpdns.org/assets/brands/subscribe/json.png","enabled":true,"editable":false,"id":"jrJEKKcTrVn"}],"description_text":"a test channel","copyright":"©2025","itunes:type":"episodic","items_sort_order":"newest_first"}}