从插座接收到的数据

发布于 2025-01-26 17:23:05 字数 2062 浏览 4 评论 0原文

上下文: 我有一个带有server.py.py文件的系统和live_client.py。服务器正在不断更新对象。如果我通过插座发送对象的属性(cliend.send(str(obj.attribute_to_send).encode('utf-8''))。

问题: 之后,我尝试使用Pickle client.send(pickle.loads(obj))从服务器发送对象。当客户端试图用cickle.loads(data_received)将对象恢复时,就会发生问题。 错误代码: _pickle.unpicklingerror:泡菜数据被截断

server.py

async def stream_feed_clients():
    obj_gen = object_generator()
    global object
    global STREAM_CLIENTS
    async for obj in obj_gen:
        object = obj        
        for client in STREAM_CLIENTS: #STREAM_CLIENTS:Each time a client connects, another thread stores the client in this variable
            try:
                client.send(pickle.dumps(obj))
            except ConnectionResetError:
                client.close()
                print("Client disconnected!")
                STREAM_CLIENTS = set(filter(lambda x: x != client, STREAM_CLIENTS))

import socket, pickle, time

from CustomClass import MyClass

HOST = 'localhost'
PORT = XXXX
try:
    c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    c.connect((HOST, PORT))
    message = {'client_type': 'LIVE'}
    print('Waiting')
    time.sleep(5)
    print('Sending')
    c.send(pickle.dumps(message)) # NOTE: This pickle works. It is used to notify the server this is a client that wants live data.
    print('Message sent')
    while True:
        data = c.recv(4096 * 32) # I've been trying with different buffer sizes.
        obj = pickle.loads(data) # ERROR OCCURS HERE: _pickle.UnpicklingError: pickle data was truncated
        print(obj.a)
except KeyboardInterrupt:
    print('Closing connection to server')
    c.close()
    print('Connection closed')
    print('Exiting...')
    exit()

客户

class MyClass:
    def __init__(self, a):
        self.a = a
    
    def get_a(self):
        return self.a

Context:
I have a system with a server.py file and a live_client.py. Server is constantly updating an object. The system works correctly if I send the attributes of the object (which are numbers) via sockets (cliend.send(str(obj.attribute_to_send).encode('utf-8')).

Problem:
After, I tried to send the object from the server with pickle client.send(pickle.loads(obj)). Problem occured when the the client tries to get the object back with pickle.loads(data_received). Error code: _pickle.UnpicklingError: pickle data was truncated.

server.py

async def stream_feed_clients():
    obj_gen = object_generator()
    global object
    global STREAM_CLIENTS
    async for obj in obj_gen:
        object = obj        
        for client in STREAM_CLIENTS: #STREAM_CLIENTS:Each time a client connects, another thread stores the client in this variable
            try:
                client.send(pickle.dumps(obj))
            except ConnectionResetError:
                client.close()
                print("Client disconnected!")
                STREAM_CLIENTS = set(filter(lambda x: x != client, STREAM_CLIENTS))

client.py

import socket, pickle, time

from CustomClass import MyClass

HOST = 'localhost'
PORT = XXXX
try:
    c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    c.connect((HOST, PORT))
    message = {'client_type': 'LIVE'}
    print('Waiting')
    time.sleep(5)
    print('Sending')
    c.send(pickle.dumps(message)) # NOTE: This pickle works. It is used to notify the server this is a client that wants live data.
    print('Message sent')
    while True:
        data = c.recv(4096 * 32) # I've been trying with different buffer sizes.
        obj = pickle.loads(data) # ERROR OCCURS HERE: _pickle.UnpicklingError: pickle data was truncated
        print(obj.a)
except KeyboardInterrupt:
    print('Closing connection to server')
    c.close()
    print('Connection closed')
    print('Exiting...')
    exit()

Class code of object being sent

class MyClass:
    def __init__(self, a):
        self.a = a
    
    def get_a(self):
        return self.a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文