分享

SOAP for Python [知识库]

 henan2000 2011-11-18

SOAP for Python

SOAP: 简单对象访问协议(Simple Object Access Protocol), 是一种轻量的、简单的、基于 XML 的协议, 它被设计成在 Web 上交换结构化的和固化的信息. SOAP 可以和现存的许多因特网协议和格式结合使用, 包括 HTTP, SMTP, MIME. 它还支持从消息系统到远程过程调用(RPC)等大量的应用程序.

Python SOAP Client

SUDS

Suds is a lightweight SOAP python client for consuming Web Services.

Install

sudo easy_install suds

Links

Sample

示例调用短信发送接口,java, XFire, SOAP WSDL, webservice

soap.py
#!/usr/bin/python
#
import logging
logging.basicConfig(level=logging.INFO)
 
from suds.client import Client
from suds.sax.element import Element
 
url = 'http://localhost:8080/SendSmsService?wsdl'
client = Client(url)
 
# custum soap headers for authentication
auth = Element('AuthenticationToken') 
system = Element('system').setText('SMS')
auth.append(system)
username = Element('username').setText('sms')
auth.append(username)
password = Element('password').setText('sms')
auth.append(password)
client.set_options(soapheaders=auth)
 
client.service.sendSms('13912345678', 'python soap client through java soap server')

Python SOAP Server

SOAPLIB

Soaplib is an easy to use python library for publishing soap web services using WSDL 1.1 standard, and answering SOAP 1.1 requests. With a very small amount of code, soaplib allows you to write a useful web service and deploy it as a WSGI application. WSGI is a python web standard for writing portable, extendable web applications in python.

Install

sudo easy_install soaplib

Links

Sample

PHP SOAP Client

PHP的soap扩展不支持自定义头的认证方式,不过可以通过自定义请求XML文件的方式来实现。以下文件需要PHP的soap扩展,请先安装后再运行。

soap.php
    $wsdl = 'http://localhost:8080/services/SendSmsService?wsdl';
    $client = new SoapClient($wsdl);
 
    $reqXml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://sendSms." xmlns:ns1="http://schemas./soap/envelope/" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas./soap/envelope/">
   <SOAP-ENV:Header>
      <AuthenticationToken>
         <system>SMS</system>
         <username>sms</username>
         <password>sms</password>
      </AuthenticationToken>
   </SOAP-ENV:Header>
   <ns1:Body>
      <ns0:sendSms>
         <ns0:in0>%s</ns0:in0>
         <ns0:in1>%s</ns0:in1>
         <ns0:in2>1</ns0:in2>
      </ns0:sendSms>
   </ns1:Body>
</SOAP-ENV:Envelope>
XML;
 
    $req = sprintf($reqXml, '13912345678', 'php soap client through java soap server');
    $location = 'http://localhost:8080/services/SendSmsService';
    $action = 'sendSms';
 
    $result = $client->__doRequest($req, $location, $action, SOAP_1_1);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多