码迷,mamicode.com
首页 > 编程语言 > 详细

zeromy quick start - python

时间:2018-06-12 00:48:11      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:text   print   .so   res   expect   sts   tar   ring   local   

软件:

pip install pyzmq

 

代码:

==server.py

#  
#   Hello World server in Python  
#   Binds REP socket to tcp://*:5555  
#   Expects "Hello" from 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 10 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, "]"  )

步骤

打开一个命令行,执行python server.py

打开一个命令行,执行python client.py

 

参考:

https://blog.csdn.net/kent45/article/details/10397917

 

zeromy quick start - python

标签:text   print   .so   res   expect   sts   tar   ring   local   

原文地址:https://www.cnblogs.com/cutepig/p/9170703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!