Created
June 5, 2020 03:36
-
-
Save timothymugayi/8ab51b7b146b3c0b7132643698285f61 to your computer and use it in GitHub Desktop.
This file contains 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
# ZeroMQ basic client Python | |
# Connects REQ socket to tcp://localhost:5555 | |
# Sends "string" to server, expects "Ok" back | |
import zmq | |
from string import ascii_letters, digits | |
from random import choice | |
context = zmq.Context() | |
# Socket to talk to server | |
print("Connecting to hello world server…") | |
socket = context.socket(zmq.REQ) | |
socket.connect("tcp://localhost:5555") | |
# Do 10 requests, waiting each time for a response | |
for request in range(10): | |
print("Sending request %s …" % request) | |
large_string = ''.join([choice(ascii_letters + digits) for i in range(10485)]) | |
# Send string to our socket server listening on localhost port 5555 | |
# alternatively you have other send options e.g | |
# socket.send(binary) | |
# socket.send_json(data) | |
# socket.send_multipart(data) | |
# socket.send_pyobj(obj) Send a Python object as a message using pickle to serialize | |
socket.send_string(large_string) | |
# Get the reply from our socket server if message acknowledged. | |
message = socket.recv() | |
print("Received reply %s [ %s ]" % (request, message)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment