如何使用JavaScript客户端和Python服务器调试txJSON-Rpc调用?
我做了一个wxPython的应用程序,里面还嵌入了一个Twisted的txJSONrpc服务器。这就是我的RPC“服务器”;我可以通过TCP套接字来调用它并发送命令。
在txjsonrpc里有一个叫client_subhandled.py的Python测试脚本,我用它来测试,结果可以成功调用RPC服务器并收到回应,所以Python的客户端和服务器之间的通信是可以正常工作的。
不过,我需要从JavaScript发起JSON RPC调用,而不是用Python。为此,我使用了一个小的Java小程序,它可以让你从JavaScript打开一个TCP套接字,并进行读写操作(java_socket_bridge.js)。这个方法也有效,我测试过,不是用JSON RPC协议,而是用一个简单的Twisted回声协议发送和接收纯文本。
问题是,当我用JavaScript作为客户端时,似乎无法让RPC JSON调用正常工作。有没有办法在txJSONrpc中调试接收到的JSON RPC调用?我希望能看到服务器接收到的JSON对象,以检查它们是否符合要求。
谢谢!
from twisted.internet import wxreactor # socket library
wxreactor.install() # for using twisted along with wxPython
# using netstring TCP protocol
from txjsonrpc.netstring import jsonrpc
from twisted.web import server
# import twisted reactor *only after* installing wxreactor
from twisted.internet import reactor
myreactor = reactor
def register(application):
# initialise and run the TWISTED reactor
reactor.registerWxApp(application)
#rpcCom.myreactor.listenTCP(9000, rpcCom.EchoServerFactory())
reactor.listenTCP(9000, factory)
reactor.run()
class Example(jsonrpc.JSONRPC):
"""An example object to be published."""
def jsonrpc_echo(self, x):
"""Return all passed args."""
print "echo called"
return x
class Testing(jsonrpc.JSONRPC):
def jsonrpc_getList(self):
"""Return a list."""
return [1,2,3,4,'a','b','c','d']
class Math(jsonrpc.JSONRPC):
"""
An example object to be published.
"""
def jsonrpc_add(self, a, b):
"""
Return sum of arguments.
"""
return a + b
factory = jsonrpc.RPCFactory(Example)
factory.putSubHandler('math', Math)
factory.putSubHandler('testing', Testing)
factory.addIntrospection()