【问题标题】:Unable to consume a .NET WCF service in Python无法在 Python 中使用 .NET WCF 服务
【发布时间】:2020-07-08 18:54:59
【问题描述】:

我正在尝试从 Python 访问 .NET WCF Web 服务并收到以下错误 - 谁能让我知道我做错了什么:

File "C:\temp\anaconda3\lib\site-packages\suds\mx\literal.py", line 87, in start
               raise TypeNotFound(content.tag)
suds.TypeNotFound: Type not found: 'authToken'

下面是我的python代码:

import uuid
from suds.client import Client
from suds.xsd.doctor import Import, ImportDoctor

url = 'http://something/something?wsdl'
imp = Import('http://www.w3.org/2001/XMLSchema', location='http://www.w3.org/2001/XMLSchema.xsd')
imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
imp = Import('http://schemas.xmlsoap.org/soap/encoding/')
imp.filter.add('http://tempuri.org/')
doctor = ImportDoctor(imp)
client = Client(url, doctor=doctor, headers={'Content-Type': 'application/soap+xml'})
logging.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)
logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)
client.set_options

myMethod = client.factory.create('myMethod')
myMethod.authToken = uuid.UUID('xxxxxxxx-35f4-4b7b-accf-yyyyyyyyyyyy')
print(f'CLIENT: {client}')
print(f'myMethod: {myMethod}')
ls_Token = client.service.myMethod(myMethod)
print(f'ACCESSTOKEN: {ls_Token}')

【问题讨论】:

    标签: python .net web-services wcf soap


    【解决方案1】:

    创建ResponseData对象,类型在wsdl中定义,如果有多个schema,需要加前缀,如ns0、ns1等

    ResponseData = client.factory.create('ns1:ResponseData')
    ResponseData.token = "Test"
    

    确保你创建的对象的属性存在,创建成功后可以查看对象的属性。

    ResponseData = client.factory.create('ns1:ResponseData')
    ResponseData.token = "Test"
    print ResponseData
    

    下图是ResponseData对象的属性:

    如果我使用下面的代码,我会得到和你一样的错误:

    ResponseData = client.factory.create('ns1:ResponseData')
    ResponseData.authToken = "Test"
    

    所以你需要检查 myMethod 对象是否有 authToken 属性。

    【讨论】:

    • 我认为这行不通。 ---- suds.MethodNotFound: Method not found: 'myMethod.WSHttpBinding_ImyMethodSingleRunExecution.__len__' 意外错误,安全恢复。 authToken 属性在 myMethod() 中
    • 您的 WCF 服务是否使用 Wshttpbinding?据我所知,suds 不支持 Wshttpbinding。您可以将服务器端绑定修改为 basichttpbinding。如果你想使用 Wshttpbinding,可以考虑 Zeep,它也是一个 Python SOAP 客户端。
    【解决方案2】:

    我们无法让它与 SOAP WCF 一起工作,必须将其修改为基于 REST 的服务才能使其工作。 Python 似乎与 REST 服务配合得更好。

    【讨论】:

    • 是的,python对SOAP服务的支持不是特别好。例如,你使用的 suds 不支持 Wshttpbinding,而 WCF Rest 服务则不同。我们可以使用Ajax、java、c#、python轻松调用WCF Rest服务,客户端只需要发起一个http请求。
    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2012-02-15
    • 2021-09-28
    • 2018-11-17
    • 2019-06-05
    相关资源
    最近更新 更多