Suds over https with cert

前端 未结 5 1692
醉话见心
醉话见心 2020-12-07 15:47

I have soap service under Apache with ssl, suds works greate without ssl.
I have client certificate (my.crt and user.p12 files).
How I need to configure suds clien

5条回答
  •  攒了一身酷
    2020-12-07 16:43

    Based on @k4ml answer, I've only added the open() which allows to fetch the WSDL using the certificate.

    This method should fix the suds.transport.TransportError: HTTP Error 403: Forbidden when trying to fetch a WSDL (at Client creation) served behind a HTTPS service.

    import requests
    from suds.transport.http import HttpAuthenticated
    from suds.transport import Reply, TransportError
    
    class RequestsTransport(HttpAuthenticated):
        def __init__(self, **kwargs):
            self.cert = kwargs.pop('cert', None)
            # super won't work because not using new style class
            HttpAuthenticated.__init__(self, **kwargs)
    
        def open(self, request):
            """
            Fetches the WSDL using cert.
            """
            self.addcredentials(request)
            resp = requests.get(request.url, data=request.message,
                                 headers=request.headers, cert=self.cert)
            result = io.StringIO(resp.content.decode('utf-8'))
            return result
    
        def send(self, request):
            """
            Posts to service using cert.
            """
            self.addcredentials(request)
            resp = requests.post(request.url, data=request.message,
                                 headers=request.headers, cert=self.cert)
            result = Reply(resp.status_code, resp.headers, resp.content)
            return result
    

    Side note, I've also made a suggested edit to k4ml's answer, but it can take ages before it gets approved.

提交回复
热议问题