File tree Expand file tree Collapse file tree 6 files changed +135
-0
lines changed Expand file tree Collapse file tree 6 files changed +135
-0
lines changed Original file line number Diff line number Diff line change
1
+ * .pyc
Original file line number Diff line number Diff line change
1
+ #### 1. Build the publisher first
2
+
3
+ sudo docker build -t docker-zmq-pub .
4
+ docker run --name docker-pub-server -p 5000:5000 -p 4444:4444 -t docker-zmq-pub
5
+
6
+ #### 2. Run the Client
7
+
8
+ python zmqclient.py
9
+
10
+ Make a request to localhost:5000/downcase/?Param=Hello
11
+
12
+ Messages from publisher will start to get recorded in the client.
13
+ Additonally if the client runs as a daemon, it logs to a file called sibscriber.log
Original file line number Diff line number Diff line change
1
+ # client.py
2
+ import zmq
3
+ import sys
4
+ import time
5
+ import logging
6
+ import os
7
+
8
+ HOST = '127.0.0.1'
9
+ PORT = '4444'
10
+
11
+ logging .basicConfig (filename = 'subscriber.log' , level = logging .INFO )
12
+
13
+
14
+ class ZClient (object ):
15
+
16
+ def __init__ (self , host = HOST , port = PORT ):
17
+ """Initialize Worker"""
18
+ self .host = host
19
+ self .port = port
20
+ self ._context = zmq .Context ()
21
+ self ._subscriber = self ._context .socket (zmq .SUB )
22
+ print "Client Initiated"
23
+
24
+ def receive_message (self ):
25
+ """Start receiving messages"""
26
+ self ._subscriber .connect ('tcp://{}:{}' .format (self .host , self .port ))
27
+ self ._subscriber .setsockopt (zmq .SUBSCRIBE , b"" )
28
+
29
+ while True :
30
+ print 'listening on tcp://{}:{}' .format (self .host , self .port )
31
+ message = self ._subscriber .recv ()
32
+ print message
33
+ logging .info (
34
+ '{} - {}' .format (message , time .strftime ("%Y-%m-%d %H:%M" )))
35
+
36
+ if __name__ == '__main__' :
37
+ zs = ZClient ()
38
+ zs .receive_message ()
Original file line number Diff line number Diff line change
1
+ FROM ubuntu:14.04
2
+
3
+ RUN apt-get update
4
+ RUN apt-get install -y --force-yes python python-dev python-setuptools software-properties-common gcc python-pip
5
+ RUN apt-get clean all
6
+
7
+ RUN pip install pyzmq
8
+
9
+ RUN pip install Flask
10
+
11
+ ADD zmqserver.py /tmp/zmqserver.py
12
+
13
+ # Flask Port
14
+ EXPOSE 5000
15
+
16
+ # Zmq Sub Server
17
+ EXPOSE 4444
18
+
19
+ CMD ["python" ,"/tmp/zmqserver.py" ]
Original file line number Diff line number Diff line change
1
+ import zmq
2
+ import random
3
+ import sys
4
+ import time
5
+
6
+ port = "4444"
7
+
8
+ if len (sys .argv ) > 1 :
9
+ port = sys .argv [1 ]
10
+ int (port )
11
+
12
+ context = zmq .Context ()
13
+ socket = context .socket (zmq .PUB )
14
+ socket .bind ("tcp://*:%s" % port )
15
+
16
+ while True :
17
+ topic = random .randrange (9999 , 10005 )
18
+ messagedata = random .randrange (1 , 215 ) - 80
19
+ print "%d %d" % (topic , messagedata )
20
+ socket .send ("%d %d" % (topic , messagedata ))
21
+ time .sleep (1 )
Original file line number Diff line number Diff line change
1
+ # server.py
2
+ import time
3
+ import zmq
4
+
5
+ HOST = '127.0.0.1'
6
+ PORT = '4444'
7
+
8
+ _context = zmq .Context ()
9
+ _publisher = _context .socket (zmq .PUB )
10
+ url = 'tcp://{}:{}' .format (HOST , PORT )
11
+
12
+
13
+ def publish_message (message ):
14
+
15
+ try :
16
+ _publisher .bind (url )
17
+ time .sleep (1 )
18
+ print "sending message : {0}" .format (message , _publisher )
19
+ _publisher .send (message )
20
+
21
+ except Exception as e :
22
+ print "error {0}" .format (e )
23
+
24
+ finally :
25
+ print "unbinding"
26
+ _publisher .unbind (url )
27
+
28
+
29
+ from flask import Flask
30
+ from flask import request
31
+ app = Flask (__name__ )
32
+
33
+
34
+ @app .route ("/downcase/" , methods = ['GET' ])
35
+ def lowerString ():
36
+
37
+ _strn = request .args .get ('param' )
38
+ response = 'lower case of {} is {}' .format (_strn , _strn .lower ())
39
+ publish_message (response )
40
+ return response
41
+
42
+ if __name__ == '__main__' :
43
+ app .run (host = '0.0.0.0' , debug = False )
You can’t perform that action at this time.
0 commit comments