博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python3 + spyn的 webservice ,分别使用python + suds-jurko 和 jmeter 接口调用
阅读量:4088 次
发布时间:2019-05-25

本文共 3604 字,大约阅读时间需要 12 分钟。

概要

本文主要关注利用spyne实现web service, 接口参数为自定义数据类型和数组

pip install spyne, suds, zeep

 

类库安装

直接使用命令行:pip install suds

报错:Traceback (most recent call last):

    File "setup.py", line 20, in <module>
      import suds
    File "/root/python-suds-0.4.1/suds/__init__.py", line 154, in <module>
      import client
  ImportError: No module named client

 

后来从网上看到了解决方案,说官网已经用另外一个库替代了这个库

pip install suds-jurko

 

此库是为了解决python访问webservice的

 

调用代码:

import suds

from suds.client import Client

 

url = "http://192.168.1.235:12581/ServiceYuYue.svc?wsdl"

client = suds.client.Client(url)

 

#getHealthyHeBei是webService提供的方法

result = client.service.getHealthyHeBei(18210409689)

 

#打印出结果

print(result)

 

>>>
import spyne, suds, zeep

>>>

spyne.__version__'2.12.14'

>>>

suds.__version__, zeep.__version__('1.3.3.0',
'1.4.1')

spyne

 ( https://github.com/arskom/spyne )

Server部分

#!/usr/bin/python
# coding:utf-8

from spyne import Application, rpc, ServiceBase

from spyne import Integer, Unicode, Array, ComplexModel, Iterable, String
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
from wsgiref.simple_server import make_server

class Person(ComplexModel):
    name = Unicode
    age = Integer

class HelloWorldService(ServiceBase):
    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello(self, name, times):
        for i in range(times):
            yield "Hello %s, It's the %s time to meet you." % (name, i + 1)

    @rpc(Array(Person), _returns=Iterable(Unicode))

    def say_hello_1(self, persons):
        print('-------say_hello_1()--------')
        if not persons:
            yield 'None'
        for person in persons:
            print('name is : %s, age is %s.' % (person.name, person.age))
            yield 'name is : %s, age is %s.' % (person.name, person.age)

class HelloWorldService2(ServiceBase):
    @rpc(Array(String), _returns=Iterable(Unicode))
    def say_hello_2(self, persons):
        if not persons:
            yield 'None'

        for person in persons:

            yield person

application = Application([HelloWorldService, HelloWorldService2],
                          'spyne.examples.hello',
                          in_protocol=Soap11(validator='lxml'),
                          out_protocol=Soap11())
wsgi_application = WsgiApplication(application)

if __name__ == '__main__':

    import logging

    host = '127.0.0.1'

    port = 8000

    logging.info("listening to http://127.0.0.1:8000")

    logging.info("wsdl is at: http://localhost:8000/?wsdl")

    server = make_server(host, port, wsgi_application)

    server.serve_forever()

 

Client部分(Suds)

#!/usr/bin/python
# coding:utf-8

from suds.client import Client

host = '127.0.0.1'

port = 8000

client = Client('http://%s:%s/?wsdl' % (host, port))

# print client

persons = client.service.say_hello('zhangsan', 2)

print persons

print '-' * 20

person = {}
person['name'] = 'zhangsan'
person['age'] = 23

persons = client.factory.create('PersonArray')

persons.Person.append(person)
persons.Person.append(person)
person = client.service.say_hello_1(persons)
print person

print '=' * 20

persons = client.factory.create('stringArray')
persons.string.append('lisi')
persons.string.append('zhangsan')
person = client.service.say_hello_2(persons)
print person

 

 

zeep - Python client

#!/usr/bin/python

# coding:utf-8

from zeep import Client

ip = '127.0.0.1'

port = 8000
client = Client("http://%s:%s/?wsdl" % (ip, port))
# print(client.wsdl.dump())

### say_hello

factory = client.type_factory("ns0")
r = client.service.say_hello('zhansgan', 3)
print(r)

### say_hello_1

factory = client.type_factory("ns0")
person = factory.Person(name='zhangsan', age=23)
persons = factory.PersonArray([person, person])
r = client.service.say_hello_1(persons)
print(r)

### say_hello_2
factory = client.type_factory("ns0")
persons = factory.stringArray(["zhansgan", "lisi"])
r = client.service.say_hello_2(persons)
print(r)
 

转载地址:http://fcuii.baihongyu.com/

你可能感兴趣的文章
O(n+k)排序:计数
查看>>
shell高级用法
查看>>
二分查找及其变种
查看>>
ip转2进制
查看>>
分析一段程序内存segment分配
查看>>
const、static、const static、static const关键字的初始化
查看>>
结构体中包裹变量实现改值
查看>>
linux下生成动态库和静态库的方法
查看>>
execv函数族
查看>>
pipe实现管道命令
查看>>
mmap相关
查看>>
signal信号相关
查看>>
线程相关
查看>>
cmakelist.demo
查看>>
使用指针访问虚函数表
查看>>
c++11使用atomic多线程同步3种方法
查看>>
使用C语言内嵌汇编实现CAS
查看>>
使用CAS实现单例模式
查看>>
玩转c++模板模板参数
查看>>
STL list sort
查看>>