ValueError:字符串长度不等于格式和分辨率大小

问题描述

我需要帮助 我试图将以下代码实现到我当前的项目中:Screen sharing in python 但是客户端返回 ValueError:String length does not equal format and resolution size. 一切似乎除了像素图像外都在工作

说明:VidStreambroadCast 是服务器,VidStreamReceive 是客户端。在客户端类 pygame.display.flip() 中产生错误,明显起源于 img = pygame.image.fromstring

class VidStreambroadCast:

    def __init__(self,conn,width=800,height=600,public="public.pem",private="private.pem"):
        self.__width = width
        self.__height = height
        self.__ViralrsA = ViralrsA.ViralrsA(public,private)
        self.__conn = conn
        thread = Thread(target=self.__retreive_screenshot,args=(self.__conn,))
        thread.start()

    def __retreive_screenshot(self,conn):
        with mss() as sct:
            # The region to capture
            rect = {'top': 0,'left': 0,'width': self.__width,'height': self.__height}

            while 'recording':
                # Capture the screen
                img = sct.grab(rect)
                # Tweak the compression level here (0-9)
                pixels = compress(img.rgb,6)

                # Send the size of the pixels length
                size = len(pixels)
                size_len = (size.bit_length() + 7) // 8
                conn.send(bytes([size_len]))

                # Send the actual pixels length
                size_bytes = size.to_bytes(size_len,'big')
                conn.send(size_bytes)

                # Send pixels
                print(pixels)
                conn.sendall(pixels)

class VidStreamReceive:

    def __init__(self,private="private.pem"):
        self.__conn = conn
        self.__width = width
        self.__height = height
        self.__ViralrsA = ViralrsA.ViralrsA(public,private)
        self.__initiate_window()
        self.__watching = True

    def __receive_all(self,length):
        """ Retreive all pixels. """

        buf = b''
        while len(buf) < length:
            data = self.__conn.recv(length - len(buf))
            if not data:
                return data
            buf += data
        return buf

    def __initiate_window(self):
        pygame.init()
        screen = pygame.display.set_mode((self.__width,self.__height))
        clock = pygame.time.Clock()
        watching = True

        while watching:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    watching = False
                    self.__watching = True
                    break

            # Retreive the size of the pixels length,the pixels length and pixels
            size_len = int.from_bytes(self.__conn.recv(1),byteorder='big')
            size = int.from_bytes(self.__conn.recv(size_len),byteorder='big')
            pixels = decompress(self.__receive_all(size))
            print(pixels)

            # Create the Surface from raw pixels
            img = pygame.image.fromstring(pixels,(self.__width,self.__height),'RGB')
            # display the picture
            screen.blit(img,(0,0))
            pygame.display.flip()
            clock.tick(60)

    def get_watching(self):
        return self.__watching

注意:我知道 ViralrsA 类,它目前对未来的 RSA 加密没有任何作用。同样在重要的情况下,这些类是通过 TLS_v1.2 SSL 套接调用的。此外,所有周围的套接字结构似乎都在工作。

编辑原因显然是pygame是睡衣

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)