Friday, 15 July 2011

python - zeromq: TypeError: string indices must be integers, not str -



python - zeromq: TypeError: string indices must be integers, not str -

i want found publish subscribe communication between machines.

the 2 machines, have, ryu-primary , ryu-secondary

the steps follow in each of machines follows.

in initializer ryu-primary (ip address 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 talk server print( "connection ryu-secondary..." ) self.sub_socket.connect( "tcp://192.168.241.132:%s" % self.sub_port ) def listen_zmq_connection(self): print( 'listen 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) ) homecoming string except zmq.zmqerror: break def push_messages(self,msg): self.pub_socket.send( "%s" % (msg) )

from ryu-secondary (ip address - 192.168.241.132)

in initializer

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 talk server print( "connection ryu-secondary..." ) self.sub_socket.connect( "tcp://192.168.241.131:%s" % self.sub_port ) def listen_zmq_connection(self): print( 'listen 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) ) homecoming string except zmq.zmqerror: break def push_messages(self,msg): print( 'pushing message publish socket' ) self.pub_socket.send( "%s" % (msg) )

these functions have.

i calling on ryu-secondary:

establish_zmq_connections() push_messages()

on ryu-primary, when phone call

listen_zmq_connection() recieve_messages()

after subscribing types of messages using .setsockopt( zmq.subscribe = '')

however message trying send of next type.

msg = {'in_port':in_port,'dst':dst,'actions':actions} self.push_messages(msg)

however on other side (recieve_messages() next error when this

flow_mod = recieve_messages() flow_mod['in_port'] flow_mod['dst'] flow_mod['actions'] typeerror: string indices must integers, not str

msg python dict, sending (and receiving) messages formatted strings. easiest thing serialize msg json format, send string, load received string dict again. then, , then, able access keys , values properly. should work (make sure import json somewhere above):

# on sending end msg = {'in_port':in_port,'dst':dst,'actions':actions} msg_string = json.dumps(msg) self.push_messages(msg) # on receiving end payload = receive_messages() message = json.loads(payload)

you can find total docs json module here (for python 2) , here python 3.

python ipc zeromq

No comments:

Post a Comment