用suds客户端重写URL

时间:2017-08-08 13:36:39

标签: soap wsdl suds

我们使用肥皂来访问肥皂服务。

url = 'https://example.com/scr-webservices/soap/AuthenticationService?wsdl'
client = Client(url)
client.login()

结果:

  File ".../suds/client.py", line 112, in __init__
    self.wsdl = reader.open(url)
  File ".../suds/reader.py", line 157, in open
    d = self.fn(url, self.options)
  File ".../suds/wsdl.py", line 159, in __init__
    self.build_schema()
  File ".../suds/wsdl.py", line 220, in build_schema
    self.schema = container.load(self.options)
  File ".../suds/xsd/schema.py", line 93, in load
    child.open_imports(options)
  File ".../suds/xsd/schema.py", line 305, in open_imports
    imported = imp.open(options)
  File ".../suds/xsd/sxbasic.py", line 542, in open
    result = self.download(options)
  File ".../suds/xsd/sxbasic.py", line 560, in download
    d = reader.open(url)
  File ".../suds/reader.py", line 84, in open
    d = self.download(url)
  File ".../suds/reader.py", line 100, in download
    fp = self.options.transport.open(Request(url))
  File ".../suds/transport/https.py", line 60, in open
    return  HttpTransport.open(self, request)
  File ".../suds/transport/http.py", line 105, in open
    fp = self.invoke(request, retfile=True)
  File ".../suds/transport/http.py", line 153, in invoke
    u2response = urlopener.open(u2request, timeout=tm)
  File "/usr/lib/python2.7/urllib2.py", line 429, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 447, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1201, in do_open
    r = h.getresponse(buffering=True)
  File "/usr/lib/python2.7/httplib.py", line 1136, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 453, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 417, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''
client.service.login()

如果我调试它,并认为发生这种情况:

  1. 我在Client(url)中提供的网址被suds客户端库
  2. 获取
  3. suds解析结果。它在第一个wsdl
  4. 中发现了其他wsdl位置
  5. suds希望通过访问第一个wsdl中找到的URL来获取其他wsdl文件。
  6. 这些网址已损坏。它们包含错误的协议:http而不是https。
  7. suds尝试将http与https服务器进行对话
  8. https服务器停止通信
  9. 在客户端我看到:BadStatusLine
  10. 但是如何解决这个问题呢?我只能访问客户端。这就是为什么我要求“解决”而不是解决方案: - )

1 个答案:

答案 0 :(得分:0)

我发现了一个非常脏的解决方案。

我正在临时替换urllib2.OpenerDirector.open()方法。

这样我可以修复我收到的wsdl中的损坏的URL。我更改了#34; http"到" https"。

更好的解决方案非常受欢迎: - )

第一个解决方案是提供替代传输对象。但并非所有客户端 - 服务器http连接似乎都使用此传输。

url = 'https://localhost:40443/scr-webservices/soap/AuthenticationService?wsdl'

import ssl

import mock
from suds.client import Client
from urllib2 import OpenerDirector


def rewrite_url(url):
    magic = 'http:'
    if magic in url:
        url = url.replace(magic, 'https:')
    return url


def my_open_director_open(original):
    def my_open(self, request_obj, **kwargs):
        request_obj.host = request_obj.get_host()
        request_obj._Request__original = rewrite_url(request_obj._Request__original)
        request_obj.type = 'https'
        return original(self, request_obj, **kwargs)

    return my_open


with mock.patch('ssl._create_default_https_context', ssl._create_unverified_context):
    opener_director_open_original = OpenerDirector.open
    try:
        OpenerDirector.open = my_open_director_open(opener_director_open_original)
        client = Client(url)
        print(client)
        print(client.service.remote_procedure_to_call())
    finally:
        OpenerDirector.open = opener_director_open_original