-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpublisher.py
52 lines (40 loc) · 1.19 KB
/
publisher.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
50
51
52
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import time
import logging
import coloredlogs
import zmq
from names import generate_name
logger = logging.getLogger()
zmq_context = zmq.Context()
def main():
coloredlogs.install(level=logging.INFO, show_hostname=False)
publisher = zmq_context.socket(zmq.PUB)
publisher.bind('tcp://0.0.0.0:8888')
poller = zmq.Poller()
poller.register(publisher, zmq.POLLOUT)
logger.info("listening on tcp://0.0.0.0:8888")
MAX = 200
index = 0
while True:
if index > MAX:
url = 'stop'
else:
url = generate_name()
index += 1
try:
socks = dict(poller.poll(0.3))
if publisher in socks and socks[publisher] == zmq.POLLOUT:
logger.info("sending data %s", url)
publisher.send(b" ".join([b'status', bytes(url)]))
if url == 'stop':
break
else:
time.sleep(.13)
except KeyboardInterrupt:
publisher.close()
zmq_context.term()
raise SystemExit
if __name__ == '__main__':
main()