py调用webservice array数组老是为空的问题
python webserbice
server
import logging
from flask import Flask
from spyne.application import Application
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
from werkzeug.serving import run_simple
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from apps import HelloWorldService, HelloWorldService2
application = Application([HelloWorldService, HelloWorldService2],
'xxxxx',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11())
wsgi_application = WsgiApplication(application)
flask_app = Flask(__name__)
application = DispatcherMiddleware(flask_app.wsgi_app, {
'xxxxx': wsgi_application,
})
if __name__ == '__main__':
host = '127.0.0.1'
port = 8902
logging.basicConfig(level=logging.DEBUG)
# 指定日志记录器的名称,设置日志记录级别
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
logging.info("wsdl is at: http://localhost:8902//xxxxx?wsdl")
run_simple(host, port, application)
然后是 apps 这是个人组织的目录架构
import json
from spyne.decorator import rpc
from spyne import ServiceBase
from spyne import Integer, Unicode, Array, ComplexModel, Iterable, String
class Person(ComplexModel):
name = Unicode
age = Integer
class PeopleResponse(ComplexModel):
name = Person
message = Unicode
# step2: 定义服务
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(String, _returns=Iterable(Unicode))
def say_hello_1(self, persons):
print(f'-------say_hello_1({persons})--------')
if not persons:
yield 'None'
persons = json.loads(persons)
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(String, _returns=Iterable(Unicode))
def say_hello_2(self, persons_array):
print('参数', persons_array)
if not persons_array:
yield 'None'
persons_array = json.loads(persons_array)
for person in persons_array:
yield person
@rpc(Person, _returns=PeopleResponse)
def say_hello_3(self, person):
if not person:
return {}
else:
# return PeopleResponse(name=People(**person))
return {
"name": person,
"message": 'name is : %s, age is %s.' % (person.name, person.age)
}
@rpc(Array(String), _returns=Iterable(String))
def say_hello_5(self, a_list):
if not a_list:
return {}
for i in a_list:
yield i
client 用zeep suds不维护了
from zeep import Client
host = '127.0.0.1'
port = 8902
# 创建zeep客户端
client = Client(f'http://{host}:{port}/xxxxx?wsdl')
# 调用say_hello方法
persons = client.service.say_hello('zhangsan', 2)
print(persons)
print('-' * 50)
# 调用接受字典作为参数的方法
pers = {"name": "张三", "age": 23} # 注意:这里使用了普通的字符串而不是Unicode字符串(在Python 3中它们是相同的)
result = client.service.say_hello_3(pers) # 如果方法期望关键字参数,可以使用**来解包字典
print(result)
print('-' * 50)
import json
persons_list = [{'name': 'zhangsan', 'age': 23}, {'name': 'lisi', 'age': 24}]
person_str_result = client.service.say_hello_1(json.dumps(persons_list)) # 假设这是正确的方法名
print(person_str_result)
print('=' * 20)
# 调用需要stringArray的方法
persons_str_list = json.dumps(['lisicvbcvb', 'zhangsan'])
person_str_result = client.service.say_hello_2(persons_str_list) # 假设这是正确的方法名
print(person_str_result)
print('=' * 20)
a_list = ['s', 'b', 'g']
fa = client.type_factory("ns0")
arr = fa.stringArray(a_list)
ress = client.service.say_hello_5( arr)
print(ress)
有个问题就是,client 传复杂结构的数据比如列表和字典 server端老是收到为空的参数
解决:
fa = client.type_factory("ns0")
arr = fa.stringArray(a_list)
要根据文档的参数类型进行一个转换 我看网上说会自动转换 但是没有转 不知道问题在哪 有大佬看到可以帮我解答一下 跪谢
本文来自博客园,作者:南鱼羁荒渡,转载请注明原文链接:https://www.cnblogs.com/nanyu/p/18337227