Last active
December 24, 2015 13:49
-
-
Save keyz182/6807542 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'keyz' | |
import zmq | |
import sys | |
import random | |
PROXY_ADDRESS = "tcp://localhost:5559" | |
def main(): | |
context = zmq.Context() | |
print "Connecting to server..." | |
socket = context.socket(zmq.REQ) | |
socket.connect (PROXY_ADDRESS) | |
client_id = random.randrange(1,10005) | |
# Do 10 requests, waiting each time for a response | |
for request in range (1,10): | |
print "Sending request ", request,"..." | |
socket.send ("Hello from %s" % client_id) | |
# Get the reply. | |
message = socket.recv() | |
print "Received reply ", request, "[", message, "]" | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'keyz' | |
import zmq | |
INTERNAL_SIDE_PORT = 5559 | |
QUEUE_RIGHT_ADDRESS = "tcp://localhost:5520" | |
def main(): | |
try: | |
context = zmq.Context(1) | |
# Socket facing clients | |
frontend = context.socket(zmq.XREP) | |
frontend.bind("tcp://*:%s" % INTERNAL_SIDE_PORT) | |
backend = context.socket(zmq.XREQ) | |
backend.connect(QUEUE_RIGHT_ADDRESS) | |
zmq.device(zmq.QUEUE, frontend, backend) | |
frontend.close() | |
backend.close() | |
context.term() | |
except Exception as e: | |
print e | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'keyz' | |
import zmq | |
QUEUE_LEFT_PORT = 5520 | |
SERVER_PORT = 5560 | |
def main(): | |
try: | |
context = zmq.Context(1) | |
# Socket facing clients | |
frontend = context.socket(zmq.XREP) | |
frontend.bind("tcp://*:%s" % QUEUE_LEFT_PORT) | |
# Socket facing services | |
backend = context.socket(zmq.XREQ) | |
backend.bind("tcp://*:%s" % SERVER_PORT) | |
zmq.device(zmq.QUEUE, frontend, backend) | |
except Exception, e: | |
print e | |
print "bringing down zmq device" | |
finally: | |
pass | |
frontend.close() | |
backend.close() | |
context.term() | |
if __name__ == "__main__": | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'keyz' | |
import zmq | |
import time | |
import sys | |
import random | |
def main(): | |
port = "5560" | |
context = zmq.Context() | |
socket = context.socket(zmq.REP) | |
socket.connect("tcp://localhost:%s" % port) | |
server_id = random.randrange(1,10005) | |
while True: | |
# Wait for next request from client | |
message = socket.recv() | |
print "Received request: ", message | |
time.sleep (1) | |
socket.send("World from server %s" % server_id) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment