在运行时更改suds客户端的web服务url(保留wsdl)

2024-10-17 06:28:39 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我的问题类似于this one

但有点不同。 我们拥有的是一系列的环境,具有相同的服务集。 对于某些环境(本地环境),我们可以访问wsdl,从而生成suds客户机。 对于外部环境,我们不能访问wsdl。但同样的,我希望我可以只更改URL而不重新生成客户端。 我试过克隆客户机,但没用。


编辑:添加代码:

    host='http://.../MyService.svc'
    wsdl_file = 'file://..../wsdl/MyService.wsdl'

    client = suds.client.Client(wsdl_file, location=host, cache=None)

    #client = baseclient.clone()

    #client.options.location = otherhost

    client.set_options(port='BasicHttpBinding_IMyService')

    result = client.service.IsHealthy()

这给了我一个例外:

由于EndpointDispatcher处的ContractFilter不匹配,无法在接收器处处理具有操作“http://tempuri.org/IMyService/IsHealthy”的消息。这可能是因为合同不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。

问题是,如果我将客户机直接设置为主机,它就可以正常工作: client=suds.client.client(主机)

如你所见,我试过克隆客户机,但也有同样的例外。我甚至试过:

    baseclient = suds.client.Client(host)

    client = baseclient.clone()

    client.options.location = otherhost
    ....

也有同样的例外。

有人能帮我吗?


Tags: clienthttphost客户机环境clonelocationsuds
3条回答

您可以通过指定服务的location来实现这一点。假设有一个名为Clientclient对象,可以通过更新client.options.location中的URL来修改服务位置。

另外,当通过对URL使用file://方案(例如file:///path/to/service.wsdl)来构造客户机时,可以使用WSDL文件的本地副本作为url。所以这可能是你的另一个选择。当然,您还必须指定location,以便覆盖WSDL中的默认位置。

我明白了!。 我甚至不知道我是怎么想出来的,但经过一点点猜测和运气,我最终得到了这个:

    wsdl_file = 'file://...../MyService.wsdl'

    client = suds.client.Client(wsdl_file)
    client.wsdl.url = host #this line did the trick

    client.set_options(port='BasicHttpBinding_IMyService')

    result = client.service.IsHealthy()

而且有效! 我找不到关于该属性(client.wsdl.url)的任何文档,但它可以工作,所以我会发布它,以防有人遇到同样的问题。

client.sd[0].service.setlocation(new_url)

…是“手动”方式,即per service-description

client.set_option(new_url)

…也应该有效,per the author

optionsis a wrapped/protected attr——直接编辑很可能被忽略。

相关问题 更多 >