Nov-07-2019, 12:21 PM
here is the client.
The server is the same.
The server is the same.
##### CLIENT CODE import socket from PIL import Image import io host = "127.0.0.1" port = 5000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) received = 0 buf = [b''] def get_item(s, buf=buf): acc = buf[0] while not b':' in acc: acc += s.recv(4096) prefix, acc = acc.split(b':', 1) if prefix == b'end': n = 0 else: n = int(prefix) while len(acc) < n: acc += s.recv(4096) buf[0] = acc[n:] return (prefix, acc[:n]) with open('received_file.jpg', 'wb') as f: print('file is open') data = s.recv(440179) f.write(data) while True: prefix, chunk = get_item(s) received += len(chunk) if prefix == b'end': image = Image.open(io.BytesIO(b'chunk')) image.show() print('End of image') print('Finished receiving. In total, {} bytes were received'.format(received)) s.close()