【发布时间】:2014-10-13 03:30:54
【问题描述】:
我想在机器之间建立发布订阅通信。
我拥有的两台机器是ryu-primary 和ryu-secondary
我在每台机器中遵循的步骤如下。
在ryu-primary的初始化器中(IP地址是192.168.241.131)
self.context = zmq.Context()
self.sub_socket = self.context.socket(zmq.SUB)
self.pub_socket = self.context.socket(zmq.PUB)
self.pub_port = 5566
self.sub_port = 5566
def establish_zmq_connection(self): # Socket to talk to server
print( "Connection to ryu-secondary..." )
self.sub_socket.connect( "tcp://192.168.241.132:%s" % self.sub_port )
def listen_zmq_connection(self):
print( 'Listen to zmq connection' )
self.pub_socket.bind( "tcp://*:%s" % self.pub_port )
def recieve_messages(self):
while True:
try:
string = self.sub_socket.recv( flags=zmq.NOBLOCK )
print( 'flow mod messages recieved {}'.format(string) )
return string
except zmq.ZMQError:
break
def push_messages(self,msg):
self.pub_socket.send( "%s" % (msg) )
来自 ryu-secondary(IP 地址 - 192.168.241.132)
在初始化器中
self.context = zmq.Context()
self.sub_socket = self.context.socket(zmq.SUB)
self.pub_socket = self.context.socket(zmq.PUB)
self.pub_port = 5566
self.sub_port = 5566
def establish_zmq_connection(self): # Socket to talk to server
print( "Connection to ryu-secondary..." )
self.sub_socket.connect( "tcp://192.168.241.131:%s" % self.sub_port )
def listen_zmq_connection(self):
print( 'Listen to zmq connection' )
self.pub_socket.bind( "tcp://*:%s" % self.pub_port )
def recieve_messages(self):
while True:
try:
string = self.sub_socket.recv( flags=zmq.NOBLOCK )
print( 'flow mod messages recieved {}'.format(string) )
return string
except zmq.ZMQError:
break
def push_messages(self,msg):
print( 'pushing message to publish socket' )
self.pub_socket.send( "%s" % (msg) )
这些是我拥有的功能。
我打电话给ryu-secondary:
establish_zmq_connections()
push_messages()
在ryu-primary,当我打电话时
listen_zmq_connection()
recieve_messages()
在使用.setsockopt( zmq.SUBSCRIBE = '')
但是我尝试发送的消息属于以下类型。
msg = {'in_port':in_port,'dst':dst,'actions':actions}
self.push_messages(msg)
但是另一方面(recieve_messages() 执行此操作时出现以下错误
flow_mod = recieve_messages()
flow_mod['in_port']
flow_mod['dst']
flow_mod['actions']
TypeError: string indices must be integers, not str
【问题讨论】:
-
@WarrenWeckesser - 我想访问该字典中包含的信息,即键 'in_port'、'dst' 和 'actions' 的值