2024-10-03 04:31:53 发布
网友
我正在尝试将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)
我知道错误了
sendMessage接受字节字符串或unicode字符串-而不是字典。这是因为WebSockets是二进制数据和文本数据的传输。它不是结构化对象的传输。
sendMessage
您可以发送字典的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:
twisted.web.client
urllib2
sendMessage
接受字节字符串或unicode字符串-而不是字典。这是因为WebSockets是二进制数据和文本数据的传输。它不是结构化对象的传输。您可以发送字典的JSON编码形式,但不能发送字典本身:
但是请注意,您实际上希望使用
^{pr2}$twisted.web.client
-而不是阻塞urllib2
:相关问题 更多 >
编程相关推荐