Last active
December 21, 2015 06:59
-
-
Save nanonyme/6268358 to your computer and use it in GitHub Desktop.
brainstorming on suds + python-requests thinamabob, MIT-licensed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?