gpt4 book ai didi

python - 使用 TCP 连接发送 PNG 文件

转载 作者:可可西里 更新时间:2023-11-01 02:33:45 24 4
gpt4 key购买 nike

我正在尝试将 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()

服务器

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")

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

最佳答案

您需要使用client_socket.sendall(strl) 而不是send

当使用 send 发送数据时,操作系统会告诉您已接受通过网络发送的数据量,但它可能小于字符串的全长。

sendall 是一种用于套接字的 python 方法,可以为您执行所需的循环。

关于python - 使用 TCP 连接发送 PNG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33177788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com