-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcp-server.py
54 lines (38 loc) · 1.13 KB
/
tcp-server.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
import argparse
import socket
import time
from constants import CHUNK_SIZE
def get_timestamp():
return int(round(time.time()*1000))
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--host", default="127.0.0.1")
parser.add_argument("-P", "--port", type=int, default="8080")
return parser.parse_args()
def main():
args = parse_arguments()
address = (args.host, args.port)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(address)
sock.listen(1)
while True:
conn, addr = sock.accept()
if not conn:
break
print("Accepted connection from {}".format(addr))
filename = "./file-{}.bin".format(get_timestamp())
f = open(filename, "wb")
bytes_received = 0
size = int(conn.recv(CHUNK_SIZE).decode())
conn.send(b'start')
while bytes_received < size:
data = conn.recv(CHUNK_SIZE)
bytes_received += len(data)
f.write(data)
print("Received file {}".format(filename))
# Send number of bytes received
conn.send(str(bytes_received).encode())
f.close()
sock.close()
if __name__ == "__main__":
main()