-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_example.py
62 lines (46 loc) · 1.69 KB
/
server_example.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import socket
import config
EXAMPLE_FILE = os.path.abspath(os.path.join(
os.path.dirname(__file__), 'example/ballas3.txd'))
def server_example():
# Get txd size.
txd_size = os.path.getsize(EXAMPLE_FILE)
# Create connection.
connection = socket.create_connection(config.SERVER_ADDRESS)
# Send txd size.
connection.send(txd_size.to_bytes(config.HEADER_SIZE,
config.HEADER_BYTEORDER))
# Send txd data.
with open(EXAMPLE_FILE, 'rb') as txd:
while True:
chunk = txd.read()
if not chunk:
break
connection.send(chunk)
# Read png count.
png_count = int.from_bytes(connection.recv(
config.HEADER_SIZE), config.HEADER_BYTEORDER)
print('PNG count: %d' % png_count)
for i in range(png_count):
filename, _ = os.path.splitext(EXAMPLE_FILE)
png_file = os.path.join(os.path.dirname(
EXAMPLE_FILE), "%s_%d.png" % (filename, i))
# Read png size.
png_size = int.from_bytes(connection.recv(
config.HEADER_SIZE), config.HEADER_BYTEORDER)
print('PNG size: %d' % png_size)
# Write png data to file.
bytes_written = 0
with open(png_file, 'wb') as png:
while bytes_written < png_size:
while True:
# Read png data.
png_data = connection.recv(
min(config.BUFFER_SIZE, png_size - bytes_written))
if not png_data:
break
bytes_written += png.write(png_data)
connection.close()
if __name__ == '__main__':
server_example()