LDAP 搜索属性
LDAP search attributes
我在我的 LDAP 库中进行了搜索:
user_account = server.search_s(
'ou=usuarios,ou=xxx,o=system xxx',
ldap.SCOPE_ONELEVEL,
'uid=' + login,
['uid', 'mail', 'objectClass', 'CPF'])[0]
if user_account[1]['CPF'] is not None:
plpy.debug("It has CPF")
如果 CPF 字段已填写,则工作正常,但如果未填写,则会给我一个错误:
ERROR: KeyError: 'CPF'
我试过:
if user_account[1]['CPF'] is None:
plpy.debug("It hasn't CPF")
但它给了我同样的错误。
您可以使用 get()
从字典中获取值,如果不存在则使用 return 获取默认值。如果您不提供默认值,它将 return None
.
cpf = user_account[1].get("CPF")
if cpf:
plpy.debug("It has CPF")
另一种方法是使用 in
运算符。它可以告诉你某个键是否存在于字典中。
if "CPF" in user_account[1]:
plpy.debug("It has CPF")
这与做的一样:
if "CPF" in user_account[1].keys():
plpy.debug("It has CPF")
我在我的 LDAP 库中进行了搜索:
user_account = server.search_s(
'ou=usuarios,ou=xxx,o=system xxx',
ldap.SCOPE_ONELEVEL,
'uid=' + login,
['uid', 'mail', 'objectClass', 'CPF'])[0]
if user_account[1]['CPF'] is not None:
plpy.debug("It has CPF")
如果 CPF 字段已填写,则工作正常,但如果未填写,则会给我一个错误:
ERROR: KeyError: 'CPF'
我试过:
if user_account[1]['CPF'] is None:
plpy.debug("It hasn't CPF")
但它给了我同样的错误。
您可以使用 get()
从字典中获取值,如果不存在则使用 return 获取默认值。如果您不提供默认值,它将 return None
.
cpf = user_account[1].get("CPF")
if cpf:
plpy.debug("It has CPF")
另一种方法是使用 in
运算符。它可以告诉你某个键是否存在于字典中。
if "CPF" in user_account[1]:
plpy.debug("It has CPF")
这与做的一样:
if "CPF" in user_account[1].keys():
plpy.debug("It has CPF")