Python/Zeep/SOAP代理问题(我认为)

标签 python proxy zeep

我正在努力让它发挥作用:

Practitioner notes -> Python scripts for automation in NA -> Python client to invoke NA SOAP APIs

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

#! /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,代码就会运行。

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

The 503 error means that the service is not available, there are 3 situations that invoke this behavior: 1. The server is under maintenance, 2. The server is overloaded, 3. In rare cases, the DNS configuration is faulty. If we see, this problem is not related to NA because the request is working fine with the IP.

对此有什么想法吗?

为什么只有 IP 有效,而 FQDN 或本地主机无效?

最佳答案

我看到的大多数关于在 Zeep 中使用代理的文档都以 client = Client(url) 开头,但如果 url 位于防火墙后面并且可以除非通过代理,否则无法访问!我尝试根据文档执行此操作,但除了超时(当然)之外什么也没做。

关键是要理解 Zeep 是基于请求构建的,并且请求可以使用代理来启动 session 。因此,您需要构建一个代理 session ,然后将该 session 传递到传输中,并使用该传输初始化 Zeep 客户端。这对我有用:


session = requests.Session()
session.auth = requests.auth.HTTPBasicAuth(soap_username, soap_password)
session.proxies = {"https": f"socks5://{settings.STATIC_PROXY}"}
transport = zeep.transports.Transport(session=session, timeout=(5, 30))
client = zeep.Client(url, transport=transport)

关于Python/Zeep/SOAP代理问题(我认为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64647550/

相关文章:

python - 从 Python 到 C, float 的精度是否发生变化?

javascript - 拦截javascript中的函数调用

Python AXL/SOAP w。啧啧。如何避免重复的字典键?

python - 在 Zeep 或 suds 库 Python 中使用 pfx 或 p12 身份验证的 Soap 调用

python - 循环队列Python实现

python - PYODBC MS Access 插入错误 - 参数太少

java - 动态代理 : how to handle nested method calls

python - 如何使用 Zeep 准备 SOAP 请求?

python - 如何处理Python中被调用库生成的弹出错误提示?

reactjs - 代理请求时发生错误 - CERT_HAS_EXPIRED