WCF and Python

后端 未结 7 533

Is there any example code of a cpython (not IronPython) client which can call Windows Communication Foundation (WCF) service?

相关标签:
7条回答
  • 2020-12-05 07:49

    TL;DR: For wsHttpBinding (SOAP 1.2) use zeep


    In case someone is having trouble using suds (or suds-jurko for that matter) with WCF and wsHttpBinding (which is SOAP 1.2):

    • suds is pretty much dead (can't even pip install it on python 3)
    • suds-jurko seems kind-of dead. The 0.6 release has a very annoying infinite recursion bug (at least on the WSDL exposed by our service) which is fixed in the tip but that's not released and it's been 1.5years (at time of this writing in Feb'17) since the last commit.
      It works on python 3 but doesn't support SOAP 1.2. Sovetnikov's answer is an attempt to get it working with 1.2 but I haven't managed to make it work for me.
    • zeep seems to be the current way to go and worked out of the box (I'm not affiliated with zeep, it just works for me and I spent several hours banging my head against a brick wall trying to make suds work). For zeep to work, the WCF service host configuration must include <security mode="None"/> under the wsHttpBinding node Actually zeep seems to support username and signature (x509) based WS-SE but I haven't tried that so can't speak to any problems around it.
    0 讨论(0)
  • 2020-12-05 07:50

    Just to help someone to access WCF SOAP 1.2 service with WS-Addressing using suds. Main problem is to inject action name in every message.

    This example for python 3 and suds port https://bitbucket.org/jurko/suds.

    Example uses custom authentification based on HTTP headers, i leave it as is.

    TODO: Automatically get api_direct_url from WSDL (at now it is hard coded).

    from suds.plugin import MessagePlugin
    from suds.sax.text import Text
    from suds.wsse import Security, UsernameToken
    from suds.sax.element import Element
    from suds.sax.attribute import Attribute
    from suds.xsd.sxbasic import Import
    
    api_username = 'some'
    api_password = 'none'
    
    class api(object):
        api_direct_url = 'some/mex'
        api_url = 'some.svc?singleWsdl|Wsdl'
    
        NS_WSA = ('wsa', 'http://www.w3.org/2005/08/addressing')
    
        _client_instance = None
        @property
        def client(self):
            if self._client_instance:
                return self._client_instance
            from suds.bindings import binding
            binding.envns = ('SOAP-ENV', 'http://www.w3.org/2003/05/soap-envelope')
    
            api_inst = self
            class _WSAPlugin(MessagePlugin):
                def marshalled(self, context):
                    api_inst._marshalled_message(context)
    
            self._client_instance = Client(self.api_url,
                                 plugins=[_WSAPlugin()],
                                 headers={'Content-Type': 'application/soap+xml',
                                          'login':api_username,
                                          'password': api_password}
                                 )
            headers = []
            headers.append(Element('To', ns=self.NS_WSA).setText(self.api_direct_url))
            headers.append(Element('Action', ns=self.NS_WSA).setText('Blank'))
            self._client_instance.set_options(soapheaders=headers)
    
            cache = self._client_instance.options.cache
            cache.setduration(days=10)
            return self._client_instance
    
        def _marshalled_message(self, context):
            def _children(r):
                if hasattr(r, 'children'):
                    for c in r.children:
                        yield from _children(c)
                        yield c
            for el in _children(context.envelope):
                if el.name == 'Action':
                    el.text = Text(self._current_action)
                    return
    
        _current_action = None
        def _invoke(self, method, *args):
            try:
                self._current_action = method.method.soap.action.strip('"')
                return method(*args)
            finally:
                self._current_action = None
    
        def GetRequestTypes(self):
            return self._invoke(self.client.service.GetRequestTypes)[0]
    
        def GetTemplateByRequestType(self, request_type_id):
            js = self._invoke(self.client.service.GetTemplateByRequestType, request_type_id)
            return json.loads(js)
    
        def GetRequestStatus(self, request_guid):
            return self._invoke(self.client.service.GetRequestStatus, request_guid)
    
        def SendRequest(self, request_type_id, request_json):
            r = json.dumps(request_json, ensure_ascii=False)
            return self._invoke(self.client.service.SendRequest, request_type_id, r)
    
    0 讨论(0)
  • 2020-12-05 07:50

    Even if there is not a specific example of calling WCF from Python, you should be able to make a fully SOAP compliant service with WCF. Then all you have to do is find some examples of how to call a normal SOAP service from Python.

    The simplest thing will be to use the BasicHttpBinding in WCF and then you can support your own sessions by passing a session token with each request and response.

    0 讨论(0)
  • 2020-12-05 07:54

    if you need binary serialized communication over tcp then consider implementing solution like Thrift.

    0 讨论(0)
  • 2020-12-05 08:05

    WCF needs to expose functionality through a communication protocol. I think the most commonly used protocol is probably SOAP over HTTP. Let's assume that's what you're using then.

    Take a look at this chapter in Dive Into Python. It will show you how to make SOAP calls.

    I know of no unified way of calling a WCF service in Python, regardless of communication protocol.

    0 讨论(0)
  • 2020-12-05 08:07

    I do not know of any direct examples, but if the WCF service is REST enabled you could access it through POX (Plain Old XML) via the REST methods/etc (if the service has any). If you are in control of the service you could expose endpoints via REST as well.

    0 讨论(0)
提交回复
热议问题