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


python webservice gzip压缩与解压缩,布布扣,bubuko.com

python webservice gzip压缩与解压缩

上一篇:监听器第二篇【统计网站人数、自定义session扫描器、踢人小案例】


下一篇:在微信小程序里自动获得当前手机所在的经纬度并转换成地址