您知道如何验证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
答案 0 :(得分:1)
只需在调用周围进行try / except并处理异常。
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)