In [4]:
#
#   Hello World client in Python
#   Connects REQ socket to tcp://localhost:5555
#   发送十次请求到服务器
#
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 10 requests, waiting each time for a response
for i in range (0,10):
    print "Sending request ", i,"..."
    socket.send (str(i))
    
    #  Get the reply.
    message = socket.recv()
    print "Received reply ", i, "[", message, "]"


Connecting to hello world server...
Sending request  0 ...
Received reply  0 [ bye ]
Sending request  1 ...
Received reply  1 [ welcome to server! ]
Sending request  2 ...
Received reply  2 [ bye ]
Sending request  3 ...
Received reply  3 [ bye ]
Sending request  4 ...
Received reply  4 [ bye ]
Sending request  5 ...
Received reply  5 [ bye ]
Sending request  6 ...
Received reply  6 [ bye ]
Sending request  7 ...
Received reply  7 [ bye ]
Sending request  8 ...
Received reply  8 [ bye ]
Sending request  9 ...
Received reply  9 [ bye ]

In [3]:


In [ ]: