文章大纲

  • 序言
  • 相关概念
    • SOA
    • web services
    • SOAP
    • WSDL
    • UDDI
  • 环境搭建
    • 我们使用 python 3.6 这个较新python 版本
  • 服务端开发
  • 客户端开发
    • suds-jurko
    • suds-py3
    • 客户端样例代码
  • 参考文献

序言

web services 已经不再流行,但是,由于它的在接口技术中有着非常重要的地位,同时现在最主要的Web 服务设计模型REST其实也属于web services 技术范畴。所以我们还是有必要学习一下。

其实 Web Serive 是一项不太容易讲清楚技术。他的相关概念包括:

  1. SOA Service-Oriented Architecture
  2. Web Services
  3. SOAP (Simple Object Access Protocol)
  4. WSDL (Web Services Description Language)
  5. UDDI (Universal Description Discovery and Integration)

相关概念

web services 这套复杂的技术如上文所述已经算是过时,但了解相关概念还是必要的

SOA

Service Oriented Ambiguity 中文一般理解为,面向服务架构,简称 SOA。
SOA 的提出是在企业计算领域,就是要将紧耦合的系统,划分为面向业务的,粗粒度,松耦合,无状态的服务。

SOA 的几个关键特性

特性:

一种粗粒度、松耦合服务架构,服务之间通过简单、精确定义接口进行通讯,不涉及底层编程接口和通讯模型。

对于 SOA 来说,并不需要太过较真 SOA 到是一个怎样的架构。只要符合它的定义和规范的软件系统都可以认为是 SOA 架构。

现在几乎所有的 SOA 应用场合都是和 Web Service 绑定的,所以不免有时候这两个概念混用。不可 否认 Web Service 是现在最适合实现 SOA 的技术,SOA 的走红在很大程度上归功于 Web Service 标准的成熟和 应用普及。

web services

Web Service 详细的描述: Web Service 是一个平台独立的,低耦合的,自包含的、基于可编程的 web 的应用程序,可使用开放的 XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分 布式的互操作的应用程序。

在 Web Service 中所有的访问都通过 SOAP 访问进行,用 WSDL 定义的接口封装,通过 UDDI 进行目录查找所以SOAP、WSDL 和 UDDI 构成了 Web Service 的三要素。

以下简略介绍这三要素。

SOAP

Simple Object Access Protocol,中文为简单对象访问协议,简称 SOAP。 SOAP 是基于 XML 在分散或分布式的环境中交换信息的简单的协议。允许服务提供者和服务客户经过防 火墙在 INTERNET 进行通讯交互。

最多的情况还是还是绑定在HTTP 协议上面传输。所以,导致大多数人认为SOAP 就是HTTP + XML, 或者认为 SOAP 是 HTTP post 请求的一个专用版本,遵循一种特殊的 XML 消息格式。

WSDL

Web Services Description Language,网络服务描述语言,简称 WSDL。它是一门基于 XML 的语言,用 于描述 Web Services 以及如何对它们进行访问。

UDDI

UDDI Universal Description, Discovery and Integration",可译为“通用描述、发现与集成服务”,简称 UDDI。 WSDL 用来描述了访问特定的 Web Service 的一些相关的信息,那么在互联网上,或者是在企业的不同 部门之间,如何来发现我们所需要的 Web Service 呢?而 Web Service 提供商又如何将自己开发的 Web Serivce 公布到因特网上呢?这就需要使用到 UDDI 了。


环境搭建

python 使用简单,第三方库丰富,我们搭建好环境,进行一整套web services 程序的开发。

我们使用 python 3.6 这个较新python 版本

python 环境管理的标准为conda 标准,我们使用conda 结合pip 进行开发环境的搭建。

创建conda 环境

conda create --name Web_Services python=3.6
conda activate Web_Services

导出依赖包

pip freeze > requirements.txt
conda list -e > requirements.txt

依赖包列表

# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
certifi=2019.6.16=py36_0
lxml=4.3.4=pypi_0
pip=19.1.1=py36_0
python=3.6.8=h9f7ef89_7
pytz=2019.1=pypi_0
setuptools=41.0.1=py36_0
spyne=2.12.16=pypi_0
sqlite=3.28.0=he774522_0
suds-py3=1.3.3.0=pypi_0
vc=14.1=h0510ff6_4
vs2015_runtime=14.15.26706=h3a45250_4
wheel=0.33.4=py36_0
wincertstore=0.2=py36h7fe50ca_0

使用conda 或者pip 批量安装

pip install -r requirements.txt
conda install --yes --file requirements.txt


但是注意,async=False ,这个参数问题在3.7版本中有问题,spyne 库会有报错。因为在Python3.7里async变成了关键字,关键字是不能做变量名的,只要把这个名字改成任意不是关键字的词就好了。


服务端开发

针对Python的WebService开发,最早开发者使用最多的库是soaplib
(官方地址:http://soaplib.github.io/soaplib/2_0/index.html ),
但从其官网可知,其最新版本“soaplib-2.0.0-beta2”从2011年3月发布后就不再进行更新了。

通过阅读soaplib的官方文档,可知其不再维护后已经转向了一个新的项目:rpclib
(官方地址:http://github.com/arskom/rpclib )
进行后续开发,但在rpclib的readme中,介绍了rpclib已经更名为spyne,并将持续进行更新。

综上,所以选用spyne进行开发了。

服务端样例代码:

https://github.com/arskom/spyne/blob/master/examples/helloworld_soap.py

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
'''
@Author  :   {SEASON}
@License :   (C) Copyright 2013-2022, {OLD_IT_WANG}
@Contact :   {}
@Software:   PyCharm
@File    :   Web_Services_Test20190708 -- Web_Services
@Time    :   2019/7/11 9:03
@Desc    :'''
#-------------------------------------------------------------------------------
# !/usr/bin/env python
# -*- coding: utf-8 -*-"""
preference:http://spyne.io/docs/2.10/index.htmlhttps://github.com/arskom/spyne/blob/master/examples/helloworld_soap.pyThis is a simple HelloWorld example to show the basics of writing
a webservice using spyne, starting a server, and creating a service
client.
Here's how to call it using suds:#>>> from suds.client import Client
#>>> hello_client = Client('http://localhost:8000/?wsdl')
#>>> hello_client.service.say_hello('punk', 5)
(stringArray){string[] ="Hello, punk","Hello, punk","Hello, punk","Hello, punk","Hello, punk",}
#>>>"""
# Application is the glue between one or more service definitions, interface and protocol choices.
from spyne import Application
# @rpc decorator exposes methods as remote procedure calls
# and declares the data types it accepts and returns
from spyne import rpc
# spyne.service.ServiceBase is the base class for all service definitions.
from spyne import ServiceBase
# The names of the needed types for implementing this service should be self-explanatory.
from spyne import Iterable, Integer, Unicodefrom spyne.protocol.soap import Soap11
# Our server is going to use HTTP as transport, It’s going to wrap the Application instance.
from spyne.server.wsgi import WsgiApplication# step1: Defining a Spyne Service
class HelloWorldService(ServiceBase):@rpc(Unicode, Integer, _returns=Iterable(Unicode))def say_hello(self, 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 times: the number of times to say hello@return  When returning an iterable, you can use any type of python iterable. Here, we chose to use generators."""for i in range(times):yield u'Hello, %s' % name# step2: Glue the service definition, input and output protocols
soap_app = Application([HelloWorldService], 'spyne.examples.hello.soap',in_protocol=Soap11(validator='lxml'),out_protocol=Soap11())# step3: Wrap the Spyne application with its wsgi wrapper
wsgi_app = WsgiApplication(soap_app)if __name__ == '__main__':import loggingfrom wsgiref.simple_server import make_server# configure the python logger to show debugging outputlogging.basicConfig(level=logging.DEBUG)logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)logging.info("listening to http://127.0.0.1:8000")logging.info("wsdl is at: http://localhost:8000/?wsdl")# step4:Deploying the service using Soap via Wsgi# register the WSGI application as the handler to the wsgi server, and run the http serverserver = make_server('127.0.0.1', 8000, wsgi_app)server.serve_forever()

客户端开发

这方面有两个python 组件可以使用,分别是:

  • suds-jurko
  • suds-py3

suds-jurko

pip install suds-jurko

文档
https://bitbucket.org/jurko/suds/wiki/Original%20Documentation

suds-py3

https://github.com/cackharot/suds-py3

pip3 install suds-py3

客户端样例代码

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
'''
@Author  :   {SEASON}
@License :   (C) Copyright 2013-2022, {OLD_IT_WANG}
@Contact :   {shiter@live.cn}
@Software:   PyCharm
@File    :   Web_Services_Test20190708 -- test_client_suds-py3_eg
@Time    :   2019/7/11 23:45
@Desc    :'''
#-------------------------------------------------------------------------------import sys
import os
suds_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(suds_path)from suds.client import Clientdef set_log():import logginglogging.basicConfig(level=logging.INFO)logging.getLogger('suds.client').setLevel(logging.DEBUG)logging.getLogger('suds.transport').setLevel(logging.DEBUG)# logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)def call_service(url):client = Client(url, username='bob', password='catbob')do_call_service(client, url)def do_call_service(client, url):print("Calling: sayHello()")result = client.service.sayHello('Username')print("Result: %s" % result)a = 10.98b = 98.83print("Calling: add()")sum = client.service.add(a, b)print("Result: Sum of %f + %f = %f" % (a,b,sum))print("Calling: addDate()")from datetime import datetimeimport timeinputDate = datetime.now()dt = client.service.addDate(inputDate, 1)print("Result: %s" % dt)def test(url):client = Client(url)for p in client.sd[0].ports:for m, args in p[1]:if len(args) == 0:print(client.service[0][m]())if __name__ == '__main__':# set_log()url = 'http://localhost:8000/?wsdl'if len(sys.argv) > 1:url = sys.argv[1]call_service(url)# test('http://dati.meteotrentino.it/service.asmx?WSDL')client1 = Client("http://127.0.0.1:8181/soap/infoservice?wsdl", username='bob', password='catbob')print(client1.service.getInfo("Bob"))# client2 = Client("http://127.0.0.1:8181/soap/infoservice?wsdl", username='bob', password='catbob')# print(client2.service.getInfo("Test2"))# # client3 = Client("http://127.0.0.1:8181/soap/infoservice?wsdl", username='bob', password='catbob')# print(client3.service.getInfo("Test3"))

参考文献

1.《web 接口开发与自动化测试 基于Python语言》
2. https://www.cnblogs.com/guanfuchang/p/5985070.html

基于soap 的 python web services 服务开发指南相关推荐

  1. 基于Spring Boot应用Apache CXF发布Web Services服务

    记录:298 场景:使用Spring Boot应用Apache CXF发布Web Services服务,实现跨系统之间交互接口. 版本: JDK 1.8 Spring Boot 2.6.3 Apach ...

  2. .NET Web Services服务

    一.简介 Web Services 可使您的应用程序成为 Web 应用程序. Web Services 通过 Web 进行发布.查找和使用. 1.什么是Web Services? Web Servic ...

  3. Zend Studio13.6.1创建web services服务

    Zend Studio13.6.1创建web services服务 一.开发环境 1.1 zendstudio13.6.1 1.2 apache httpd-2.4.46-o111j-x64-vc15 ...

  4. VMware vSphere Web Services SDK编程指南(二)-Java 开发设置

    Java 开发设置 这节说明如何设置开发java客户端的开发环境. 主要包括以下内容: ■ Java 开发的各项要求 ■ java 开发设置 ■ 生成存根和编译类文件 ■ 运行 SimpleClien ...

  5. 小白都能看懂的实战教程 手把手教你Python Web全栈开发(DAY 1)

    小白都能看懂的实战教程 手把手教你Python Web全栈开发 Flask(Python Web)实战系列之在线论坛系统 第一讲 博主博客文章内容导航(实时更新) 更多优质文章推荐: 收藏!最详细的P ...

  6. 基于MVC的JavaScript Web富应用开发

    基于MVC的JavaScript Web富应用开发 (美)麦卡劳(MacCaw,A.)著 李晶,张散集译 ISBN 978-7-121-10956-0 2012年5月出版 定价:59.00 元 16开 ...

  7. PHP是脚本语言 因此不能通过浏览器运行,SQL_基于ASP技术的Web数据库应用开发

    基于ASP技术的Web数据库应用开发 第一节 Web数据库解决方案 一.Web数据库访问过程 Web数据库访问过程如下图: 可以将上述过程分为四步: 1.在浏览器地址栏输入http://mynetse ...

  8. 2015年CVTE校园招聘--Web后台服务开发工程师笔试题目及参考答案

    Web后台服务开发工程师笔试题目及参考答案 笔试时间:2014.09.14 地点: 广工 1.编程题:输入一串数字,返回查询数值的数组下标 解答:public static void main(Str ...

  9. 网络云存储技术Windows server 2012 (项目二十 基于NLB的企业Web站点服务部署)

    网络云存储技术Windows server 2012 (项目二十 基于NLB的企业Web站点服务部署) 前言 网络存储技术,是以互联网为载体实现数据的传输与存储,它采用面向网络的存储体系结构,使数据处 ...

最新文章

  1. Nature:全球表层土微生物组群落结构和功能
  2. Centos7Yum安装Mysql8
  3. 最新电子皮肤的触觉有多灵?连空气流动都能感受到
  4. Javascript获取select的选中值和选中文本
  5. (18)打鸡儿教你Vue.js
  6. JVM虚拟机总结 内存分析及调试
  7. CentOS 7 : Docker私有仓库搭建和使用
  8. MongoDB基础介绍安装与使用
  9. 精(李沐)多头注意力,代码理解
  10. 进程的描述和进程的创建
  11. 编辑距离(线性DP+暴力匹配)
  12. 谷歌代码规范的中文版
  13. Scipy安装遇到的问题
  14. android 投屏与控制,android投屏pc及电脑adb控制手机
  15. Robot Rapping Results Report CodeForces - 645D
  16. pycharm连接MySQL数据库
  17. 给文字上加中划线_Word中为字符添加上划线该怎么做
  18. 在Linux(Ubuntu)下安装Arial、Times New Roman等字体
  19. RabbitMQ 网页端控制台开启方式
  20. 解脱的生活——《心经》与人生

热门文章

  1. 设计模式——七大原则
  2. 计算机视觉课程设计:基于SSD、Dlib多进程目标检测的对比研究
  3. 分享99个中国风ppt,总有一款适合你
  4. 微信公众号之错误返回码
  5. Python 数据分析微专业课程--项目实战11 中国城市资本流动问题探索
  6. 客户端网页API(二)——从服务器获取数据
  7. Revit二次开发小技巧(十二)创建带洞口的墙
  8. 【计算机硬件组成】基础知识(必备)
  9. 高效开发:java对象转化成String类型的四种方法
  10. 妇产科护理学试题及答案(整理版)