0
So is 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 customer:
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
The error you posted is incomplete - just put the error line, without the complete traceback. Traceback would be useful to know where exactly the error is occurring. You can edit the question and add.
– nosklo
You’re making
print(f.read())
and thenconn.send(f.read())
howeverf.read()
only works once, the second time it returns nothing, because the file has already been read all over. If you want to use the file contents twice, you have to store it in a variable, rewind the file or open it again.– nosklo
I will correct and put the new error
– Diego Cândido