python webservice gzip压缩与解压缩

################################ Model.py

# -*- coding: UTF-8 -*-

__author__ = 'fanyunlei'
import gzip
from cStringIO import StringIO
import binascii
def zipString(uncom_data):
         buf =StringIO()
         f=gzip.GzipFile(mode='web',fileobj=buf)

         try:
            f.write(uncom_data)
         finally:
            f.close()
         compressed_data=buf.getvalue()
         print buf.getvalue()
         inbuffer = StringIO(compressed_data)
         f = gzip.GzipFile(mode="rb", fileobj=inbuffer)
         rdata = f.read()
         print rdata
         print binascii.hexlify(compressed_data)

         return binascii.hexlify(compressed_data)

###################################################service.py

# -*- coding: UTF-8 -*-
__author__ = 'fanyunlei'
import soaplib
import Model
from soaplib.core.util.wsgi_wrapper import run_twisted
from soaplib.core.server import wsgi
from soaplib.core.service import DefinitionBase
from soaplib.core.service import soap
from soaplib.core.model.clazz import Array
from soaplib.core.model.clazz import ClassModel
from soaplib.core.model.primitive import Integer,String
import gzip
from cStringIO import StringIO
import binascii
class C_ProbeCdrModel(ClassModel):
    __namespace__ = "C_ProbeCdrModel"
    Name=String
    Id=Integer

class HelloWorldService(DefinitionBase):


    @soap(String,_returns=String)
    def say_hello(self,name):
        if (name=='fanyunlei'):

            uncom_data='登陆成功%s'%name
            return Model.zipString(uncom_data)




        else:
            return u'该用户无权限'
    @soap(_returns=Array(String))
    def GetCdrArray(self):
        L_Result=['1','2','3']
        return L_Result
    @soap(_returns=C_ProbeCdrModel)
    def GetCdr(self):
        L_Model=C_ProbeCdrModel()
        L_Model.Name='fanyunlei'
        L_Model.Id='10086'
        return L_Model
    @soap(_returns=Array(C_ProbeCdrModel))
    def GetArrayModel(self):
        array=[]
        L_Model=C_ProbeCdrModel()
        L_Model.Name='fanyunlei'
        L_Model.Id='1001'
        array.append(L_Model)
        L_Model.Name='tonghang'
        L_Model.Id='1002'
        array.append(L_Model)
        return array





    '''if __name__=='__main__':
    soap_app=soaplib.core.Application([HelloWorldService],'tns')
    wsgi_app=wsgi.Application(soap_app)
    print 'listening on 127.0.0.1:7789'
    print 'wsdl is at: http://127.0.0.1:7789/SOAP/?wsdl'
    run_twisted(((wsgi_app,"SOAP"),),7789)'''
if __name__=='__main__':
        try:

            from wsgiref.simple_server import make_server
            print 'start....'
            soap_application=soaplib.core.Application([HelloWorldService], 'tns')
            wsgi_application=wsgi.Application(soap_application)
            server =make_server('localhost',7789,wsgi_application)
            server.serve_forever()

        except ImportError:
            print 'error'

########################################################client

# -*- coding: UTF-8 -*-
__author__ = 'fanyunlei'
from suds.client import Client
import json
import gzip
from cStringIO import StringIO
import binascii

if '__main__'==__name__:
    test=Client('http://localhost:7789/SOAP/?wsdl')
    #print test.service.say_hello('heh')
    array= test.service.say_hello('fanyunlei')
    compressed_data = binascii.a2b_hex(array)
    inbuffer = StringIO(compressed_data)
    f = gzip.GzipFile(mode="rb", fileobj=inbuffer)
    rdata = f.read()
    print rdata
    print array


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值