-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_listener.py
executable file
·49 lines (36 loc) · 1.17 KB
/
py_listener.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
48
49
#!/usr/bin/env python
# pip reqs: zmq
import re
import socket
import zmq
cached_hosts = {}
def resolve_ip(ip_addr: str) -> str:
host = cached_hosts.get(ip_addr)
if host is None:
try:
results = socket.gethostbyaddr(ip_addr)
if len(results) > 0:
host = results[0]
except socket.herror:
host = ip_addr
cached_hosts[ip_addr] = host
return host
def main():
context = zmq.Context()
# Socket to talk to server
print('Connecting...')
zmq_sub = context.socket(zmq.SUB)
zmq_sub.connect('tcp://localhost:5555')
zmq_sub.setsockopt_string(zmq.SUBSCRIBE, 'reset')
msg_pat = re.compile(r'^reset ((?:\d{1,3}\.){3}\d{1,3}):(\d+?) ((?:\d{1,3}\.){3}\d{1,3}):(\d+?)$')
while True:
message = zmq_sub.recv_string()
match = msg_pat.match(message)
if match is not None:
saddr = resolve_ip(match.group(1))
sport = int(match.group(2))
daddr = resolve_ip(match.group(3))
dport = int(match.group(4))
print('{}:{} <-> {}:{} was reset'.format(saddr, sport, daddr, dport))
if __name__ == '__main__':
main()