Question: Below are three programs: ticker.py, server.py, and client.py . I need help integrating these microservices using ZeroMQ. Please modify the ticker.py script to act as
Below are three programs: ticker.py, server.py, and client.py. I need help integrating these microservices using ZeroMQ. Please modify the ticker.py script to act as a client that sends requests to the server.py script, which would act as the server that responds to those requests.
ticker.py
import sys import tkinter
companies = [ None, "Apple Inc.", "Microsoft Corp.", "Amazon.com Inc.", "Facebook Inc.", "Berkshire Hathaway Inc.", "Google (aka Alphabet Inc.)" ]
tickers = [ None, "AAPL", "MSFT", "AMZN", "FB", "BRK", "GOOG" ] root = tkinter.Tk() root.title("Stock ticker symbols")
#labels
compLabel = tkinter.Label(text = "Choose company name:", anchor = "w",padx = 20) compLabel.grid(row = 0, column = 0)
mytikLabel = tkinter.Label(text = "Ticker:", anchor = "e",padx = 5) mytikLabel.grid(row = 2, column = 0)
tikLabel = tkinter.Label(anchor = "e", pady = 10, padx = 40, bg = "white", fg = "green", font = 20) tikLabel.grid(row = 2, column = 1, sticky = "e")
#Variables:
com = tkinter.StringVar(root) com.set(companies[1]) #default value
#menu
def erase(companies): tikLabel["text"] = "" compMenu = tkinter.OptionMenu(root, com, *companies[1:], command = erase) compMenu.grid(row = 0, column = 1, sticky = "ew")
#button def fetchticker(): select = com.get() for ticker in tickers: if select == companies[1]: tik = tickers[1] elif select == companies[2]: tik = tickers[2] elif select == companies[3]: tik = tickers[3] elif select == companies[3]: tik = tickers[3] elif select == companies[4]: tik = tickers[4] elif select == companies[5]: tik = tickers[5] elif select == companies[6]: tik = tickers[6]
tikLabel["text"] = tik
button = tkinter.Button(root, text = "Fetch ticker symbol", anchor = "e", padx = 20, pady = 5, command = fetchticker, bg = "red", fg="white") button.grid(row = 1, column = 1, sticky = "e")
tkinter.mainloop()
client.py
import zmq
context = zmq.Context()
# Socket to talk to server print("Connecting to hello world server...") socket = context.socket(zmq.REQ) socket.connect("tcp://localhost:5555") print(f"Current libzmq version is {zmq.zmq_version()}") print(f"Current pyzmq version is {zmq.__version__}") # Do 10 requests, waiting each time for a response for request in range(1): print(f"Sending request {request} ...") socket.send_string("GOOG")
# Get the reply. message = socket.recv() print(f"Received reply {request} [ {message} ]")
server.py
import zmq import time
context = zmq.Context() socket = context.socket(zmq.REP) socket.bind("tcp://*:5555")
def get_company_name(ticker_symbol): ticker_to_company = {"AAPL": "Apple Inc.", "GOOG": "Alphabet Inc.", "MSFT": "Microsoft Corporation"} return ticker_to_company.get(ticker_symbol, "Unknown")
while True: ticker_symbol = socket.recv().decode()
time.sleep(1)
company_name = get_company_name(ticker_symbol)
socket.send(company_name.encode())
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
