Python SUDS, WSDL, HTTPS proxy and SOAP authentication.

pythonToday I would like to share a recipe how to utilize WSDL (SOAP) in a Python SUDS script behind the HTTPS proxy. It may be useful for getting some commercial feeds on a server sitting behind the corporate firewall.

First of all, I find out the Python SUDS are very convenient. You can download it here or just install it via yum (or apt-get):

yum install python-suds

It is compatible even with older Python 2.4 (too bad that it does not have ElementTree).

OK, but now the issue – SUDS ignores env setting for proxy, whenever you try to connect to a WSDL service, it simply time outs. Furthermore all attempts to explicitly specify proxy settings fail:

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

Depending how you specify the proxy settings, the SUDS either time outs or returns really weird error about EOF in communication. The reason is simple – by default the urllib2 is not compatible with HTTPS proxy. It is able to utilize a HTTP proxy, but not a HTTPS proxy. Too bad!

Finally I find out the following solution:

1. Implement a “opener” for SSL proxy:

urrlib2 opener for SSL proxy (CONNECT method) (Python recipe)

2. Utilize this opener properly:

from suds.client import Client
from httpsproxy import ConnectHTTPSHandler, ConnectHTTPHandler
import urllib2, urllib
from suds.transport.http import HttpTransport

opener = urllib2.build_opener(ConnectHTTPHandler, ConnectHTTPSHandler)
urllib2.install_opener(opener)
t = HttpTransport()
t.urlopener = opener
url = 'https://myurl.com'
client = Client(url, transport=t)

3. Add credentials to the WSDL client object:

servicereq=client.factory.create('SomeRequest')
servicereq.credentials.userName='myname'
servicereq.credentials.password='mypassword'

The last step depends on particular WSDL service specification for credentials, please consult the Web API service document.

Leave a comment