A microservice for CS361 that sends strings to client
- MUST import Zeromq library. See https://zeromq.org/get-started/ on how to import.
- Create and set variable context=zmq.Context()
- Create and set variable socket = context.socket(zmq.REQ)
- Connect to socket.connect("tcp://some_address")
- Create a list to receive data
- Create for loop in range for requests.
- In for loop, send message to to socket. socket.send(b"message here")
- Message must be 'low', 'medium', 'high' or 'perfect' else error message is sent
REQUEST EXAMPLE (Python):
import zmq
context = zmq.Context()
# Socket to talk to server
print("Connecting to server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
da_list=[]
for request in range(1):
print(f"Sending request {request} …")
socket.send(b"message")
- Inside current for loop, create and assign variable message = socket.recv()
- Since return message is a string, decode message by message=message.decode("utf-8")
- Append message to list created earlier. da_list.append(message)
RECEIVE EXAMPLE (Python):
message = socket.recv()
print(f"Received {message} ")
message=message.decode("utf-8")
da_list.append(message)
import zmq
context = zmq.Context()
# Socket to talk to server
print("Connecting to server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
da_list=[]
for request in range(1):
print(f"Sending request {request} …")
socket.send(b"message")
message = socket.recv()
print(f"Received {message} ")
message=message.decode("utf-8")
da_list.append(message)
- See https://zeromq.org/get-started/ on how to add library to IDE
- Download Server.py
- Run in seperate terminal in preferred IDE