Request/Response
Server:
import zmq
context = zmq.Context()
socket = context.socket (zmq.REP)
socket .bind ("tcp://*: 5555" )
while True:
message = socket .recv ()
print "Received request: " , message
socket .send ('Message from server...received' )
Client:
import zmq
context = zmq.Context()
socket = context.socket (zmq.REQ)
socket .connect ("tcp://localhost:5555" )
print "Sending request ..."
socket .send ("Hello" )
message = socket .recv ()
print message
Single Part
Publish server:
import time
from random import choice
from random import randrange
import zmq
stock_symbols = [u'谷歌' , 'RAX' , 'EMC' , 'GOOG' , 'AAPL' , 'RHAT' , 'AMZN' ]
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5555" )
while True :
time.sleep(0.5 )
stock_symbol = choice(stock_symbols).encode('utf-8' )
stock_price = str(randrange(1 , 100 )).encode('utf-8' )
msg = "{0} ${1}" .format(stock_symbol, stock_price)
print "Sending Message: {0}" .format(msg).decode('utf-8' )
socket.send(msg)
Client:
import zmq
topic = [u'谷歌' , "GOOG" , "RAX" ]
context = zmq.Context()
socket = context.socket(zmq.SUB)
for top in topic:
socket.setsockopt(zmq.SUBSCRIBE, top.encode('utf-8' ))
socket.connect("tcp://127.0.0.1:5555" )
while True :
msg = socket.recv()
print msg.decode('utf-8' )
Multiple Parts
Publish server:
import time
from random import choice
from random import randrange
import zmq
stock_symbols = [u'谷歌' , 'RAX' , 'EMC' , 'GOOG' , 'AAPL' , 'RHAT' , 'AMZN' ]
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5555" )
while True :
time.sleep(0.5 )
stock_symbol = choice(stock_symbols).encode('utf-8' )
stock_price = str(randrange(1 , 100 )).encode('utf-8' )
msg = "{0} ${1}" .format(stock_symbol, stock_price)
print "Sending Message: {0}" .format(msg).decode('utf-8' )
socket.send_multipart([stock_symbol, stock_price])
Client:
import zmq
topic = [u'谷歌' , "GOOG" , "RAX" ]
context = zmq.Context()
socket = context.socket(zmq.SUB)
for top in topic:
socket.setsockopt(zmq.SUBSCRIBE, top.encode('utf-8' ))
socket.connect("tcp://127.0.0.1:5555" )
while True :
topic, msg = socket.recv_multipart()
print topic.decode('utf-8' ), '$' +msg.decode('utf-8' )
Json
Publish Server:
import time
from random import choice
from random import randrange
import zmq
import json
stock_symbols = [u'谷歌' , 'RAX' , 'EMC' , 'GOOG' , 'AAPL' , 'RHAT' , 'AMZN' ]
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5555" )
while True :
time.sleep(0.5 )
stock_symbol = choice(stock_symbols).encode('utf-8' )
stock_price = str(randrange(1 , 100 )).encode('utf-8' )
msg = {'Name' : stock_symbol, 'Price' : stock_price}
print "Sending Message: {0}" .format(msg).decode('utf-8' )
socket.send_json(msg)
Client:
import zmq
context = zmq.Context()
socket = context.socket (zmq.SUB)
socket .setsockopt (zmq.SUBSCRIBE, '' )
socket .connect ("tcp://127.0.0.1:5555" )
while True:
msg = socket .recv_json()
print msg
Multiple Parts 带Json,可过滤
Publish Server:
import time
from random import choice
from random import randrange
import zmq
import json
stock_symbols = [u'谷歌' , 'RAX' , 'EMC' , 'GOOG' , 'AAPL' , 'RHAT' , 'AMZN' ]
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5555" )
while True :
time.sleep(0.5 )
stock_symbol = choice(stock_symbols).encode('utf-8' )
stock_price = str(randrange(1 , 100 )).encode('utf-8' )
msg = {'Name' : stock_symbol, 'Price' : stock_price}
print "Sending Message: {0}" .format(msg).decode('utf-8' )
socket.send_multipart([stock_symbol, json.dumps(msg)])
Client:
import zmq, json
topic = [u'谷歌' , "GOOG" , "RAX" ]
context = zmq.Context()
socket = context.socket(zmq.SUB)
for top in topic:
socket.setsockopt(zmq.SUBSCRIBE, top.encode('utf-8' ))
socket.connect("tcp://127.0.0.1:5555" )
while True :
topic, msg = socket.recv_multipart()
print json.loads(msg)
转自:http://www.giantflyingsaucer.com/blog/?p=4967