Software:
Pip Install PYZMQ
Code:
==server.py
#
# Hello World Server in Python
# binds REP socket to tcp://*:5555
# expects "Hello" from the client, replies with "World"
#
Import ZMQ
Import time
Context = Zmq. Context ()
Socket = Context.socket (ZMQ. REP)
Socket.bind ("tcp://*:5555")
While True:
# Wait for next request from client
message = SOCKET.RECV ()
Print ("Received Request:", message)
# do some ' work '
Time.sleep (1) # do some ' work '
# Send reply back to client
Socket.send_string ("World")
==client.py
#
# Hello World Client in Python
# connects REQ socket to tcp://localhost:5555
# sends "Hello" to server, expects ' world ' back
#
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")
# do ten requests, waiting each time for a response
For request in range (1,10):
Print ("Sending request", request, "...")
Socket.send_string ("Hello")
# Get the reply.
message = SOCKET.RECV ()
Print ("Received reply", Request, "[", Message, "]")
Steps
Open a command line and execute the Python server.py
Open a command line and execute the Python client.py
Reference:
10397917
Zeromy Quick Start-python