0

我有一个用 Python(使用 xmlrpclib)编写的 XMLRPC 服务器,它定义了以下方法:

def saveFileOnServer(fileName, xmldata):
    handle = open(fileName, "wb")
    handle.write(xmldata.data)
    handle.close()
    return 0

如果我使用 Python 客户端连接并发送文件,一切正常(文件已传输):

import sys, xmlrpclib
client = xmlrpclib.Server('http://10.205.11.28:10044')
with open("resource.res", "rb") as handle:
    binary_data = xmlrpclib.Binary(handle.read())
client.saveFileOnServer("C:\\Location\\resource.res",binary_data)

但是...我必须从 TCL 脚本连接到这个 XMLRPC 服务器。我正在做以下事情:

package require XMLRPC
package require SOAP
package require rpcvar
package require http
set fd [open "resource.res" r]
fconfigure $fd -translation binary
set data [read $fd]
close $fd

XMLRPC::create ::PytharAgent::saveFileOnServer -name "saveFileOnServer" -proxy [::PytharAgent::endpoint] -params {fileName string file binary}
puts [::PytharAgent::saveFileOnServer "C:\\Location\\resource.res" $data]

由此我得到以下错误:

<class 'xml.parsers.expat.ExpatError'>:not well-formed (invalid token): line 2, column 154
    invoked from within
"$parseProc $procVarName $reply"
    (procedure "invoke2" line 17)
    invoked from within
"invoke2 $procVarName $reply"
    (procedure "::SOAP::invoke" line 25)
    invoked from within
"::SOAP::invoke ::SOAP::_PytharAgent_saveFileOnServer {C:\Location\resource.res} IxNResourceItev1.0.0.0JTYPE2\tm_versio..."
    ("eval" body line 1)
    invoked from within
"eval ::SOAP::invoke ::SOAP::_PytharAgent_saveFileOnServer $args"
    (procedure "::PytharAgent::saveFileOnServer" line 1)
    invoked from within
"::PytharAgent::saveFileOnServer "C:\\Location\\resource.res" $data"
    invoked from within
"puts [::PytharAgent::saveFileOnServer "C:\\Location\\resource.res" $data]"
    (file "test.pythat-agent.tcl" line 109)

然后,我使用 Python 代码中的二进制数据和 TCL 代码中的二进制数据,并将它们与原始文件进行比较。我在 HEX 视图中验证后发现,使用 TCL 读取的数据有原始数据加上不时的一些额外的 HEX 代码或稍微修改了一些 HEX 代码。

所以我猜这可能与 TCL 与 Python 处理二进制数据的不同方式有关。还是我在使用 TCL 阅读时做错了什么?

PS我也发现这个问题似乎与我的相似,但我不明白解决方案究竟是什么。

4

1 回答 1

1

试试这个:

package require base64
XMLRPC::create ::PytharAgent::saveFileOnServer -name "saveFileOnServer" -proxy [::PytharAgent::endpoint] -params {fileName string file base64}
puts [::PytharAgent::saveFileOnServer "C:\\Location\\resource.res" [::base64::encode $data]]]

基本上binary似乎不是 XMLRPC 的公认数据类型。

于 2014-03-14T18:29:14.480 回答