python发布webservice接口

python基础

浏览数:442

2019-8-26

1.安装soaplib
下载https://github.com/soaplib/soaplib.git

cd soaplib
python setup.py install

2.服务端代码

import soaplib
from soaplib.core.service import rpc, DefinitionBase
from soaplib.core.model.primitive import String,Integer,Boolean
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import Array
from soaplib.core.service import soap
from soaplib.core.model.clazz import ClassModel 


class Rules(ClassModel):
    __namespace__ = "Rules"
    username=String
    emotion=String
class HelloWorldService(DefinitionBase):
    @soap(String,Integer,_returns=Array(String))
    def say_hello(self,name,times):
        results = []
        for i in range(0,times):
            results.append('Hello, %s'%name)
        return results
    @soap(Rules,_returns=Boolean)
    def get_recommend(self,rules):
        print rules.username
        print 111
        print rules.emotion
       

        return 1

if __name__=='__main__':
    try:
        from wsgiref.simple_server import make_server
        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: example server code requires Python >= 2.5"

发布的接口即为
http://localhost:7789/?wsdl

2.安装suds客户端测试

pip install  suds-jurko

代码

from suds.client import Client
hello_client = Client('http://localhost:7789/?wsdl')
hello_client.options.cache.clear()
rules={}
rules["username"]="alle"
rules["emotion"]="1-2-3"

print rules
result = hello_client.service.get_recommend(rules)
print result

安装suds报错

Command "python setup.py egg_info" failed with error code 2 in /tmp/pip-build-maqndg/suds-jurko/

解决:

pip install --upgrade setuptools

参考网址:
https://stackoverflow.com/questions/11425106/python-pip-install-fails-invalid-command-egg-info

参考网址
官网:http://soaplib.github.io/soaplib/2_0/index.html
实例,返回一个复杂数据:http://m.jb51.net/article/43477.htm

作者:玄月府的小妖在debug