【问题标题】:How to get response on error in SUDS?如何获得对 SUDS 错误的响应?
【发布时间】:2014-07-02 13:23:58
【问题描述】:

如果我有一些错误的授权数据(例如错误的密码)SUDS 出现异常(400, u'Bad Request'),我无法从中得到任何东西,但在日志中是响应,其中包含密码错误的数据,但如何获得此响应?我试过这样:

except Exception as e:

    print str(e)
    print self._client.last_received()

打印出来:

(400, u'Bad Request')
None

但在日志中有很长的xml,其中包含<SOAP-ENV:Reason><SOAP-ENV:Text xml:lang="en">Sender not authorized</SOAP-ENV:Text></SOAP-ENV:Reason>

【问题讨论】:

  • 你如何定义self._client
  • @Andy 它是 suds 对象
  • 你能显示代码的try 部分吗?您所展示的内容正在使用我自己的 try 块为我工作。让我们看看你在做什么
  • @Andy 有很多代码......主要问题是异常......如果我写正确的密码last_received 工作正常,但是当我犯一些错误时它会在异常后失败。

标签: python suds


【解决方案1】:

由于代码块,我将其从评论中提取出来并放入答案中。

import suds.client

try:
    auth_url = "https://url.to.my.service/authenticator?wsdl"
    auth_client = suds.client.Client(auth_url)
    cookie = auth_client.service.authenticate(user,password)
except Exception as e:
    print str(e)
    print auth_client.last_received()

使用此代码,如果我传递了无效密码,我会收到来自我的服务的适当响应:

Server raised fault: 'error.pwd.incorrect'
None

如果我传递了无效的用户 ID,则会得到适当的响应:

Server raised fault: 'error.uid.missing'
None

您可能想要考虑做的事情是更改您的 except 语句以捕获 suds.WebFault 而不是通用异常。可能还有其他事情正在发生并触发您的异常块。

另一件可能对您的问题有所帮助的方法是在您的Client() 调用中传递faults=True

客户端可以配置为将 Web 故障作为 WebFault 或 返回一个元组 (, )

我在上面发布的代码如下所示:

auth_client = suds.client.Client(auth_url, faults=True)

【讨论】:

  • 怎么不工作了?它在做什么与您的期望是什么?
  • auth_client = suds.client.Client(auth_url, faults=True) 对我不起作用。仍然auth_client.last_received() 没有给我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多