如果找不到方法,如何在python中使用suds处理Exception

时间:2014-01-06 12:02:35

标签: python suds

嗨如果找不到特定的Soap方法,如何处理suds中的异常。我的要求是如果找不到的方法(由用户输入)程序应该提示不是有效的方法。

 for method in client.wsdl.services[0].ports[0].methods.values():
     print "the existing methods in webservice are:" +method.name
     try:
        s=raw_input("enter the name of the method you want to scan: ")
        name= getattr(client.service,s)
     except suds.WebFault,e:
        print e

但如果我输错了方法,我的程序就会被终止。

2 个答案:

答案 0 :(得分:0)

你的程序应该是这样的:

for method in client.wsdl.services[0].ports[0].methods.values():
    print "the existing methods in webservice are:" +method.name
while True
    try:
        s=raw_input("enter the name of the method you want to scan: ")
        #if user enter exit or some special command, break here
        name= getattr(client.service,s)
        #call the target method here ...
    except suds.WebFault,e:
        print e

可以对第一个print行进行更多编辑,以使输出消息更具可读性,留给您。

答案 1 :(得分:0)

您没有任何循环来让用户输入新数据。尝试类似:

methods = list(client.wsdl.services[0].ports[0].methods.values())
print "Existing methods are {0} and {1}.".format(", ".join(map(str, methods[:-1])), 
                                              str(methods[-1]))
while True:
    s = raw_input("Enter the name of the method you want to scan: ")
    if s not in methods:
        print "Not a valid method, please try again."
    else:
        break # valid method, continue
# rest of your code goes here