Skip to main content
 首页 » 编程设计

python之如何在 SUDS 中获得错误响应

2024年10月25日10Renyi-Fan

如果我有一些错误的授权数据(例如错误的密码)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>

请您参考如下方法:

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

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而不是通用异常。可能还有其他事情正在发生并触发您的异常 block 。

可能有助于解决您的问题的另一件事是通过 faults=True在您的 Client() 调用中。

The Client can be configured to throw web faults as WebFault or to return a tuple (, )

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

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