首页 > 解决方案 > 在 python spyne 服务器中打印出整个 SOAP 请求

问题描述

我有一个使用spyne. 这是代码:

class myService(ServiceBase):

    @rpc(Integer, String, _returns=String)
    def my_method(ctx):
        sys.stdout.write("Request for `myService`: {}\n".format(ctx))
        return "0," + "".join(random.sample(string.hexdigits, 16))


application = Application(
    [myService],
    tns=NAMESPACE,
    in_protocol=Soap11(validator="lxml"),
    out_protocol=Soap11()
)


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger("spyne.util").setLevel(logging.DEBUG)
    wsgi_app = WsgiApplication(application)
    server = make_server("0.0.0.0", 5555, wsgi_app)
    server.serve_forever()

这是一个模拟真实服务的模拟器。我将它们与所需的客户端连接起来,但code 400, message Bad HTTP/0.9 request type每个请求都会出错。由于我无权访问正在调用我的模拟服务器的客户端,因此我想在我的服务器中打印出接收到的请求。上面的代码不起作用(sys.stdout.write)。

编辑1:

我改为sys.stdoutpython logger

import logging
logger = logging.getLogger("spyne.util")
...
...
...
logger.info("Request for `myService`: {}\n".format(ctx))

但是没有打印任何内容,这次我遇到了以下错误:

code 400, message Bad request syntax

标签: pythonsoapspyne

解决方案


以下应该做到这一点:

logging.getLogger('').setLevel(logging.DEBUG)
logging.getLogger('spyne.protocol').setLevel(logging.DEBUG)
logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)

推荐阅读