使用autobahn python发送json

2024-10-03 04:31:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将json内容从url widhsendmessage发送到具有的客户机。在

def broadcast(self):
  response = urllib2.urlopen('http://localhost:8001/json?as_text=1')
  data = json.load(response)

  for c in self.clients:
     c.sendMessage(data)

我知道错误了

^{pr2}$

Tags: selfjsonlocalhosthttpurl内容data客户机
1条回答
网友
1楼 · 发布于 2024-10-03 04:31:53

sendMessage接受字节字符串或unicode字符串-而不是字典。这是因为WebSockets是二进制数据和文本数据的传输。它不是结构化对象的传输。

您可以发送字典的JSON编码形式,但不能发送字典本身:

def broadcast(self):
    response = urllib2.urlopen('http://localhost:8001/json?as_text=1')

    for c in self.clients:
        c.sendMessage(response)

但是请注意,您实际上希望使用twisted.web.client-而不是阻塞urllib2

^{pr2}$

相关问题 更多 >