-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen_client.py
More file actions
30 lines (27 loc) · 1001 Bytes
/
Copy pathscreen_client.py
File metadata and controls
30 lines (27 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import socket, struct
def main(host):
# Connect to server and get image size.
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, 65000))
packed = recvall(client, struct.calcsize('!I'))
# Decode the size and get the image data.
size = struct.unpack('!I', packed)[0]
print('Receiving data from:', host)
data = recvall(client, size)
# Shutdown the socket and create the image file.
client.shutdown(socket.SHUT_RDWR)
client.close()
with open('image.bmp', 'wb') as file:
file.write(data)
def recvall(sock, size):
message = bytearray()
# Loop until all expected data is received.
while len(message) < size:
buffer = sock.recv(size - len(message))
if not buffer:
# End of stream was found when unexpected.
raise EOFError('Could not receive all expected data!')
message.extend(buffer)
return bytes(message)
if __name__ == '__main__':
main('localhost')