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

时间:2010-05-14 17:42:52

标签: python wsdl suds

首先,我的问题类似于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(host)

正如您所看到的,我已尝试克隆客户端,但具有相同的异常。我甚至试过这个:

    baseclient = suds.client.Client(host)

    client = baseclient.clone()

    client.options.location = otherhost
    ....

得到了同样的例外。

任何人都可以帮助我?

3 个答案:

答案 0 :(得分:4)

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

...是“手动”的方式,即。 per service-description

client.set_option(new_url)

......也应该有效,per the author

options is a wrapped/protected attr - 直接编辑可能会被忽略。

答案 1 :(得分:3)

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

    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),但它有效,所以我发布它以防有人遇到同样的问题。

答案 2 :(得分:0)

您可以通过指定服务的location来实现此目的。假设您有一个名为Client的{​​{1}}对象,则可以通过更新client中的网址来修改服务位置。

此外,在构建客户端时,您可以使用WSDL文件的本地副本作为client.options.location,方法是使用URL的url方案,例如: file://。所以这可能是你的另一种选择。当然,您还必须指定file:///path/to/service.wsdl,以便覆盖WSDL中的默认位置。