SOAP, Python, suds
Asked Answered
L

3

6

Please advise library for working with soap in python.
Now, I'm trying to use "suds" and I can't undestand how get http headers from server reply
Code example:

from suds.client import Client
url = "http://10.1.0.36/money_trans/api3.wsdl"
client = Client(url)
login_res = client.service.Login("login", "password")

variable "login_res" contain xml answer and doesnt contain http headers. But I need to get session id from them.

Laraelaraine answered 10/3, 2010 at 15:38 Comment(1)
Are you asking for general advice on Python libraries for SOAP, or are you asking for help on a particular aspect of suds? Please try to ask specific questions.Angilaangina
J
4

I think you actually want to look in the Cookie Jar for that.

client = Client(url)
login_res = client.service.Login("login", "password")
for c in client.options.transport.cookiejar:
   if "sess" in str(c).lower():
      print "Session cookie:", c

I'm not sure. I'm still a SUDS noob, myself. But this is what my gut tells me.

Jacktar answered 21/3, 2010 at 2:36 Comment(1)
+1 because this is on the right track, assuming that urllib2 (the opener used natively by Suds) plays nice with cookies. If you have any 302 redirects in the path, all bets are off because urllib2 + 302 redirects + cookies = HELL.Aftermost
A
4

The response from Ishpeck is on the right path. I just wanted to add a few things about the Suds internals.

The suds client is a big fat abstraction layer on top of a urllib2 HTTP opener. The HTTP client, cookiejar, headers, request and responses are all stored in the transport object. The problem is that none of this activity is cached or stored inside of the transport other than, maybe, the cookies within the cookiejar, and even tracking these can sometimes be problematic.

If you want to see what's going on when debugging, my suggestion would be to add this to your code:

import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)

Suds makes use of the native logging module and so by turning on debug logging, you get to see all of the activity being performed underneath including headers, variables, payload, URLs, etc. This has saved me tons of times.

Outside of that, if you really need to definitively track state on your headers, you're going to need to create a custom subclass of a suds.transport.http.HttpTransport object and overload some of the default behavior and then pass that to the Client constructor.

Here is a super-over-simplified example:

from suds.transport.http import HttpTransport, Reply, TransportError
from suds.client import Client

class MyTransport(HttpTransport):
    # custom stuff done here

mytransport_instance = MyTransport()
myclient = Client(url, transport=mytransport_instance)
Aftermost answered 12/4, 2011 at 0:14 Comment(0)
G
0

I think Suds library has a poor documentation so, I recommend you to use Zeep. It's a SOAP requests library in Python. Its documentation isn't perfect, but it's very much clear than Suds Doc.

Gradus answered 29/1, 2020 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.