zeromq:TypeError:字符串索引必须是整数,而不是str [英] zeromq: TypeError: string indices must be integers, not str

查看:123
本文介绍了zeromq:TypeError:字符串索引必须是整数,而不是str的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在计算机之间建立发布订阅通信.

I want to establish publish subscribe communication between to machines.

我拥有的两台机器是ryu-primaryryu-secondary

The two machines, that I have, are ryu-primary and ryu-secondary

我在每台计算机上执行的步骤如下.

The steps I follow in each of the machines are as follows.

ryu-primary的初始化程序中(IP地址为192.168.241.131)

In the initializer for ryu-primary (IP address is 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)

From ryu-secondary (IP address - 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) )

这些是我所拥有的功能.

These are the functions that I have.

我正在呼叫ryu-secondary:

establish_zmq_connections()
push_messages() 

ryu-primary上,当我致电

listen_zmq_connection()
recieve_messages() 

之后使用 .setsockopt( zmq.SUBSCRIBE = '')

after subscribing to all types of messages using .setsockopt( zmq.SUBSCRIBE = '')

但是我尝试发送的消息属于以下类型.

However the message I am trying to send is of the following type.

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

但是另一方面(recieve_messages()当我这样做时,出现以下错误

However on the other side (recieve_messages() I get the following error when I do this

flow_mod = recieve_messages() 

flow_mod['in_port']
flow_mod['dst']
flow_mod['actions']


TypeError: string indices must be integers, not str

推荐答案

msg是Python字典,但是您正在发送(和接收)格式化为字符串的消息.可能最简单的方法是将msg序列化为JSON格式,将其作为字符串发送,然后将接收到的字符串再次加载回dict中.然后,只有这样,您才能正确访问键和值.这样的事情应该起作用(确保您import json在上面的某个地方):

msg is a Python dict, but you are sending (and receiving) messages formatted as strings. Probably the easiest thing to do is to serialize msg to JSON format, send it as a string, then load the received string back into a dict again. Then, and only then, will you be able to access the keys and values properly. Something like this should work (make sure you import json somewhere above):

# on the sending end
msg = {'in_port':in_port,'dst':dst,'actions':actions}
msg_string = json.dumps(msg)
self.push_messages(msg)

# on the receiving end
payload = receive_messages()
message = json.loads(payload)

您可以在此处中找到json模块的完整文档.对于Python 2)和此处(对于Python 3).

You can find the full docs for the json module here (for Python 2) and here for Python 3.

这篇关于zeromq:TypeError:字符串索引必须是整数,而不是str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆