您的位置:首页 > 运维架构 > Linux

linux下安装zeromq及其测试

2014-06-15 17:48 477 查看
linux下安装zeromq及其测试

1. 在ubuntu上安装zeromq以及python组件

        1、在http://www.zeromq.org/area:download页面下载最新的zeromq 2.1.7.

               wget http://download.zeromq.org/zeromq-2.1.7.tar.gz

       2、安装相关软件

             $ sudo apt-get install libtool autoconf automake

             $ sudo apt-get install uuid-dev g++

             $ sudo apt-get install python-dev

       3、编译安装zmq

             $ ./configure

             $ make

             $ sudo make install

             $ sudo ldconfig

       4、pyzmq

             在http://www.zeromq.org/bindings:python下载。

             $ python setup.py build_ext --inplace

             $ python setup.py test

             $ sudo python setup.py install

       5、测试

            $ python -c "import zmq"
          通过就可以了。
 

2. 测试

     下面写一个客户端和服务器端通信的例子。
     client_mq.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")

# Do 10 requests, waiting each time for a response

for request in range (1,10):
print "Sending request", request, "..."
socket.send("Hello")

#Get the reply
message = socket.recv()
print "Receives reply" , request, "[", message, "]"
     
     server_mq.py的代码:
    
import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
count = 0

while True:
# Wait for next request from client
message = socket.recv()
count += 1
print "Receive request: ",message,count

#do some 'work'
time.sleep(1)

socket.send("World")


测试结果如下:
chenglin@chenglin-ubuntu1:~/zeromq$ python client_zmq.py
COnnecting to hello world server...
Sending request 1 ...
Receives reply 1 [ World ]
Sending request 2 ...
Receives reply 2 [ World ]
Sending request 3 ...
Receives reply 3 [ World ]
Sending request 4 ...
Receives reply 4 [ World ]
Sending request 5 ...
Receives reply 5 [ World ]
Sending request 6 ...
Receives reply 6 [ World ]
Sending request 7 ...
Receives reply 7 [ World ]
Sending request 8 ...
Receives reply 8 [ World ]
Sending request 9 ...
Receives reply 9 [ World ]

chenglin@chenglin-ubuntu1:~/zeromq$ python server_zmq.py
Receive request:  Hello 1
Receive request:  Hello 2
Receive request:  Hello 3
Receive request:  Hello 4
Receive request:  Hello 5
Receive request:  Hello 6
Receive request:  Hello 7
Receive request:  Hello 8
Receive request:  Hello 9


参考资料: 
  1.  http://zguide.zeromq.org/page:all
  2.  http://www.firefoxbug.com/?p=2755
              
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: