Javascript 使用autobahn python发送json
我正在尝试将json内容从url widh sendMessage发送到具有的客户端Javascript 使用autobahn python发送json,javascript,python,json,twisted,autobahn,Javascript,Python,Json,Twisted,Autobahn,我正在尝试将json内容从url widh sendMessage发送到具有的客户端 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) 我得到了错误 File "myServer.py", line 63,
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)
我得到了错误
File "myServer.py", line 63, in broadcast
c.sendMessage(data)
File "/Library/Python/2.7/site-packages/autobahn-0.6.3-py2.7.egg/autobahn /websocket.py", line 2605, in sendMessage
self.sendMessageHybi(payload, binary, payload_frag_size, sync, doNotCompress)
File "/Library/Python/2.7/site-packages/autobahn-0.6.3-py2.7.egg/autobahn /websocket.py", line 2671, in sendMessageHybi
self.sendFrame(opcode = opcode, payload = payload, sync = sync, rsv = 4 if sendCompressed else 0)
File "/Library/Python/2.7/site-packages/autobahn-0.6.3-py2.7.egg/autobahn/websocket.py", line 2161, in sendFrame
raw = ''.join([chr(b0), chr(b1), el, mv, plm])
exceptions.TypeError: sequence item 4: expected string, dict found
sendMessage
接受字节字符串或unicode字符串,而不是字典。这是因为WebSocket是二进制数据和文本数据的传输。它不是结构化对象的传输
您可以发送字典的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
:
from twisted.internet import reactor
from twisted.web.client import Agent, readBody
agent = Agent(reactor)
def broadcast(self):
getting = agent.request(
b"GET", b"http://localhost:8001/json?as_text=1")
getting.addCallback(readBody)
def got(body):
for c in self.clients:
c.sendMessage(body)
getting.addCallback(got)
return getting
sendMessage
接受字节字符串或unicode字符串,而不是字典。这是因为WebSocket是二进制数据和文本数据的传输。它不是结构化对象的传输
您可以发送字典的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
:
from twisted.internet import reactor
from twisted.web.client import Agent, readBody
agent = Agent(reactor)
def broadcast(self):
getting = agent.request(
b"GET", b"http://localhost:8001/json?as_text=1")
getting.addCallback(readBody)
def got(body):
for c in self.clients:
c.sendMessage(body)
getting.addCallback(got)
return getting
sendMessage
接受字节字符串或unicode字符串,而不是字典。这是因为WebSocket是二进制数据和文本数据的传输。它不是结构化对象的传输
您可以发送字典的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
:
from twisted.internet import reactor
from twisted.web.client import Agent, readBody
agent = Agent(reactor)
def broadcast(self):
getting = agent.request(
b"GET", b"http://localhost:8001/json?as_text=1")
getting.addCallback(readBody)
def got(body):
for c in self.clients:
c.sendMessage(body)
getting.addCallback(got)
return getting
sendMessage
接受字节字符串或unicode字符串,而不是字典。这是因为WebSocket是二进制数据和文本数据的传输。它不是结构化对象的传输
您可以发送字典的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
:
from twisted.internet import reactor
from twisted.web.client import Agent, readBody
agent = Agent(reactor)
def broadcast(self):
getting = agent.request(
b"GET", b"http://localhost:8001/json?as_text=1")
getting.addCallback(readBody)
def got(body):
for c in self.clients:
c.sendMessage(body)
getting.addCallback(got)
return getting
确切地同意让·保罗的建议。。特别是使用Twisted的异步Web客户端,而不是(阻止)Python内置的东西。我从Twisted.Web.client import readBody ImportError中得到错误:不能导入名称readBody
readBody
是在Twisted 13.1.0中引入的。您可能正在使用比该版本更旧的Twisted。如果可以,请升级。否则,您可能希望使用(不太好的)twisted.web.client.getPage来代替。同意让·保罗的建议。。特别是使用Twisted的异步Web客户端,而不是(阻止)Python内置的东西。我从Twisted.Web.client import readBody ImportError中得到错误:不能导入名称readBodyreadBody
是在Twisted 13.1.0中引入的。您可能正在使用比该版本更旧的Twisted。如果可以,请升级。否则,您可能希望使用(不太好的)twisted.web.client.getPage来代替。同意让·保罗的建议。。特别是使用Twisted的异步Web客户端,而不是(阻止)Python内置的东西。我从Twisted.Web.client import readBody ImportError中得到错误:不能导入名称readBodyreadBody
是在Twisted 13.1.0中引入的。您可能正在使用比该版本更旧的Twisted。如果可以,请升级。否则,您可能希望使用(不太好的)twisted.web.client.getPage来代替。同意让·保罗的建议。。特别是使用Twisted的异步Web客户端,而不是(阻止)Python内置的东西。我从Twisted.Web.client import readBody ImportError中得到错误:不能导入名称readBodyreadBody
是在Twisted 13.1.0中引入的。您可能正在使用比该版本更旧的Twisted。如果可以,请升级。否则,您可能需要使用(不太好的)twisted.web.client.getPage
。