Skip to content

Instantly share code, notes, and snippets.

@MaxMorais
Forked from nanonyme/gist:6268358
Created October 3, 2015 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaxMorais/a066b2350ed2f7c28c2b to your computer and use it in GitHub Desktop.
Save MaxMorais/a066b2350ed2f7c28c2b 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment