Python Forum
send all pictures instead of just two
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
send all pictures instead of just two
#9
here is the client.
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()
Reply


Messages In This Thread
send all pictures instead of just two - by mcgrim - Nov-06-2019, 09:35 PM
RE: send all pictures instead of just two - by mcgrim - Nov-07-2019, 12:21 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020