ZeroMQ学习使用
操作系统:CentOS 6.0
系统内核:linux 2.6.32
ZeroMQ版本:
1. 安装
安装ZeroMQ:
到http://www.zeromq.org/intro:get-the-software下载最新的源码包
到http://www.zeromq.org/intro:get-the-software下载最新的源码包
$wget http://download.zeromq.org/zeromq-3.2.2.tar.gz$tar zxvf zeromq-3.2.2.tar.gz $ cd zeromq-3.2.2$./configure & make#make install#ldconfig # 刷新库
准备python
#yum install python-devel python-setuptools #ubuntu 下是sudo apt-get install python-dev python-setuptools
安装python的支持库pyzmq
# pip install pyzmq
2.测试使用
sender端
1 #encoding=utf-8 2 # Hello World client in Python 3 # Connects REQ socket to tcp://localhost:5555 4 # Sends "Hello" to server, expects "World" back 5 # 6 import zmq 7 8 context = zmq.Context() 9 10 #欢迎信息 11 print "正在连接hello world服务器" 12 socket = context.socket(zmq.REQ) #socket 模式通讯 13 socket.connect ("tcp://localhost:5555") # 连接到本地的5555端口 14 15 #发出10次请求,每次等待服务器的一个响应 16 for request in range (1,10): 17 print "客户端发送请求", request,"..." 18 socket.send ("李剑,你好吗?") 19 20 #获取服务器响应 21 message = socket.recv() 22 print "客户端收到回复", request, "[", message, "]"
receiver端:
1 #encoding=utf-8 2 # Hello World server in Python 3 # Binds REP socket to tcp://*:5555 4 # Expects "Hello" from client, replies with "World" 5 # 6 import zmq 7 import time 8 9 context = zmq.Context() 10 socket = context.socket(zmq.REP) #socket模式通讯 11 socket.bind("tcp://*:5555") #监听5555端口,等待请求 12 13 while True: 14 #等待从客户端发送的请求 15 message = socket.recv() 16 print "服务器收到请求: ", message 17 18 #干点啥,鳖喜欢睡,所以睡会 19 time.sleep (1) 20 21 #向客户端发送请求 22 socket.send("李剑不好:(")
运行就可以 了
注:使用Czmq时,添加share library:
Add /usr/local/lib to /etc/ld.so.conf then run ldconfig
A web serach for ld.so.conf or ldconfig should turn up plently of info.
Add /usr/local/lib to /etc/ld.so.conf then run ldconfig
A web serach for ld.so.conf or ldconfig should turn up plently of info.
版权声明:本文为hustlijian原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。