简体   繁体   English

肥皂、Python、泡沫

[英]SOAP, Python, suds

Please advise library for working with soap in python.请建议库在 python 中使用肥皂。
Now, I'm trying to use "suds" and I can't undestand how get http headers from server reply现在,我正在尝试使用“suds”,但我无法理解如何从服务器回复中获取 http 标头
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.变量“login_res”包含 xml 答案,不包含 http 标头。 But I need to get session id from them.但我需要从他们那里获取会话 ID。

I think you actually want to look in the Cookie Jar for that.我认为您实际上想查看 Cookie Jar。

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.我自己还是个 SUDS 菜鸟。 But this is what my gut tells me.但这是我的直觉告诉我的。

The response from Ishpeck is on the right path. Ishpeck的回应是正确的。 I just wanted to add a few things about the Suds internals.我只是想添加一些关于 Suds 内部结构的东西。

The suds client is a big fat abstraction layer on top of a urllib2 HTTP opener. suds 客户端是一个位于 urllib2 HTTP 开启器之上的大型抽象层。 The HTTP client, cookiejar, headers, request and responses are all stored in the transport object. HTTP 客户端、cookiejar、标头、请求和响应都存储在transport对象中。 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.问题在于,除了 cookiejar 中的 cookie 之外,这些活动都没有被缓存或存储在传输中,甚至跟踪这些有时也会有问题。

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. Suds 利用本机logging模块,因此通过打开调试日志记录,您可以看到在下面执行的所有活动,包括标头、变量、有效负载、URL 等。这为我节省了大量时间。

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.除此之外,如果您确实需要明确跟踪标头上的状态,则需要创建suds.transport.http.HttpTransport对象的自定义子类并重载一些默认行为,然后将其传递给Client构造函数。

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)

I think Suds library has a poor documentation so, I recommend you to use Zeep .我认为 Suds 库的文档很差,所以我建议您使用Zeep It's a SOAP requests library in Python.它是 Python 中的 SOAP 请求库。 Its documentation isn't perfect, but it's very much clear than Suds Doc.它的文档并不完美,但比 Suds Doc 清晰得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM