`
ice_cube
  • 浏览: 24105 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

python编写SOAP服务

 
阅读更多
SOAP简介
引用

简单对象访问协议(SOAP,全写为Simple Object Access Protocol)是一种标准化的通讯规范,主要用于Web服务(web service)中。SOAP的出现是为了简化网页服务器(Web Server)在从XML数据库中提取数据时,无需花时间去格式化页面,并能够让不同应用程序之间透过HTTP通讯协定,以XML格式互相交换彼此的数据,使其与编程语言、平台和硬件无关

参考:http://zh.wikipedia.org/wiki/SOAP

http://www.ibm.com/developerworks/cn/xml/x-sisoap/index.html

python的soap包

引用

Older libraries:
    SOAPy: Was the "best," but no longer maintained. Does not work on Python 2.5+
    ZSI: Very painful to use, and development is slow. Has a module called "SOAPpy", which is different than SOAPy (above).

"Newer" libraries:
    SUDS: Very Pythonic, and easy to create WSDL-consuming SOAP clients. Creating SOAP servers is a little bit more difficult.
    soaplib: Creating servers is easy, creating clients a little bit more challenging.
    ladon: Creating servers is much like in soaplib (using a decorator). Ladon exposes more interfaces than SOAP at the same time without extra user code needed.
    pysimplesoap: very lightweight but useful for both client and server - includes a web2py server integration that ships with web2py.


参考: http://stackoverflow.com/questions/206154/whats-the-best-soap-client-library-for-python-and-where-is-the-documentation-f

用SOAPpy编写的一个简单例子

SOAPpy包:http://pypi.python.org/pypi/SOAPpy/

A simple "Hello World" http SOAP server:
import SOAPpy
def hello():
    return "Hello World"
server = SOAPpy.SOAPServer(("localhost", 8080))
server.registerFunction(hello)
server.serve_forever()


And the corresponding client:

import SOAPpy
server = SOAPpy.SOAPProxy("http://localhost:8080/")
print server.hello()


soaplib编写soap server

选用soaplib,因为看对各包的简介,soaplib对服务器端的编写更加简单

soaplib包: http://pypi.python.org/pypi/soaplib/0.8.1

soaplib 2.0的安装

git clone git://github.com/soaplib/soaplib.git
cd soaplib
python setup.py install

参考:http://soaplib.github.com/soaplib/2_0/

soaplib2.0 和 wsgi webserver 编写的一个简单例子

Declaring a Soaplib Service

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


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

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"

SOAP Client

>>> from suds.client import Client
>>> hello_client = Client('http://localhost:7789/?wsdl')
>>> result = hello_client.service.say_hello("Dave", 5)
>>> print result

(stringArray){
   string[] =
      "Hello, Dave",
      "Hello, Dave",
      "Hello, Dave",
      "Hello, Dave",
      "Hello, Dave",
 }


  

soaplib 2.0 的一个bug

在运行上面的小例子时,服务器端报错:

......
  File "/usr/local/lib/python2.6/dist-packages/soaplib-2.0.0_beta2-py2.6.egg/soaplib/core/_base.py", line 331, in parse_xml_string
    return _parse_xml_string(xml_string, charset)
NameError: global name 'x' is not defined

修改源码包:/usr/local/lib/python2.6/dist-packages/soaplib-2.0.0_beta2-py2.6.egg/soaplib/core/_base.py

line 331
...
    def parse_xml_string(self, xml_string, charset=None):
        #return _parse_xml_string(x, charset)
        return _parse_xml_string(xml_string, charset)
...


修改后,例子可以正常运行,这么明显的错误都有,果然是2.0beta版

用rpclib实现soap server

文档:http://arskom.github.com/rpclib/

rpclib服务器端接收对象参数

一个简单例子的实现

SERVER

import logging
from rpclib.application import Application
from rpclib.decorator import srpc
from rpclib.interface.wsdl import Wsdl11
from rpclib.protocol.soap import Soap11
from rpclib.service import ServiceBase
from rpclib.model.complex import Iterable
from rpclib.model.primitive import Integer
from rpclib.model.primitive import String
from rpclib.server.wsgi import WsgiApplication
from rpclib.util.simple import wsgi_soap11_application

class HelloWorldService(ServiceBase):
    @srpc(String, Integer, _returns=Iterable(String))
    def say_hello(name, times):
        '''
        Docstrings for service methods appear as documentation in the wsdl
        <b>what fun</b>
        @param name the name to say hello to
        @param the number of times to say hello
        @return the completed array
        '''
        print times

        for i in xrange(times):
            yield u'Hello, %s' % name

if __name__=='__main__':
    try:
        from wsgiref.simple_server import make_server
    except ImportError:
        print "Error: example server code requires Python >= 2.5"

    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger('rpclib.protocol.xml').setLevel(logging.DEBUG)

    application = Application([HelloWorldService], 'rpclib.examples.hello.soap', interface=Wsdl11(), in_protocol=Soap11(), out_protocol=Soap11())

    server = make_server('192.168.0.31', 7789, WsgiApplication(application))

    print "listening to http://192.168.0.31:7789"
    print "wsdl is at: http://192.168.0.31:7789/?wsdl"

    server.serve_forever()


 

Client
from suds.client import Client
c = Client('http://192.168.0.31:7789/?wsdl')
a = c.service.say_hello(u'punk', 5)
print a

分享到:
评论

相关推荐

    Python-用Python3编写的通用文件搜索和替换工具

    用Python 3编写的通用文件搜索和替换工具

    Python研究 从新手到高手 Dive Into Python 中文版

    12.SOAP Web服务 13.单元测试 14.以测试优先为原则的编程 15.重构 16.有效编程 17.动态函数 18.性能优化"&gt;Python 从新手到高手 Dive Into Python 是为有经验的程序员编写的一本 Python 书。 1.在多个平台安装Python...

    Python编程入门经典

    第13章 使用Python编写GUI 213 13.1 Python的GUI编程工具箱 213 13.2 Tkinter简介 215 13.3 用Tkinter创建GUI 小组件 215 13.3.1 改变小组件的尺寸 215 13.3.2 配置小组件选项 216 13.3.3 使用小组件 217 13.3.4 ...

    flask-soap-server:一个带有烧瓶的简单肥皂服务器

    在这个项目中,您将拥有一个用python和javascript客户端编写的基本soap服务器。 依存关系 所有依赖项都写在requirements.txt中,您可以使用以下命令进行安装: pip install -r requirements.txt 您可能需要安装...

    Python概述(1).pdf

    Python 概述 Python 用途: Python 对操作系统服务的内置接口,使其成为编写可移植的维护操作系统的管理工具 和部件(有时也被称为 Shell 工具)的理想工具。Python 程序可以搜索文件和目录树,可 以运行其他程序,...

    用Python编写web API的教程

    自从Roy Fielding博士在2000年他的博士论文中提出REST(Representational State Transfer)风格的软件架构模式后,REST就基本上迅速取代了复杂而笨重的SOAP,成为Web API的标准了。 什么是Web API呢? 如果我们想要...

    zato:Python中的ESB,SOA,REST,API和云集成

    下一代用Python编写的ESB和应用程序服务器,并以商业友好的LGPL许可发布。 认为: 提高生产力 无痛部署,停机时间更少 对HTTP,JSON,SOAP,Redis,AMQP,IBM MQ,ZeroMQ,FTP,SQL,热部署,作业调度,统计信息,...

    Python的RPC框架Agnos.zip

    Agnos是为了让不同语言编写轻松通过提供互操作所需的绑定和隐藏所有的编程细节的方案。该项目基本上是作为服务器如SOAP,WSDL和CORBA和其他现有技术同样的目的,但需要对当前的问题简约的做法。 标签:...

    Mastering-Python-Scripting-for-System-Administrators-:Packt发行的《面向系统管理员的精通Python脚本》

    使用Python编写脚本并将其自动化以用于实际管理任务 这本书是关于什么的? Python随着时间的推移而发展,并将其功能扩展到所有可能的IT操作。 Python简单易学,但具有功能强大的库,可用于构建功能强大的Python...

    PyHP-SOAP-Scan-开源

    PyHP-SOAP-Scan 是一个用 Python 编写的简单脚本,它允许您通过 Web 界面在 HP 多功能设备(支持扫描)上执行简单的扫描任务。 HP MFD 必须是启用 SOAP 的设备(默认情况下它们处于开启状态)。

    python-atws:自动任务Web服务python模块

    atws是AutoTask SOAP网络服务API的包装器 免费软件:MIT许可证 文档: : 。 特征 Py2和Py3支持 易于编程的查询编写(无需XML) 查询结果生成器检索所有实体,而不仅仅是500个 区域发现(您只需要用户名和密码) ...

    upnp-bhunter:Burp Suite Extension对检查UPnP安全性很有用

    UPnP BHunter描述UPnP BHunter是用Python / Jython编写的Burp Suite扩展,可用作UPnP渗透测试工具,用于查找活动的UPnP服务/设备并提取相关的SOAP,Subscribe和Presentation请求(支持IPv4和IPv6),然后对其进行...

    用Python的Django框架编写从Google Adsense中获得报表的应用

    我完成了更新我们在 Neutron的实时收入统计。在我花了一周的时间完成并且更新了我们的PHP脚本之后,我最终认决定开始使用Python进行抓取,这是值得我...2.SOAP很糟糕,但它至少是一个API,Suds使SOAP好一点。我了解到S

    msbin:Microsoft 二进制 XML 格式的简单解码器(MC-NBFX、MC-NBFS)

    用 Python 编写的 Microsoft 二进制 XML 格式 (MC-NBFX) 的简单解码器。 也适用于二进制 SOAP(MC-NBFS、soap+msbin1)。 基本上,这个解码器所做的就是将二进制 XML 字节转换为dictionary 。 可以用作独立脚本: ...

    starcore_for_winuwp.3.5.0.zip

    •支持简单的web服务开发,自动生成wsdl, 支持使用c / c + +, lua, python,java,ruby,c#等语言开发web服务 •支持分布式功能,CLE将对象的定义和描述同步到客户端,提供远程调用接口,可以在客户端使用对象的属性,...

    Python基础教程(第3版)-201802出版-文字版

    久负盛名的 Python 入门经典针对 Python 3 全新升级十个出色的项目,让你尽快可以使用 Python 解决实际问题目录第 1章 快速上手:基础知识 ........................ 1 1.1 交互式解释器 .............................

    DCSOfflineChecker:检查并报告通过DCSv3服务器脱机的IDC和其他设备

    DCSOfflineChecker 检查并报告通过DCSv3服务器脱机的IDC和... 最初也是在2016年使用SOAP / XML接口用Python 2为DCSv2服务器编写的,但在2019年,它使用移植到了Python 3和DCSv3服务器。 2020年,它发布到了GitHub!

    毕业设计网站开发源码-pavanarya:帕瓦那亚

    毕业设计网站开发源码 Pavan Kumar Aryasomayajulu ...Jan 21, 1989 Visakhapatnam , ...我是一个充满激情的程序员,在构建各种企业...服务、Python Flask 中实现服务。 精通 Microsoft Sql Server 等数据库,并了解 Couchbas

    Building-RESTful-Web-Services-with-Go:Packt发行的《使用Go构建RESTful Web服务》

    关于这本书最初,基于SOAP的Web服务在XML中变得更加流行。 然后,自2012年以来,REST加快了步伐,全面吞并了SOAP。 与传统语言(如ASP.NET和Spring)相比,新一代网络语言(如Python,JavaScript(Node.js)和Go)的...

    java8stream源码-cv:我的简历/履历

    网络服务(Rest/SOAP) 4 珀尔 4 JavaServer Faces 3 C# 3 MySQL 3 经验: TDC Group/Nuuday - 奥胡斯/哥本哈根 2018 - 当前 高级 Java 开发人员 在迁移到内部 Kubernetes 集群(OpenShift/Rancher)的内部应用程序...

Global site tag (gtag.js) - Google Analytics