aboutsummaryrefslogtreecommitdiff
path: root/netsec-assignment1-S4498062/exercise2/netcat-l.py
blob: 8a4ce2f839e6ebd2bcfced1bf0b7424dde5ecb64 (plain) (blame)
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
#!/usr/bin/env python3

import socket

def handle(passedconn):
    data = b""
    newdata, addr = passedconn.recvfrom(size)
    print(newdata, addr)
    while newdata:
        datastring = newdata.decode("utf-8")
        print(handlestring(datastring, len("spam "), "\n"))
        newdata, addr = passedconn.recvfrom(size)

def handlestring(datastring, length, delimiter):
    stringlist = datastring.split(sep=delimiter)
    filteredlist = [s[length:] for s in stringlist]
    filteredstring = delimiter.join(filteredlist)
    return filteredstring

def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host, port))
    handle(s)
    s.close()

host = "localhost"
port = 42424
size = 2 ** 16 - 1

if __name__ == "__main__":
    main()