在soappy上压缩xml
我正在开发一个在Python中使用Web服务的应用程序,双方(服务器和客户端)都是用Python开发的,并使用SOAPpy作为Web服务,但是,你知道,xml太冗长了,我想压缩它,但到目前为止正如我在谷歌中搜索的那样,我找不到有用的东西。
I'm developing an application that uses webservices in python, both sides (server and client) are developed in Python and uses SOAPpy for the webservices, but, you know, the xml is too verbose, I want to compress it, but as far as I have searched in google I can't find something helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 HTTP 标头添加到 SOAPpy 调用,如下所示 此处(此示例发送cookie,但您可以将其概括为添加不同的标头)--要请求压缩,请添加标头
Accept-Encoding:gzip
。 Web 服务器(不是应用程序服务器,如 Python 中的“SOAPpy 服务器”,而是运行在其上的实际 HTTP 服务器,例如 Apache)应提供压缩并在响应中包含标头Content-Encoding: gzip
来确认(如果这不能正常工作,您必须对传输类进行子类化并自己在其中插入压缩 - 我手头没有 SOAPpy 安装来检查)。缺少的一点是,如何在进一步处理之前欺骗您的
SOAPpy.SOAPProxy
来解压有效负载 - 正确的方法是再次子类化HTTPTransport
,就像添加标头部分;在上面的 URL 中,查看data = response.read()
行并考虑检查标头(以确认内容编码为所需的 gzip)并根据需要进行解压缩。要处理gzip压缩和解压,当然可以使用zlib 模块(不是
gzip
模块,它向zlib
添加了标头元数据处理以生成和读取.gz
文件 - - 您处理的不是.gz
文件,而是使用gzip
算法压缩的流,这就是zlib
的工作)。You can add HTTP headers to SOAPpy call as shown here (this example sends cookies, but you can generalize it to add different headers) -- to request compression, add header
Accept-Encoding: gzip
. The web server (not the application server, like your "SOAPpy server" in Python, but the actual HTTP server it runs on top on, e.g. Apache) should provide the compression and have in the response a headerContent-Encoding: gzip
to confirm that (if that doesn't work properly, you'll have to subclass the transport class and insert compression there yourself -- I have no SOAPpy installation at hand to check).The missing bit is, how to trick your
SOAPpy.SOAPProxy
into decompression of the payload before further processing -- and the right approach is once again to subclassHTTPTransport
, just as for the add-header part; in the URL above, look at the linedata = response.read()
and consider checking the headers (to confirm that the content encoding is gzip as required) and decompressing as needed.To deal with gzip compression and decompression, of course, you can use the zlib module of Python's standard library (not the
gzip
module, which adds tozlib
the header metadata processing to make and read.gz
files -- you're not dealing with.gz
files but with streams compressed with thegzip
algorithm, and that'szlib
's job).