肥皂、Python、泡沫

发布于 2024-08-24 09:58:46 字数 322 浏览 10 评论 0原文

请建议库在Python中使用soap。
现在,我正在尝试使用“suds”,但我无法理解如何从服务器回复获取http标头
代码示例:

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")

变量“login_res”包含 xml 答案,但不包含 http 标头。但我需要从他们那里获取会话 ID。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛滥成性 2024-08-31 09:58:47

我想你实际上想在饼干罐里找找。

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

我不知道。我自己还是个 SUDS 菜鸟。但这是我的直觉告诉我的。

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.

眼睛会笑 2024-08-31 09:58:47

Ishpeck 的响应位于正确的路径上。我只是想添加一些有关 Suds 内部结构的内容。

suds 客户端是 urllib2 HTTP opener 之上的一个庞大的抽象层。 HTTP 客户端、cookiejar、标头、请求和响应都存储在 transport 对象中。问题是,除了 cookiejar 中的 cookie 之外,这些活动都不会缓存或存储在传输内部,甚至跟踪这些活动有时也会出现问题。

如果您想查看调试时发生的情况,我的建议是将其添加到您的代码中:

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

Suds 使用本机日志记录 模块,因此通过打开调试日志记录,您可以看到下面正在执行的所有活动,包括标头、变量、有效负载、URL 等。救了我很多次。

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

这是一个超级简单的例子:

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)

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)
五里雾 2024-08-31 09:58:47

我认为 Suds 库的文档很差,所以我建议您使用 Zeep。它是 Python 中的 SOAP 请求库。它的文档并不完美,但比 Suds Doc 清楚得多。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文