summaryrefslogtreecommitdiffstats
path: root/test/functional/util/dummy_udp.py
blob: f05d6d6802d6508366432987c5d8436d6ac6d4a7 (plain)
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
#!/usr/bin/env python3

import socket
import sys

import dummy_killer

UDP_IP = "127.0.0.1"
PID = "/tmp/dummy_udp.pid"

if __name__ == "__main__":
    alen = len(sys.argv)
    if alen > 1:
        port = int(sys.argv[1])
    else:
        port = 5005
    sock = socket.socket(socket.AF_INET, # Internet
                         socket.SOCK_DGRAM) # UDP
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((UDP_IP, port))
    dummy_killer.write_pid(PID)

    while True:
        data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
        print("received message:", data)
        sock.sendto(data, addr)