首页 > 解决方案 > Python / Zeep / SOAP 代理问题(我认为)

问题描述

我试图让它工作:

从业者笔记 -> NA 中用于自动化的 Python 脚本 -> 调用 NA SOAP API 的 Python 客户端

这是我的代码(稍微清理一下):

#! /usr/bin/env python3
from requests import Session
from zeep import Client
from zeep.transports import Transport

session = Session()
session.verify = False

transport = Transport(session=session)

client = Client( 'https://SERVER_FQDN/soap?wsdl=api.wsdl.wsdl2py', transport=transport)

# I added this for the network proxy
client.transport.session.proxies = {
        'http': '10.0.0.1:80',
        'https': '10.0.0.1:80',
        }

# Then found I needed this because "localhost" is hard-coded in the WSDL 
client.service._binding_options['address'] = 'https://SERVER_FQDN/soap' 

login_params = { 
            'username':'user',
            'password':'PASSWORD',
        }
loginResult = client.service.login(parameters=login_params )

sesnhdr_type = client.get_element('ns0:list_deviceInputParms')

sesnhdr = sesnhdr_type(sessionid=loginResult.Text)

devices = client.service.list_device(_soapheaders=[sesnhdr], parameters=sesnhdr)

print('\n\n ----------------------------- \n')
for i in devices.ResultSet.Row:
    print(i.hostName + ' ---> '+i.primaryIPAddress)
    params = {
            "ip":i.primaryIPAddress,
            "sessionid": loginResult.Text
            }
    device = client.service.show_deviceinfo(parameters=params)
    print(device.Text)
    print('\n\n ----------------------------- \n')

这是我的输出:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 667, in urlopen
    self._prepare_proxy(conn)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 932, in _prepare_proxy
    conn.connect()
  File "/usr/local/lib/python3.6/site-packages/urllib3/connection.py", line 317, in connect
    self._tunnel()
  File "/usr/lib64/python3.6/http/client.py", line 929, in _tunnel
    message.strip()))
OSError: Tunnel connection failed: 503 Service Unavailable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='SERVER_FQDN', port=443): Max retries exceeded with url: /soap (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 503 Service Unavailable',)))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./na-1.py", line XX, in <module>
    loginResult = client.service.login(parameters=login_params )
  File "/usr/local/lib/python3.6/site-packages/zeep/proxy.py", line 51, in __call__
    kwargs,
  File "/usr/local/lib/python3.6/site-packages/zeep/wsdl/bindings/soap.py", line 127, in send
    response = client.transport.post_xml(options["address"], envelope, http_headers)
  File "/usr/local/lib/python3.6/site-packages/zeep/transports.py", line 107, in post_xml
    return self.post(address, message, headers)
  File "/usr/local/lib/python3.6/site-packages/zeep/transports.py", line 74, in post
    address, data=message, headers=headers, timeout=self.operation_timeout
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 578, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 510, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='SERVER_FQDN', port=443): Max retries exceeded with url: /soap (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 503 Service Unavailable',)))

如果我使用“localhost”并在有问题的服务器上运行脚本,我会得到相同的错误。系统设置了代理环境值。服务器有正确的正向和反向 DNS 条目。服务器的名称和 IP 也在 /etc/hosts 中

问题是:如果我使用 IP 地址而不是服务器的 FQDN,代码就会运行。

供应商支持表示问题不在于提供端点的应用程序:

503错误表示服务不可用,有3种情况会调用此行为:1.服务器正在维护中,2.服务器超载,3.极少数情况下DNS配置错误。如果我们看到,这个问题与 NA 无关,因为请求与 IP 一起工作正常。

对此有什么想法吗?

为什么只有 IP 有效,而 FQDN 或 localhost 无效?

标签: pythonproxyzeep

解决方案


我的问题在于客户端的初始化想要继续并建立连接,但我需要在开始时设置代理。因此,我将官方文档中的两个示例拼凑在一起,以在建立连接时设置代理。

from zeep import Client
from zeep.transports import Transport
from requests import Session

session = Session()
session.proxies = {
    'http': 'http://username:password@proxy.example.com:8080',
    'https': 'http://username:password@proxy.example.com:8080'
}
transport=Transport(session=session)
client = Client(URL,transport=transport)

推荐阅读