使用json时出现UnboundLocalError 错误
本帖最后由 eveson 于 2015-01-21 11:15 编辑问题:
直接调用self.rpc({"jsonrpc": "2.0", "method": "test", "params": , "id": 1})就没有问题
如果换成变量self.rpc(recvstr)就会抛出异常
代码:def test(num):
str = {"FunId":1, "Stat":0, "param":num}
print(str)
return str
global recvstr
class MyServerProtocol(WebSocketServerProtocol):
def __init__(self):
self.rpc = JsonRpc()
self.rpc['test'] = test
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
recvstr = format(payload.decode('utf8'))
print("Text message received:",recvstr)
self.rpc(recvstr)
#self.rpc({"jsonrpc": "2.0", "method": "test", "params": , "id": 1})
## echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
错误:Text message received: {"method": "test", "params": , "id": 1, "jsonrpc": "2.0"}
Exception in callback WebSocketAdapterProtocol._consume.<locals>.process(<Future finished result=None>) at /usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py:94
handle: <Handle WebSocketAdapterProtocol._consume.<locals>.process(<Future finished result=None>) at /usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py:94>
Traceback (most recent call last):
File "/usr/local/lib/python3.4/asyncio/events.py", line 119, in _run
self._callback(*self._args)
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py", line 98, in process
self._dataReceived(data)
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1328, in _dataReceived
self.consumeData()
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1344, in consumeData
while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED:
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1503, in processData
return self.processDataHybi()
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1817, in processDataHybi
fr = self.onFrameEnd()
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 1947, in onFrameEnd
self._onMessageEnd()
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py", line 154, in _onMessageEnd
res = self.onMessageEnd()
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/websocket/protocol.py", line 743, in onMessageEnd
self._onMessage(payload, self.message_is_binary)
File "/usr/local/lib/python3.4/site-packages/autobahn-0.9.5-py3.4.egg/autobahn/asyncio/websocket.py", line 160, in _onMessage
res = self.onMessage(payload, isBinary)
File "server.py", line 46, in onMessage
self.rpc(recvstr)
File "/usr/local/lib/python3.4/site-packages/jsonrpc2-0.4.1-py3.4.egg/jsonrpc2/__init__.py", line 206, in __call__
return resdata
UnboundLocalError: local variable 'resdata' referenced before assignment 请高手给看下怎么回事,搞了一上午了,哎,python用的不熟啊 你测试的参数和运行是的参数 recvstr数据类型不是一样的
recvstr.__class___ 看下 解决了。追到库jsonrpc的源码里面,找了个他自己使用的过程看了下,原来调用rpc之前必须json.loads一下。
C语言用惯了,什么事情,总想自己推理、实现,用python还是老老实实看api,看demo
页:
[1]