我正在使用webservice(用java编写) - 它基本上返回一个byte []数组(SOAP等价物是base64编码的二进制数据).
我正在使用python suds库,下面的代码在我的mac(以及windows下的cygwin)上为我工作,但解码不适用于vanilla windows(python 2.6.5).我主要是一个java开发人员,所以任何帮助都会非常有用.
from suds.client import Client import base64,os,shutil,tarfile,StringIO u = "user" p = "password" url = "https://xxxx/?wsdl" client = Client(url, username=u, password=p) bin = client.service.getTargz("test") f = open("tools.tar.gz", "w") f.write(base64.b64decode(bin.encode('ASCII'))) f.close() print "finished writing" tarfile.open("tools.tar.gz").extractall()
在Mac上工作得很好 - 但是在Windows上给了我这个错误:
C:\client>python client.py xml Getting the sysprep file from the webservice finished writing Traceback (most recent call last): File "client.py", line 28, in tarfile.open("tools.tar.gz").extractall() File "C:\Python26\lib\tarfile.py", line 1653, in open return func(name, "r", fileobj, **kwargs) File "C:\Python26\lib\tarfile.py", line 1720, in gzopen **kwargs) File "C:\Python26\lib\tarfile.py", line 1698, in taropen return cls(name, mode, fileobj, **kwargs) File "C:\Python26\lib\tarfile.py", line 1571, in __init__ self.firstmember = self.next() File "C:\Python26\lib\tarfile.py", line 2317, in next tarinfo = self.tarinfo.fromtarfile(self) File "C:\Python26\lib\tarfile.py", line 1235, in fromtarfile buf = tarfile.fileobj.read(BLOCKSIZE) File "C:\Python26\lib\gzip.py", line 219, in read self._read(readsize) File "C:\Python26\lib\gzip.py", line 271, in _read uncompress = self.decompress.decompress(buf) zlib.error: Error -3 while decompressing: invalid distance too far back
Alex Martell.. 7
尝试
f = open("tools.tar.gz", "wb")
告诉Python它是一个二进制文件是至关重要的(在Py3中,它在Unixy系统上也变得至关重要,但在Py2中它们并不严格需要它们,这就是你的代码在MacOSX上运行的原因):默认是文本,其中, Windows,\n
写入时将每个写入\r\n
磁盘的内容翻译.
尝试
f = open("tools.tar.gz", "wb")
告诉Python它是一个二进制文件是至关重要的(在Py3中,它在Unixy系统上也变得至关重要,但在Py2中它们并不严格需要它们,这就是你的代码在MacOSX上运行的原因):默认是文本,其中, Windows,\n
写入时将每个写入\r\n
磁盘的内容翻译.