带有zeep的python SOAP,requests.exception:403客户端错误
Posted
技术标签:
【中文标题】带有zeep的python SOAP,requests.exception:403客户端错误【英文标题】:python SOAP with zeep, requests.exception: 403 client error 【发布时间】:2018-05-11 14:39:06 【问题描述】:我正在尝试使用zeep
在python 中创建一个SOAP 客户端@到目前为止,我试图让它运行的运气为零。我不断收到以下错误。
Traceback (most recent call last):
File "C:/Users/z905/PycharmProjects/Soap_Test/soap_test.py", line 14, in <module>
client = Client(wsdl=client_location, transport=transport_with_basic_auth)
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\zeep\client.py", line 134, in __init__
self.wsdl = Document(wsdl, self.transport, strict=strict)
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\zeep\wsdl\wsdl.py", line 81, in __init__
root_definitions = Definition(self, document, self.location)
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\zeep\wsdl\wsdl.py", line 178, in __init__
self.parse_imports(doc)
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\zeep\wsdl\wsdl.py", line 270, in parse_imports
document = self.wsdl._get_xml_document(location)
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\zeep\wsdl\wsdl.py", line 140, in _get_xml_document
location, self.transport, self.location, strict=self.strict)
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\zeep\loader.py", line 72, in load_external
content = transport.load(url)
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\zeep\transports.py", line 110, in load
content = self._load_remote_data(url)
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\zeep\transports.py", line 126, in _load_remote_data
response.raise_for_status()
File "C:\Users\z905\PycharmProjects\Soap_Test\venv\lib\site-packages\requests\models.py", line 935, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden (Forbidden port) for url: http://url:7104
我已经尝试过使用和不使用基本身份验证的程序。
from zeep import Client
from requests import Session
from requests.auth import HTTPBasicAuth
from zeep.transports import Transport
client_location = "file://C:/<path-to-local-file>"
loc = "<url-to-wsdl>"
session = Session()
session.auth = HTTPBasicAuth('usrname', 'pass')
transport_with_basic_auth = Transport(session=session)
client = Client(wsdl=client_location, transport=transport_with_basic_auth)
以及没有基本身份验证的版本。
from zeep import Client
from zeep.wsse.username import UsernameToken
client_location = "file://C:/<path-to-local-file>"
loc = "<url-to-wsdl>"
client = Client(wsdl=loc, wsse=UsernameToken(usr, password, use_digest=True))
【问题讨论】:
在浏览器中打开http://url:7104
是否成功?如果不是问题出在 python 之外。
我可以在浏览器中打开那个网址,没问题。
普通的Client('http://url:7104')
有什么作用?跟踪的关键似乎是“禁止端口”,这是一个网络问题。
它做同样的事情,返回同样的错误信息。
【参考方案1】:
我能够通过执行以下操作来解决问题。该问题是由某些防火墙问题和代理无法正常工作引起的。
import requests
session = requests.session()
session.trust_env = False
transport = Transport(session=session)
client = Client(wsdl=client_location, wsse=UsernameToken(usr, password, use_digest=True), transport=transport)
【讨论】:
以上是关于带有zeep的python SOAP,requests.exception:403客户端错误的主要内容,如果未能解决你的问题,请参考以下文章
带有 Zeep 和 Python 的 SOAP 客户端中的不记名令牌授权标头
Soap 请求在 Soapui 中运行良好,但在 python 中运行良好 - ZEEP - SUDS
Python Zeep SOAP 客户端模块 - 无法为元素设置属性