小编Mao*_*871的帖子

使用 TCP 连接发送 PNG 文件

我正在尝试将 png 图像(屏幕截图)从服务器发送到客户端。

我解码它并将解码后的字符串发送给客户端,客户端使用它来将图像保存在他的计算机中。

但是我从客户那里得到的图像并不完美,根本......

客户

while "finish" not in data:
        data += receive(data_len)
    data = data[:-7]

fh = open("imageToSave.png", "wb")
fh.write(data.decode('base64'))
fh.close()
Run Code Online (Sandbox Code Playgroud)

服务器

ImageGrab.grab().save("screen_capture.png", "PNG")
            #Convert the image to a string that it will be able to be send to the client
            with open("screen_capture.png", "rb") as imageFile:
                Image_Str = base64.b64encode(imageFile.read())
            fh = open("text", "wb")
            fh.write(Image_Str)
            fh.close
            fh = open("text", "rb")
            str1 = fh.read(150)
            client_socket.send("150~" + str1)
            while str1:
                str1 = fh.read(150)
                client_socket.send(str1)
            client_socket.send("6finish")
Run Code Online (Sandbox Code Playgroud)

我试图检查字符串是否相同 - 看起来它们是......当我尝试将字符串解码回服务器中的图像时 - 它工作......

python sockets networking tcp

4
推荐指数
1
解决办法
1522
查看次数

标签 统计

networking ×1

python ×1

sockets ×1

tcp ×1