-
Notifications
You must be signed in to change notification settings - Fork 59
/
udpaddr
executable file
·60 lines (52 loc) · 1.29 KB
/
udpaddr
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
55
56
57
58
59
60
#!/usr/bin/env python3
# vim:fileencoding=utf-8
'''echo back clients' udp address'''
import sys
import time
import socket
registry = []
def get_peer(name, addr):
now = time.time()
for t, k, a in reversed(registry):
if now - t > 500:
ret = None
break
if k == name and addr[0] != a[0]:
ret = a
break
else:
ret = None
registry.append((now, name, addr))
del registry[:-50]
return ret
def main(port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', port))
try:
while True:
data, addr = s.recvfrom(4096)
msg = data.decode('utf-8', 'replace').rstrip('\n')
try:
cmd, *rest = msg.split()
except ValueError:
continue
cmd = cmd.upper()
if cmd == 'REG' and len(rest):
name = rest[0]
back = 'ADDR %s %d' % addr
peer_addr = get_peer(name, addr)
if peer_addr:
back += ' %s %d' % peer_addr
back += '\n'
else:
back = 'Your address is %r\n' % (addr,)
s.sendto(back.encode(), addr)
print(time.strftime('%Y-%m-%d %H:%M:%S'), addr, msg)
except KeyboardInterrupt:
print()
if __name__ == '__main__':
try:
port = int(sys.argv[1])
except (ValueError, IndexError):
sys.exit('which port to listen?')
main(port)