Therefore, I think that we can solve this problem using the suds library.
Here is a very simple example of sending a SOAP request that includes headers:
Example:
from suds.sax.element import Element client = client(url) ssnns = ('ssn', 'http://namespaces/sessionid') ssn = Element('SessionID', ns=ssnns).setText('123') client.set_options(soapheaders=ssn) result = client.service.addPerson(person)
This is an example of how you send the following header:
<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP ENC="http://www.w3.org/2003/05/soap-encoding"> <ssn:SessionID SOAP-ENV:mustUnderstand="true">123</ssn:SessionID> </SOAP-ENV:Header>
NB: I really have not tried this on my own since I do not have access to any available SOAP / XML services with which I can test!
So, in your specific example, you would do something like this:
>>> from suds.sax.element import Element >>> subns = ("sub", "http://namespaces/sub") >>> sub = Element("auth", ns=subns) >>> token = Element("token").setText("?") >>> user_id = Element("user_id").setText("?") >>> user_token = Element("user_token").setText("?") >>> sub.append(token) Element (prefix=sub, name=auth) >>> sub.append(user_id) Element (prefix=sub, name=auth) >>> sub.append(user_token) Element (prefix=sub, name=auth) >>> print(sub.str()) <sub:auth xmlns:sub="http://namespaces/sub"> <token>?</token> <user_id>?</user_id> <user_token>?</user_token> </sub:auth>
Then call set_options() on your client object:
client.set_options(soapheaders=sub)
And you can easily install suds with pip by doing:
$ pip install suds
source share