【问题标题】:XML-RPC message format troubleXML-RPC 消息格式问题
【发布时间】:2012-02-08 13:10:22
【问题描述】:

我有一个接受一些 XMLRPC 调用的 WS (ZOPE/PLONE)。

所以,我编写了一个 python sn-p 代码来调用 WS 并做一些事情。

我遵循我找到的 here 的消息格式,这是我的 sn-p 代码:

import httplib

def queryInventory():
    try:
        xmlrpc_envelope = '<?xml version="1.0"?>'\
                        '<methodCall>'\
                        '<methodName>easyram</methodName>'\
                          '<params>'\
                            '<param>'\
                              '<value>%s</value>'\
                            '</param>'\
                          '</params>'\
                        '</methodCall>'        

        params = '<EasyRAM>'\
             '<authentication><user>EasyRAM</user><pwd>EasyRAM</pwd><hotel>52</hotel></authentication>'\
             '<operation type="QueryInventory" rate="master"><date from="2012-03-10" to="2012-03-10" /><date from="2012-03-22" to="2012-03-22" /></operation>'\
             '</EasyRAM>'

        data = xmlrpc_envelope % params
        print data
        headers = {"Content-type": "text/xml"}
        conn = httplib.HTTPSConnection('myHost')
        aa = '/ws/xmlrpc/public/EasyRAM'
        conn.request("POST", aa, data, headers)
        response = conn.getresponse()
        print "EasyRAM.queryInventory() response: status=%s, reason=%s" % (response.status, response.reason)
        print "EasyRAM.queryInventory() response=%s" % response.read()
        conn.close()
    except Exception, ss:
        print "EasyRAM.queryInventory() -> Error=%s" % ss
        raise

    return ''

queryInventory()

问题是我收到以下错误消息:

无效请求 请求中省略了参数 params 。确保指定所有必需的参数,然后重试请求。

好像参数没有传递。

如果我通过以这种方式将我的参数(称为params)包装到&lt;string&gt;&lt;/string&gt; 来修改我的 sn-p:

 xmlrpc_envelope = '<?xml version="1.0"?>'\
                        '<methodCall>'\
                        '<methodName>easyram</methodName>'\
                          '<params>'\
                            '<param>'\
                              '<value><string>%s</string></value>'\
                            '</param>'\
                          '</params>'\
                        '</methodCall>'     

有些事情发生了,但不是我想要的;事实上,我的参数结果是空的(如果你愿意,也可以是 void)。

有什么想法或建议吗?

PS.:我知道存在一个名为 xmlrpclib 的用于 python 的 xml-rpc 库,但我必须以这种方式开发,因为这是一个不能直接使用这样的库的客户端示例

【问题讨论】:

    标签: python web-services xml-rpc


    【解决方案1】:

    我刚刚解决了。

    如果我添加这样的函数:

    def escape(s, replace=string.replace):
    s = replace(s, "&", "&amp;")
    s = replace(s, "<", "&lt;")
    return replace(s, ">", "&gt;",)
    

    在调用 connection 方法之前,我会执行以下操作:

    params = escape(params)
    

    然后一切顺利。

    希望对未来有用

    【讨论】:

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