bTCP_server.py 925 Bytes
Newer Older
StevenWdV's avatar
StevenWdV committed
1
#!/bin/python3
2
import argparse
StevenWdV's avatar
StevenWdV committed
3
import logging
4 5
import socket
import struct
StevenWdV's avatar
StevenWdV committed
6
import sys
StevenWdV's avatar
StevenWdV committed
7

8 9
import btcp

10
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
StevenWdV's avatar
StevenWdV committed
11

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

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

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

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

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

34
binding.close()