Python SUDS: Using proxy

Suds is a lightweight SOAP python client for consuming Web Services. As my other developers I ran into the problem that my SUDS client did not connect via a proxy server to a desired URL.

One can find lots of tipps requiring an indecent amount of custom code, which I did not fancy. Below I present a solution with only a small number of lines of code to achieve the desired goal.

Problem

Digging into the code of SUDS and some other useful modules I found that the proxy settings are used for sending requests and receiving responses, but not for the initial retrieval of the wsdl definitions. Fortunately I noticed that SUDS uses sessions, which allows for a very simple solution.

Solution


import requests
import suds
import suds_requests

url = "https://.......?wsdl"
proxy = { 
'http': "http://......:8080", 
'https': "http://.......:8080", }

data = { 
"login-form-type": "pwd", 
"username": "", 
"password": "", 
"fe11y": "" }

session = requests.Session()
session.verify = False
session.proxies = proxy

r = session.post( 
"https://......./login.form", 
data=data, 
proxies=proxy
)

transport = suds_requests.RequestsTransport(session=session, cookies=r.cookies)
client = suds.client.Client(url, transport=transport, proxy=proxy)

In this example solution the proxy information is set in the session object. As this was a test setup with a self-signed certificate the verify attribute of the session is set to False. In a productive solution you wouldn't do that. The proxy server requires an authentication with username and password on a web frontend. So the dictionary data holds the POST data for the login at the proxy. The login is performed using the sessions post method. To keep the authentication status for further requests via the proxy the cookies from the response are extracted.

For the sake of simplicity I use the suds_requests module, which provides the RequestsTransport class, which is derived from suds.transport and uses the requests module to handle requests/responses.

To send and receive via the proxy with SUDS a RequestsTransport object is initiated. As arguments the session object and retrieved cookies are given. Finally the SUDS client is initiated with the RequestsTransport object.

Now have fun with your SOAP calls.

 

References

  1. https://fedorahosted.org/suds/wiki/Documentation
  2. http://stackoverflow.com/questions/12414600/suds-ignoring-proxy-setting
  3. http://blog.nguyenvq.com/blog/2014/07/31/issues-with-https-proxy-in-python-via-suds-and-urllib2/
  4. https://github.com/armooo/suds_requests/blob/master/suds_requests.py