Search code examples
pythonjsonrpcspyne

What is the client for spyne with json document protocol?


I'm trying with the spyne hello world example , the codes basicly are:

class HelloWorldService(ServiceBase):
    @srpc(Unicode, Integer, _returns=Array(Unicode))
    def say_hello(name, times):
        for i in range(times):
            yield 'Hello, %s' % name

application = Application([HelloWorldService],
              tns='spyne.examples.hello',
              in_protocol=JsonDocument(validator='soft'),
              out_protocol=JsonDocument()
          )

if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    wsgi_app = WsgiApplication(application)
    server = make_server('0.0.0.0', 7789, wsgi_app)
    print "rpc server start"
    server.serve_forever()

And I'm trying to connect to it with requests like:

url = "http://127.0.0.1:7789/sayhello"
data = { "name": "World", "times": 4 }
headers = { 'content-type': 'application/json' }
r = requests.post(url, data=json.dumps(data), headers=headers)

It returns with 404.

But if I'm using HttpRpc protocol, the requests way is fine.

So how do I implement a client to use the Json Document protocol. Using the lib requests is preferred.


Solution

  • If you want to use JsonDocument as in_protocol, you should use this notation for send data...

    data = { "say_hello" : { "name": "World", "times": 4 } }
    

    It means that you should pass your function name as the main key in your json, and its content should be a json with the arguments of your function.

    and your url should be the same, but without the function name i.e:

    url = "http://127.0.0.1:7789/"
    

    if you want to check more stuff, could read the spyne blog at http://spyne.io/blog/