广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python SOAP 调用
  • 211
分享到

Python SOAP 调用

PythonSOAP 2023-01-31 02:01:42 211人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

python编写SOAP服务   SOAP简介引用 简单对象访问协议(SOAP,全写为Simple Object Access Protocol)是一种标准化的通讯规范,主要用于WEB服务(web service)中。SOAP的出

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:
Python代码  收藏代码
  1. import SOAPpy  
  2. def hello():  
  3.     return "Hello World"  
  4. server = SOAPpy.SOAPServer(("localhost", 8080))  
  5. server.reGISterFunction(hello)  
  6. server.serve_forever()  


And the corresponding client:

Python代码  收藏代码
  1. import SOAPpy  
  2. server = SOAPpy.SOAPProxy("http://localhost:8080/")  
  3. 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

Python代码  收藏代码
  1. import soaplib  
  2. from soaplib.core.service import rpc, DefinitionBase  
  3. from soaplib.core.model.primitive import String, Integer  
  4. from soaplib.core.server import wsgi 
  5. from soaplib.core.service import soap
  6. from soaplib.core.model.clazz import Array  
  7.   
  8.   
  9. class HelloWorldService(DefinitionBase):  
  10.     @soap(String,Integer,_returns=Array(String))  
  11.     def say_hello(self,name,times):  
  12.         results = []  
  13.         for i in range(0,times):  
  14.             results.append('Hello, %s'%name)  
  15.         return results  
  16.   
  17. if __name__=='__main__':  
  18.     try:  
  19.         from wsgiref.simple_server import make_server  
  20.         soap_application = soaplib.core.Application([HelloWorldService], 'tns')  
  21.         wsgi_application = wsgi.Application(soap_application)  
  22.         server = make_server('localhost', 7789, wsgi_application)  
  23.         server.serve_forever()  
  24.     except ImportError:  
  25.         print "Error: example server code requires Python >= 2.5"  

SOAP Client

Python代码  收藏代码
  1. >>> from suds.client import Client  
  2. >>> hello_client = Client('http://localhost:7789/?wsdl')  
  3. >>> result = hello_client.service.say_hello("Dave", 5)  
  4. >>> print result  
  5.   
  6. (stringArray){  
  7.    string[] =  
  8.       "Hello, Dave",  
  9.       "Hello, Dave",  
  10.       "Hello, Dave",  
  11.       "Hello, Dave",  
  12.       "Hello, Dave",  
  13.  }  


  

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
Python代码  收藏代码
  1. ...  
  2.     def parse_xml_string(self, xml_string, charset=None):  
  3.         #return _parse_xml_string(x, charset)  
  4.         return _parse_xml_string(xml_string, charset)  
  5. ...  


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

用rpclib实现soap server

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

rpclib服务器端接收对象参数

一个简单例子的实现

SERVER

Python代码  收藏代码
  1. import logging  
  2. from rpclib.application import Application  
  3. from rpclib.decorator import srpc  
  4. from rpclib.interface.wsdl import Wsdl11  
  5. from rpclib.protocol.soap import Soap11  
  6. from rpclib.service import ServiceBase  
  7. from rpclib.model.complex import Iterable  
  8. from rpclib.model.primitive import Integer  
  9. from rpclib.model.primitive import String  
  10. from rpclib.server.wsgi import WsgiApplication  
  11. from rpclib.util.simple import wsgi_soap11_application  
  12.   
  13. class HelloWorldService(ServiceBase):  
  14.     @srpc(String, Integer, _returns=Iterable(String))  
  15.     def say_hello(name, times):  
  16.         ''''' 
  17.         Docstrings for service methods appear as documentation in the wsdl 
  18.         <b>what fun</b> 
  19.         @param name the name to say hello to 
  20.         @param the number of times to say hello 
  21.         @return the completed array 
  22.         '''  
  23.         print times  
  24.   
  25.         for i in xrange(times):  
  26.             yield u'Hello, %s' % name  
  27.   
  28. if __name__=='__main__':  
  29.     try:  
  30.         from wsgiref.simple_server import make_server  
  31.     except ImportError:  
  32.         print "Error: example server code requires Python >= 2.5"  
  33.   
  34.     logging.basicConfig(level=logging.DEBUG)  
  35.     logging.getLogger('rpclib.protocol.xml').setLevel(logging.DEBUG)  
  36.   
  37.     application = Application([HelloWorldService], 'rpclib.examples.hello.soap', interface=Wsdl11(), in_protocol=Soap11(), out_protocol=Soap11())  
  38.   
  39.     server = make_server('192.168.0.31', 7789, WsgiApplication(application))  
  40.   
  41.     print "listening to http://192.168.0.31:7789"  
  42.     print "wsdl is at: http://192.168.0.31:7789/?wsdl"  
  43.   
  44.     server.serve_forever()  


 

Client
Python代码  收藏代码
  1. from suds.client import Client  
  2. c = Client('http://192.168.0.31:7789/?wsdl')  
  3. a = c.service.say_hello(u'punk', 5)  
  4. print a 

--结束END--

本文标题: Python SOAP 调用

本文链接: https://www.lsjlt.com/news/186869.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python SOAP 调用
    python编写SOAP服务   SOAP简介引用 简单对象访问协议(SOAP,全写为Simple Object Access Protocol)是一种标准化的通讯规范,主要用于Web服务(web service)中。SOAP的出...
    99+
    2023-01-31
    Python SOAP
  • java soap请求怎么调用
    要调用Java SOAP请求,可以按照以下步骤进行:1. 导入所需的Java库。在Java中,可以使用JAX-WS(Java API...
    99+
    2023-09-26
    java
  • php soap 方法如何调用
    php soap 方法如何调用?php下调用soap实现对接PHP5下SOAP调用实现过程本文以某公司iPhone 6手机预约接口开发为例,介绍PHP5下SOAP调用的实现过程。一、基础概念SOAP(Simple Object Access...
    99+
    2015-01-05
    php soap
  • php的soap方法怎么调用
    本篇内容介绍了“php的soap方法怎么调用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!php soap方法调用:1、在php.ini文件...
    99+
    2023-06-21
  • android中soap协议使用(ksoap调用webservice)
    如下面代码所示: 代码如下:SoapObject request  = new SoapObject(serviceNamespace, methodName); ...
    99+
    2022-06-06
    WebService soap协议 SOAP Android
  • 如何使用SoapClient类进行PHP SOAP调用
    要使用SoapClient类进行PHP SOAP调用,可以按照以下步骤进行操作:1. 创建SoapClient对象:使用SoapCl...
    99+
    2023-09-27
    PHP
  • 开始尝试一下soap,用python访问
    实验一下天气预报Webservice服务,数据来源于中国气象局: http://www.webxml.com.cn/WebServices/WeatherWebService.asmxwsdl python的程序:如果需要库支持,下...
    99+
    2023-01-31
    soap python
  • PHP中soap怎么使用
    在PHP中,使用SOAP(简单对象访问协议)可以进行远程过程调用(RPC)和构建Web服务。以下是使用SOAP的一般步骤:1. 确保...
    99+
    2023-08-18
    PHP soap
  • python调用调用Linux命令
    如何调用Linux命令下面代码演示了调用一个shell命令, 其中,命令的输出会存储到result变量中, 而命令的返回值,则存储到exitcode中,由此可见,调用shell命令还是很方便的:import commandsexitcode...
    99+
    2023-01-31
    命令 python Linux
  • python调用golang并回调
    最近折腾python交互,也真够呛的,一连玩了好几天,被虐的不要不要的。天天各种百度,Google之间。好吧,废话少说,转入我们的正题。其实,py调用go一般的函数,只是第一道坎,正主其实是py调用go,并且go还回调py!!!网上其实这...
    99+
    2023-01-31
    回调 python golang
  • 如何在PHP中使用SOAP函数
    随着互联网的发展,Web服务变得越来越普及,并且成为了许多企业和组织之间交换数据的重要方式。SOAP(Simple Object Access Protocol)是一种基于XML的通信协议,可以在Web服务中使用。在本篇文章中,我们将介绍如...
    99+
    2023-05-18
    使用 PHP SOAP函数
  • python 调用grep
    #因为我现在还有找到在大量文件查找python实现的好方法。 #实现采用了grep的方法。 #使用了os.popen而不是subprocess中的Popen,因为前者的参数更简单 #不知subprocess中的Popen是否有更好的地方?...
    99+
    2023-01-31
    python grep
  • Python调用Mysql
     最近在学习Python,发现Python的众多类库给Python开发带来了极大的便利性。 由于项目中使用Mysql,就考虑尝试使用Python调用Mysql,方便写一些调试用的小程序代码。花了半天差了些资料,自己动手,做了个简单的demo...
    99+
    2023-01-31
    Python Mysql
  • python调用PHP
    . 调用php 方法一: import subprocess #simple caller, disguard output subprocess.call("php /path/to/my/old/script.php")...
    99+
    2023-01-31
    python PHP
  • android 调用 python
    我这里使用AS,如果使用ec开发的直接看 http://www.srplab.com/cn/index.html 官方下载的开发包 里面有demo,我下载了可以跑通; 不管是不是AS和ec,开始还是去看下CLE官网的开发...
    99+
    2023-01-31
    android python
  • Python调用DLL
    C语言中的函数默认是__cdecl调用,C++中可用__stdcall来显示声明调用,但也可以用extern “C” 用python调用dll时需要根据不同的调用约定而使用不同的函数。但是不管什么调用,最后都必须用extern “C”...
    99+
    2023-01-31
    Python DLL
  • Python调用autoit
    1. 安装pywin32模块,地址:http://sourceforge.net/projects/pywin32/  选择对应的版本下载 2.从autoit3\AutoItX下找到AutoItX3_x64.dll AutoitX.dll...
    99+
    2023-01-31
    Python autoit
  • python调用caffe
    首先需要安装caffe for python,安装过程可以参考:http://blog.csdn.net/u011961856/article/details/76557509 python 中调用caffe库函数为: import ca...
    99+
    2023-01-31
    python caffe
  • Python调用ansible2.4
    代码如下:#!/usr/bin/env python import json import shutil from collections import namedtuple from ansible.parsing.dataloade...
    99+
    2023-01-31
    Python
  • Python调用:'get_column
    在学习《Python编程快速上手》12.3.4:列字母和数字之间的转换按照书上的代码做练习,结果输出如下:ImportError: cannot import name 'get_column_letter'导入错误:不能导入'get_co...
    99+
    2023-01-31
    Python get_column
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作