简体   繁体   English

如何在Python中使用SSL和身份验证连接到SOAP服务(无WSDL)?

[英]How to connect to a SOAP service (without WSDL) using SSL and authentication in Python?

I want to connect to a SOAP API that does not have WSDL in Python. 我想连接到在Python中没有WSDL的SOAP API。 To connect I need to a add a SSL certificate and authenticate afterwards. 要进行连接,我需要添加一个SSL证书,然后进行身份验证。

from pysimplesoap.client import SoapClient, SimpleXMLElement

cacert = open(path, 'rb').read()  # read the certificate
header = SimpleXMLElement('<Header/>')
credentials = header.add_child('Credentials')
credentials.marshall('Password', 'password')
credentials.marshall('Username', 'username')

client = SoapClient(
    location="https://mytest.com/Services/",
    cacert=cacert)
client['Header'] = header

client.action = "https://mytest.com/Services/Action1"

client.Action1()  # gives SSL error

The result I receive is a SSL error: 我收到的结果是SSL错误:

SSLHandshakeError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

Can anyone, please, tell me how to solve this issue? 谁能告诉我如何解决这个问题? Or can you advise any other library I can use. 或者您可以建议我可以使用的任何其他图书馆。 Most SOAP libraries I found offer connection only to WSDL. 我发现大多数SOAP库仅提供与WSDL的连接。

Usually in a pfx file there is the client certificate with key, not the CA file. 通常在pfx文件中,包含带有密钥的客户端证书,而不是CA文件。 The libraries seems to expect the client certificate as PEM. 这些库似乎期望将客户端证书作为PEM。 You should extract the certificate and the key as show in https://stackoverflow.com/a/9516936/3929826 . 您应该按照https://stackoverflow.com/a/9516936/3929826中的说明提取证书和密钥。

Then hand it in to the SoapClient() initiation as cert and key_file argument: 然后将其作为certkey_file参数传递给SoapClient()初始化:

client = SoapClient(location="https://mytest.com/Services/",
                    cert='mycert.pem',
                    key_file='mycert.key)

It should also be possible to put both into the cert file. 也应该将两者都放入cert文件中。

If that still does not work your have to add the CA certificate as the cacert parameter after you retrieved it as described in https://stackoverflow.com/a/7886248/3929826 . 如果仍然不能解决问题,则必须按照https://stackoverflow.com/a/7886248/3929826中的说明,在检索到CA证书cacert其添加为cacert参数。

For further reference see the source code of simplesoap : https://code.google.com/p/pysimplesoap/source/browse/pysimplesoap/client.py#75 . 有关更多参考,请参见simplesoap的源代码: https : //code.google.com/p/pysimplesoap/source/browse/pysimplesoap/client.py#75

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

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