7

I'm trying to use the salesforce-python-toolkit to make web services calls to the Salesforce API, however I'm having trouble getting the client to go through a proxy. Since the toolkit is based on top of suds, I tried going down to use just suds itself to see if I could get it to respect the proxy setting there, but it didn't work either.

This is tested on suds 0.3.9 on both OS X 10.7 (python 2.7) and ubuntu 12.04.

an example request I've made that did not end up going through the proxy (just burp or charles proxy running locally):

import suds
ws = suds.client.Client('file://sandbox.xml',proxy={'http':'http://localhost:8888'})
ws.service.login('user','pass')

I've tried various things with the proxy - dropping http://, using an IP, using a FQDN. I've stepped through the code in pdb and see it setting the proxy option. I've also tried instantiating the client without the proxy and then setting it with: ws.set_options(proxy={'http':'http://localhost:8888'})

Is proxy not used by suds any longer? I don't see it listed directly here http://jortel.fedorapeople.org/suds/doc/suds.options.Options-class.html, but I do see it under transport. Do I need to set it differently through a transport? When I stepped through in pdb it did look like it was using a transport, but I'm not sure how.

Thank you!

1
  • Looks like that should work, if you were using http instead of a file: protocol url. Jun 26, 2013 at 0:26

6 Answers 6

14

I went into #suds on freenode and Xelnor/rbarrois provided a great answer! Apparently the custom mapping in suds overrides urllib2's behavior for using the system configuration environment variables. This solution now relies on having the http_proxy/https_proxy/no_proxy environment variables set accordingly.

I hope this helps anyone else running into issues with proxies and suds (or other libraries that use suds). https://gist.github.com/3721801

from suds.transport.http import HttpTransport as SudsHttpTransport 


class WellBehavedHttpTransport(SudsHttpTransport): 
    """HttpTransport which properly obeys the ``*_proxy`` environment variables.""" 

    def u2handlers(self): 
        """Return a list of specific handlers to add. 

        The urllib2 logic regarding ``build_opener(*handlers)`` is: 

        - It has a list of default handlers to use 

        - If a subclass or an instance of one of those default handlers is given 
            in ``*handlers``, it overrides the default one. 

        Suds uses a custom {'protocol': 'proxy'} mapping in self.proxy, and adds 
        a ProxyHandler(self.proxy) to that list of handlers. 
        This overrides the default behaviour of urllib2, which would otherwise 
        use the system configuration (environment variables on Linux, System 
        Configuration on Mac OS, ...) to determine which proxies to use for 
        the current protocol, and when not to use a proxy (no_proxy). 

        Thus, passing an empty list will use the default ProxyHandler which 
        behaves correctly. 
        """ 
        return []

client = suds.client.Client(my_wsdl, transport=WellBehavedHttpTransport())
0
4

I think you can do by using a urllib2 opener like below.

import suds
t = suds.transport.http.HttpTransport()
proxy = urllib2.ProxyHandler({'http': 'http://localhost:8888'})
opener = urllib2.build_opener(proxy)
t.urlopener = opener
ws = suds.client.Client('file://sandbox.xml', transport=t)
3
  • Hi Olly, thanks for the reply, but unfortunately that does not cause it to go through a proxy either. Would it be helpful if I posted an example wsdl?
    – cji
    Sep 13, 2012 at 21:30
  • maybe, what actual error are you getting? also how come the wsdl is a local file?
    – olly_uk
    Sep 13, 2012 at 21:34
  • there's no error per say - it just doesn't show up in the proxy traffic, it's hitting the server straight - I get a result fine. The wsdl file is local because that's how it works with the Salesforce stuff - they don't host the wsdl, you 'generate' it and then use it locally.
    – cji
    Sep 13, 2012 at 21:45
4

I was actually able to get it working by doing two things:

  • making sure there were keys in the proxy dict for http as well as https.
  • setting the proxy using set_options AFTER creation of the client.

So, my relevant code looks like this:

self.suds_client = suds.client.Client(wsdl) self.suds_client.set_options(proxy={'http': 'http://localhost:8888', 'https': 'http://localhost:8888'})

2

I had multiple issues using Suds, even though my proxy was configured properly I could not connect to the endpoint wsdl. After spending significant time attempting to formulate a workaround, I decided to give soap2py a shot - https://code.google.com/p/pysimplesoap/wiki/SoapClient

Worked straight off the bat.

1

For anyone who's attempting cji's solution over HTTPS, you actually need to keep one of the handlers for the basic authentication. I also am using python3.7 so urllib2 has been replaced with urllib.request.

from suds.transport.https import HttpAuthenticated as SudsHttpsTransport
from urllib.request import HTTPBasicAuthHandler

class WellBehavedHttpsTransport(SudsHttpsTransport):
    """ HttpsTransport which properly obeys the ``*_proxy`` environment variables."""

    def u2handlers(self):
        """ Return a list of specific handlers to add.

        The urllib2 logic regarding ``build_opener(*handlers)`` is:

        - It has a list of default handlers to use

        - If a subclass or an instance of one of those default handlers is given
            in ``*handlers``, it overrides the default one.

        Suds uses a custom {'protocol': 'proxy'} mapping in self.proxy, and adds
        a ProxyHandler(self.proxy) to that list of handlers.
        This overrides the default behaviour of urllib2, which would otherwise
        use the system configuration (environment variables on Linux, System
        Configuration on Mac OS, ...) to determine which proxies to use for
        the current protocol, and when not to use a proxy (no_proxy).

        Thus, passing an empty list (asides from the BasicAuthHandler) 
        will use the default ProxyHandler which behaves correctly.
        """
        return [HTTPBasicAuthHandler(self.pm)]
0

If it can help someone:

proxy = {
    'http': 'http://127.0.0.1:8080',
    'https': 'http://127.0.0.1:8080'
}

class ProxyTransport(SudsHttpTransport):
    def __init__(self):
        super(ProxyTransport, self).__init__()

    def u2open(self, u2request):
        import urllib
        proxy_handler = urllib.request.ProxyHandler(proxy)
        opener = urllib.request.build_opener(proxy_handler)
        return opener.open(u2request)

client = suds.client.Client(my_wsdl, transport=ProxyTransport())

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.