使用 autobahn python 发送 json
Send json with autobahn python
我正在尝试将 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, 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
相关文章:
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 如何:浏览器将JSON发送到服务器
- 从Javascript for jqGrid通过JSON发送服务器端对象
- 解析将数据从 JSON 发送到 Django 时出错
- 如何使用 electron-json-storage 将 json 发送回 angular
- 如何使用ajax jquery获取表单字段值并将其作为json发送到服务器
- 为什么php找不到JSON发送的索引
- 将 json 发送到(不是 REST)WCF 服务
- Angularjs$http.post-将参数作为JSON发送到ASPXwebmethod
- 序列化方向结果到 JSON 发送到我的网络服务无法正常工作
- 使用 $.post 将基本 JSON 发送到 php,并带有回调
- 有没有办法使用 XMLHttpRequest() 将 json 发送到远程 cfc
- 如何通过Postman将JSON发送到NodeJS Express Server
- 如何在java中解码由javascript使用readAsDataURL()生成并由Json发送的字符串
- 将 JSON 发送到 Google App Engine Datastore
- jQuery序列化多个表单并作为JSON发送
- 使用 JSON 发送电子邮件
- Angularjs 将空的 JSON 发送到服务器(仅在 IE 9 及更高版本中)
- 在通过json发送unix时间戳之前进行转换
- 如何使用jqGrid将JSON发送到服务器