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

Soap call using pfx or p12 authentication in Zeep or suds library Python

我想使用 .p12 证书文件来使用 SOAP 调用服务。我试过下面的代码。但它抛出路径错误。

有什么解决办法吗,在肥皂水里python3?

*TypeError: stat: path should be string, bytes, os.PathLike or integer, not X509*

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

pkcs12 = crypto.load_pkcs12(open(CertPath, 'rb').read(), Password)
session = Session()
session.cert = (pkcs12.get_certificate(),pkcs12.get_privatekey())
transport = Transport(session=session)
client = Client(
    'http://my.own.sslhost.local/service?WSDL',
    transport=transport) 

在添加到 session.cert 之前,您需要将 p12 证书转换为 pem 格式,例如如下所示:

pkcs12 = crypto.load_pkcs12(open(CertPath, 'rb').read(), Password)

cert = crypto.dump_certificate(crypto.FILETYPE_PEM, pkcs12.get_certificate())
key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkcs12.get_privatekey())

with open('cert.pem', 'wb') as f:
    f.write(cert)    
with open('key.pem', 'wb') as f:
    f.write(key)

session.cert = ('cert.pem', 'key.pem')