【问题标题】:What is the client for spyne with json document protocol?带有 json 文档协议的 spyne 客户端是什么?
【发布时间】:2013-01-13 05:38:57
【问题描述】:

我正在尝试使用 spyne hello world example ,代码基本上是:

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()

我正在尝试使用requests 连接到它,例如:

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)

返回 404。

但是如果我使用的是HttpRpc协议,那么请求方式就可以了。

那么我如何实现一个客户端来使用 Json Document 协议。首选使用 lib requests。

【问题讨论】:

    标签: python json rpc spyne


    【解决方案1】:

    如果你想使用 JsonDocument 作为 in_protocol,你应该使用这个符号来发送数据...

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

    这意味着你应该将你的函数名作为你的json中的主键传递,它的内容应该是一个带有你函数参数的json。

    您的网址应该相同,但没有函数名称,即:

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

    如果您想查看更多内容,可以阅读 spyne 博客http://spyne.io/blog/

    【讨论】:

    • 谢谢。你说得对。我希望他们将其添加到演示代码中。
    【解决方案2】:

    我刚刚通过 JsonDocument 协议示例将请求添加到 http://spyne.io。

    查看:http://spyne.io/#inprot=JsonDocument&outprot=JsonDocument&s=rpc&tpt=WsgiApplication&validator=true

    供参考;你可以同时做:

    curl http://localhost:7789 -d '{ "say_hello" : { "name": "World", "times": 4 } }' 
    

    或

    curl http://localhost:7789 -d '{ "say_hello" : ["World", 4]}' 
    

    参数顺序与Python端的参数顺序相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多