-
Notifications
You must be signed in to change notification settings - Fork 0
/
sctp_server.py
47 lines (41 loc) · 1.38 KB
/
sctp_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
import socket
import sctp
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class SCTPHandler:
# SCTP server class to handle incoming data.
def __init__(self):
# Initialisations
self.dest_ip_port_tuples = (("10.10.2.2", 8765), ("10.10.4.2", 8765))
self.filename = None
self.sock = sctp.sctpsocket_tcp(socket.AF_INET)
self.sock.bindx(self.dest_ip_port_tuples)
self.sock.listen(10)
def serve_forever(self):
# Allows server to receive more than 1 file
while 1:
self.serve()
def serve(self):
# Accept incoming connection
connection, address = self.sock.accept()
# Get initial information like filename etc.
initial = connection.recv(1000)
incoming = initial.split(':')
self.total_size = int(incoming[2])
self.buffer_size = int(incoming[1])
self.filename = incoming[0]
self.file = open(self.filename, 'wb')
total = 0
while 1:
# Get file until all bits are received.
buf = connection.recv(self.buffer_size)
if buf > 0:
self.file.write(buf)
total += len(buf) # or buf ?
print total
if total == self.total_size:
break
if __name__ == "__main__":
sctp_server = SCTPHandler()
sctp_server.serve_forever()