bTCP_server.py 974 Bytes
Newer Older
StevenWdV's avatar
StevenWdV committed
1
#!/bin/python3
2 3
# s1005864 Lars Kuipers, s1011387 Steven Wallis de Vries

4
import argparse
StevenWdV's avatar
StevenWdV committed
5
import logging
6 7
import socket
import struct
StevenWdV's avatar
StevenWdV committed
8
import sys
StevenWdV's avatar
StevenWdV committed
9

10 11
import btcp

12
logging.basicConfig(stream=sys.stderr, level=logging.ERROR)
StevenWdV's avatar
StevenWdV committed
13

StevenWdV's avatar
StevenWdV committed
14
parser = argparse.ArgumentParser()
15
parser.add_argument("-w", "--window", help="Define bTCP window size", type=int, default=80)
StevenWdV's avatar
StevenWdV committed
16
parser.add_argument("-t", "--timeout", help="Define bTCP timeout in milliseconds", type=int, default=100)
17
parser.add_argument("-o", "--output", help="Where to store file", default="tmp.file")
StevenWdV's avatar
StevenWdV committed
18 19
args = parser.parse_args()

20
binding = btcp.Binding(socket.AF_INET, ("localhost", 9001), args.window, args.timeout)
21 22 23 24
server = binding.bind_server(0)

server.start_listen(1)
connection = server.accept()
25
connection.close_sender()
StevenWdV's avatar
StevenWdV committed
26

27
file_size: int = struct.unpack("!Q", connection.receive(8))[0]
StevenWdV's avatar
StevenWdV committed
28

StevenWdV's avatar
StevenWdV committed
29
file = open(args.output, "w+b")
30 31 32
data = connection.receive_array(file_size)
for block in data:
    file.write(block)
33
file.close()
StevenWdV's avatar
StevenWdV committed
34

35
binding.close()