-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathzmq-client.py
67 lines (51 loc) · 1.48 KB
/
zmq-client.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import zmq
import time
import random
from names import generate_name
context = zmq.Context()
# FRB prefix so it won't clash with auto generated variables of docker compose
server = os.environ.get("FRB_QUEUE_HOST", "localhost")
port = os.environ.get("FRB_CLIENT_PORT", "5559")
print "Connecting to server..."
socket = context.socket(zmq.REQ)
socket.connect("tcp://{}:{}".format(server, port))
client_id = generate_name()
poller = zmq.Poller()
poller.register(socket, zmq.POLLIN | zmq.POLLOUT)
colors = {
'bold': '\033[1m',
'red': '\033[31m',
'yellow': '\033[33m',
'green': '\033[32m',
'blue': '\033[34m',
'magenta': '\033[35m',
'cyan': '\033[36m',
'white': '\033[37m',
'normal': '\033[0m',
}
def log(response):
msg_template = (
"{red}[CLIENT: {}]"
"{bold}{green}SERVER "
"{white}{} "
"{magenta}{}{normal}"
)
print msg_template.format(client_id, response['server_id'], response['message'], **colors)
while True:
available = dict(poller.poll())
if socket not in available:
continue
if available[socket] == zmq.POLLOUT:
request = {
'client_id': client_id,
'message': generate_name()
}
socket.send_json(request)
if available[socket] == zmq.POLLIN:
response = socket.recv_json()
log(response)
time.sleep(random.choice([.3, .5, .8, .2, .6]))