gadgets-OSC/tools/relay.py

61 lines
1.2 KiB
Python
Raw Normal View History

2020-04-14 20:08:52 +11:00
#!/usr/bin/python3
2020-02-22 04:05:13 +11:00
import socket, sys, getopt
# --- default values
RX_UDP_IP = "" # pour ecouter sur toutes les interfaces
RX_UDP_PORT = 5005 # entree du relais
# ---
rx_port = RX_UDP_PORT
verbose = 0
# --- parse command line arguments
options = "hvp:c:"
arguments = sys.argv[1:]
opts, args = getopt.getopt(arguments, options)
for o, a in opts:
2020-04-14 20:08:52 +11:00
print (' ', o, ' --> ', a)
2020-02-22 04:05:13 +11:00
if "-v" == o: verbose += 1
elif "-p" == o: rx_port = int(a)
2020-04-14 20:08:52 +11:00
print ("listening port : ", rx_port)
2020-02-22 04:05:13 +11:00
# ---
cibles = [ ];
for ligne in open("destinations.liste"):
a, p = ligne.replace('\n', '').split(":")
# print a, p
if p: cibles.append((a, int(p)))
for cible in cibles:
2020-04-14 20:08:52 +11:00
print (" -> ", cible)
2020-02-22 04:05:13 +11:00
# point d'entree, d'ecoute
sock_rx = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock_rx.bind((RX_UDP_IP, rx_port))
# point de sortie vers les autres
sock_tx = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
while True:
data, addr = sock_rx.recvfrom(1024) # buffer size is 1024 bytes
if verbose:
2020-04-14 20:08:52 +11:00
print ("got:", addr, " ", len(data))
2020-02-22 04:05:13 +11:00
for cible in cibles:
# print cible
sock_tx.sendto(data, cible)
# hop, ce truc doit fonctionner !