Skip to content

Instantly share code, notes, and snippets.

@nanonyme
Last active December 21, 2015 06:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nanonyme/6268358 to your computer and use it in GitHub Desktop.
Save nanonyme/6268358 to your computer and use it in GitHub Desktop.
brainstorming on suds + python-requests thinamabob, MIT-licensed
from suds.transport import Transport, Reply
from suds.client import Client
import requests
from StringIO import StringIO
class RequestsTransport(Transport):
def open(self, request):
""" suds assumes urllib2 which doesn't have keepalives so things can go royally wrong if it doesn't
read the entire request. Hence, read it all up and give a StringIO object instead.
Error handling needed
"""
verify_ssl = self.options.get("verify_ssl", True)
r = requests.get(url=request.url, headers=request.headers,
verify=verify_ssl)
return StringIO(r.content)
def send(self, request):
""" Just a naivistic suds.transport.Request->requests->suds.transport.Reply wrapper. This is
missing proper error handling
"""
verify_ssl = self.options.get("verify_ssl", True)
r = requests.post(url=request.url, data=request.message, headers=request.headers,
verify=verify_ssl)
return Reply(code=r.status_code, headers=r.headers, message=r.content)
class RequestsClient(Client):
def __init__(self, url, **kwargs):
kwargs.setdefault("transport", RequestsTransport)
Client.__init__(self, url, **kwargs)
@multivoxmuse
Copy link

This is great work, it looks like it will be perfect for a project at work. Do you have an example of how it should be used in conjunction with suds? It looks like the open and send methods are expecting a 'request' object, so are you to use suds to create the requests object then send it to the methods of RequestsTransport subclass?

@manelclos
Copy link

I tried this gist with a suds 0.4 and it failed.

I tried lots of SUDS versions and forks, and finally got to find one that works with proxies, https and authenticated services, find it here:

https://github.com/unomena/suds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment