Python 如何输出正在生成/接收的 SUD?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4426204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I output what SUDs is generating/receiving?
提问by alfredo
I have the following code:
我有以下代码:
from suds.client import Client
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)
logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)
logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)
SB_PRIVATE_ACCESS = {"PATH":"https://thisurl.com:443/services/",}
client = Client(SB_PRIVATE_ACCESS['PATH'])
print client
but I am getting 500 errors. I am trying to send what XML is being generated and received through SUDs, to the wsdl developer, but I can't figure how to output it? I have been looking in the documentation of SUDs, but can't seem to find it :/ Does anyone know how to output the raw xml that is sent and received?
但我收到 500 个错误。我正在尝试将通过 SUD 生成和接收的 XML 发送给 wsdl 开发人员,但我不知道如何输出它?我一直在查看 SUD 的文档,但似乎找不到它:/ 有谁知道如何输出发送和接收的原始 xml?
回答by Mikko Ohtamaa
Suds supports internal logging, as you have been doing.
Suds 支持内部日志记录,就像您一直在做的那样。
I am setting info levels like you:
我正在像您一样设置信息级别:
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG) # MUST BE THIS?
logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG)
logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)
logging.getLogger('suds.resolver').setLevel(logging.DEBUG)
logging.getLogger('suds.xsd.query').setLevel(logging.DEBUG)
logging.getLogger('suds.xsd.basic').setLevel(logging.DEBUG)
logging.getLogger('suds.binding.marshaller').setLevel(logging.DEBUG)
And I also sometimes need to override the root logger logging level, depending on the framework being used under Suds calls (Django, Plone). If the root logger has a higher logging threshold, log messaegs may never appear (not sure how logger hierarchies should go). Below is an example how to override:
而且我有时还需要覆盖根记录器日志记录级别,具体取决于在 Suds 调用(Django、Plone)下使用的框架。如果根记录器具有更高的记录阈值,则日志消息可能永远不会出现(不确定记录器层次结构应该如何)。以下是如何覆盖的示例:
def enableDebugLog(self):
""" Enable context.plone_log() output from Python scripts """
import sys, logging
logger = logging.getLogger()
logger.root.setLevel(logging.DEBUG)
logger.root.addHandler(logging.StreamHandler(sys.stdout))
回答by olly_uk
SUDS provides some convenience methods to do just that:
SUDS 提供了一些方便的方法来做到这一点:
client.last_sent()
client.last_received()
These should provide you with what you need. I use them for error logging. The API docfor Client class should have any extra info you need.
这些应该为您提供您需要的东西。我用它们来记录错误。 Client 类的 API 文档应该包含您需要的任何额外信息。
回答by Dustin
try changing
尝试改变
logging.basicConfig(level=logging.INFO)
to
到
logging.basicConfig(filename="/tmp/suds.log", level=logging.DEBUG)
回答by Fernando César
To get only the generated message this also works:
要仅获取生成的消息,这也适用:
from suds.client import Client
import sys
SB_PRIVATE_ACCESS = {"PATH":"https://thisurl.com:443/services/",}
client = Client(SB_PRIVATE_ACCESS['PATH'])
client.set_options(nosend=True)
resp = ...<invoke client here>...
sys.stdout.buffer.write(resp.envelope)
回答by Damian
You can use the MessagePlugin to do this (this will work on the newer Jurko fork where last_sent and last_received have been removed)
您可以使用 MessagePlugin 来执行此操作(这将适用于已删除 last_sent 和 last_received 的较新的 Jurko fork)
from suds.plugin import MessagePlugin
class LogPlugin(MessagePlugin):
def sending(self, context):
print(str(context.envelope))
def received(self, context):
print(str(context.reply))
client = Client("http://localhost/wsdl.wsdl", plugins=[LogPlugin()])
回答by Alex Punnen
If you want to reduce logging by jurko-suds
如果您想减少 jurko-suds 的日志记录
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.INFO)
logging.getLogger('suds.transport').setLevel(logging.INFO)
logging.getLogger('suds.xsd.schema').setLevel(logging.INFO)
logging.getLogger('suds.wsdl').setLevel(logging.INFO)
logging.getLogger('suds.resolver').setLevel(logging.INFO)
logging.getLogger('suds.xsd.query').setLevel(logging.INFO)
logging.getLogger('suds.xsd.sxbasic').setLevel(logging.INFO)
logging.getLogger('suds.xsd.sxbase').setLevel(logging.INFO)
logging.getLogger('suds.metrics').setLevel(logging.INFO)
logging.getLogger('suds.binding.marshaller').setLevel(logging.INFO)
回答by Oly
I have hit this problem working with the bingads API, worth noting the order is important I had to import logging then import suds start the logging then import bingads, any other order and nothing was output in the logs from suds.
我在使用 bingads API 时遇到了这个问题,值得注意的是顺序很重要,我必须导入日志,然后导入 suds 开始日志记录,然后导入 bingads,任何其他顺序都没有在 suds 的日志中输出。
So check your import order, and move your logging statements and it may fix your issue.
因此,请检查您的导入顺序,并移动您的日志记录语句,它可能会解决您的问题。

