Как отправить изображение с сервера клиенту в PythonPython

Программы на Python
Ответить
Anonymous
 Как отправить изображение с сервера клиенту в Python

Сообщение Anonymous »

Я новичок в Python и не могу понять, что не так с моим кодом:
Я настроил клиент и сервер (оба на локальном хосте), сервер сделать снимок и предположим отправить его клиенту, который сохранит его в какой-то папке, все это происходит, но файл JPG некорректно открывает изображение, вместо этого пишет, что он не поддерживает формат файла:\
код:
clientFile.py

Код: Выделить всё

import socket

# set up a socket connection object
my_socket = socket.socket()
# make the socket connect to a certain ip&port
my_socket.connect(("127.0.0.1", 8821))

data = ''

while data != 'EXIT':
message = input('enter a message to send to the server \n')
# send the server what to do (in this case take a snapshot)
my_socket.send(message.encode())

data = my_socket.recv(1024).decode()

if data == 'snapshot has been taken':
# recieve the file's byte length from the server ( for example: 2000000 bytes => 7 length)
data = my_socket.recv(1).decode()

sizeLength = int(data) % 10
# recieve the size of the file picture
data = my_socket.recv(sizeLength).decode()
size = int(data)

# recieve the file in binary
picture = my_socket.recv(size)
# trying to write the file :\
myfile = open(r'C:\test\client\screen.jpg', 'wb')
myfile.write(picture)
myfile.close()
else:
print('closing the socket')
my_socket.close()
server.py

Код: Выделить всё

    import socket
import datetime
import glob
import os
import pyautogui
import subprocess

server_socket = socket.socket()
# accepting a socket from which client (0.0.0.0) means everyone
server_socket.bind(("0.0.0.0", 8821))
server_socket.listen()
# accept sockets and recieve socket and client ip&port as tuples
(client_socket, client_address) = server_socket.accept()

# data = client_socket.recv(1024).decode()
data = ''
while data != 'EXIT':
.
.
.
elif data == 'SNAPSHOT':
#snapshot saved succesfully !
image = pyautogui.screenshot()
image.save(r'C:\test\screen.jpg')

# file stats (size and stuff)
picStats = os.stat(r'C:\test\screen.jpg')

picSize = picStats.st_size
picLength = len(str(picSize))

myfile = open(r'C:\test\screen.jpg', 'rb')
print(myfile.read())
reply = 'snapshot has been taken'
# make the client prepere for the picture to be send
client_socket.send(reply.encode())
# send the picture length
client_socket.send(str(picLength).encode())
# send the picture size
client_socket.send(str(picSize).encode())
client_socket.send(myfile.read())

.
.
.
#closing sockets eventually ...

мне кажется, я что-то упускаю...
спасибо

Подробнее здесь: https://stackoverflow.com/questions/654 ... -in-python
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»