So there's the server:
import socket
from PIL import Image
port = 8000
host = '127.0.0.1'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(5)
while True:
conn, addr = s.accept()
print('Conectado a {}'.format(addr))
with open('logo_python.png', 'rb') as f:
conn.send(f.read())
l = f.read()
#im = Image.open(l)
#im.show()
f.close()
print('Arquivo enviado')
And the client:
import socket
from PIL import Image
host = '127.0.0.1'
port = 8000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
print("Recebendo Dados...\n")
with open('recebido.png', 'wb') as f:
print('file opened')
print('Recebendo dados...')
data = s.recv(4000)
f.write(data)
print(data)
print("ENVIADO")
f.close()
with open('recebido.png', 'rb') as f:
im = Image.open(f)
im.show()
print('Transferência completa!!!')
s.close()
print('Conexão encerrada.')
The error that appears:
File "C:/Users/Diego Cândido/PycharmProjects/servarq/ClienteTeste.py", line 18, in <module>
im = Image.open(f)
File "C:\Users\Diego Cândido\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 2618, in open
prefix = fp.read(16)
File "C:\Users\Diego Cândido\AppData\Local\Programs\Python\Python37\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 172: character maps to <undefined>
Basically what the server is sending to the client is:
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x02Y\x00\x00\x00\xcb\x08\x06\x00\x00\x00]\xc9\x86&\x00\x00\x00\x04sBIT\x08\x08\x08\x08 #... e assim por diante