2015-05-20 64 views
0

您知道如何验证iam用户名是否已经存在?验证iam用户名是否已经存在

我试着用下面的代码,使用get_user,但是当用户不存在时我得到一个错误:The user with name teste cannot be found

你知道怎么做吗?

c = boto.iam.connect_to_region("us-east-1") 
while True: 
    username = raw_input("Username:") 
    password = raw_input("Password:") 
    if not username or not password: 
     print "Please enter a valid username and password" 
    if c.get_user(username): 
     print "Username already exist, please enter a different username..." 

新的尝试:

while True: 
    username = raw_input("Username:") 
    password = raw_input("Password:") 
    if not username or not password: 
     print "Please enter a valid username and password" 
    try: 
     if not c.get_user(username): 
      print "Username valid" 
    except BotoServerError, e: 
     print "Username already exist 

回答

1

只要把一个try /除了随时待命,并处理异常。

from boto.exception import BotoServerError 
c = boto.iam.connect_to_region("us-east-1") 
while True: 
    username = raw_input("Username:") 
    password = raw_input("Password:") 
    if not username or not password: 
     print "Please enter a valid username and password" 
    try: 
     c.get_user(username) 
    except BotoServerError, e: 
     print(e.message) 
+0

感谢您的回答。但林有一个错误:“没有模块名为例外”。 – techman

+0

对不起,它是''boto.exception''而不是''boto.exceptions''。我修正了这个例子。 – garnaat

+0

我用新的尝试更新我的问题。因为我只想输入,除非用户名已经存在,否则get_user(用户名)会返回一些内容。但即使用户不存在,我总是进入。你看到问题在哪里? – techman

相关问题