简体   繁体   English

403通过Python SUDS检索WSDL时

[英]403 when retrieving a WSDL via Python SUDS

I can't seem to get SUDS to download a WSDL that requires Basic auth credentials. 我似乎无法让SUDS下载需要基本身份验证凭证的WSDL。 My code is simple: 我的代码很简单:

wsdl_url = 'https://example.com/ChangeRequest.do?WSDL'
self.client = Client(wsdl_url, username=username, password=password)

I've also tried: 我也尝试过:

from suds.transport.https import HttpAuthenticated

wsdl_url = 'https://example.com/ChangeRequest.do?WSDL'
credentials = dict(username=username, password=password)
t = HttpAuthenticated(**credentials)
self.client = Client(url=wsdl_url, transport=t)

In both cases, the service returns a 403 Forbidden error. 在这两种情况下,服务均返回403禁止错误。 I can go down into the SUDS code in http.py and add this line to the call: 我可以进入http.py中的SUDS代码,并将此行添加到调用中:

u2request.add_header('Authorization','Basic xxxxxxxxxxxxxxxxxxxx')

This works. 这可行。 What am I doing wrong to get SUDS to pass my credentials when downloading the WSDL? 在下载WSDL时让SUDS通过我的凭据,我在做什么错?

Note: I try to connect to the WSDL directly using both Chrome's Postman plugin and SoapUI, and the service works as well. 注意:我尝试使用Chrome的Postman插件和SoapUI直接连接到WSDL,并且该服务也能正常工作。 So I know the credentials are correct. 因此,我知道凭据是正确的。

I used to have this issue before and compare with the soap UI header. 我以前曾经遇到过这个问题,并与soap UI标头进行比较。 Found that suds missing to include the header (Host). 发现该肥皂泡缺少标题(主机)。

client.set_options(headers={'Host': 'value'})

And issue fixed. 并解决问题。

I encountered a similar issue (suds v0.4, wsdl, 403), and found out that it was because the server I'm trying to access blocks any requests with the header User-Agent set like Python-urllib* (suds is using urllib2, hence the default header). 我遇到了类似的问题(suds v0.4,wsdl,403),发现这是因为我要访问的服务器阻塞了标头为User-Agent任何请求,如Python-urllib* (suds正在使用urllib2,因此是默认标头)。 Explicitly change the header solves the issue. 显式更改标题可以解决此问题。

Particular to my solution: I overrode the open method of a transport class, and set client options, like the following code snippet. 特定于我的解决方案:我覆盖了传输类的open方法,并设置了客户端选项,如以下代码片段所示。 Note that we need to explicitly set for open and subsequent requests separately. 请注意,我们需要分别为打开的请求和后续请求分别设置显式设置。 Please advice better ways to circumvent this if you know any. 如果知道的话,请提出更好的方法来规避这一点。 And hope this post could help save someone's time in the future. 希望这篇文章可以帮助您节省以后的时间。

import urllib2
import suds
from suds.transport.https import HttpAuthenticated
from suds.transport import TransportError

URL = 'https://example.com/ChangeRequest.do?WSDL'

class HttpHeaderModify(HttpAuthenticated):
    def open(self, request):
        try:
            url = request.url
            u2request = urllib2.Request(url, headers={'User-Agent': 'Mozilla'})
            self.proxy = self.options.proxy
            return self.u2open(u2request)
        except urllib2.HTTPError, e:
            raise TransportError(str(e), e.code, e.fp)

transport = HttpHeaderModify()
client = Client(URL, transport=transport, timeout=10)

# Subsequent requests' header needs to be set again here. The overridden transport
# class only handles opening of the client.
client.set_options(headers={'User-Agent': 'Mozilla'})

PS Though my problem may not be the same, searching for "403 suds" pops up this SO question, so I decide just post my solution here. PS虽然我的问题可能不尽相同,但搜索“ 403肥皂水”会弹出此SO问题,因此我决定将解决方案发布在这里。

reference post that gave me the right direction: https://bitbucket.org/jurko/suds/issues/27/client-request-for-wsdl-does-not-use-given 给我正确方向的参考文章: https : //bitbucket.org/jurko/suds/issues/27/client-request-for-wsdl-does-not-use-given

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM