Mao*_*871 4 python sockets networking tcp
我正在尝试将 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)
我试图检查字符串是否相同 - 看起来它们是......当我尝试将字符串解码回服务器中的图像时 - 它工作......
您需要使用client_socket.sendall(strl)而不是send.
使用send操作系统发送数据时,会告诉您已接受通过网络发送的数据量,但它可能小于字符串的全长。
sendall 是一种用于套接字的 Python 方法,可为您执行所需的循环。
| 归档时间: |
|
| 查看次数: |
1522 次 |
| 最近记录: |