Hello everyone, please tell me how to properly consider the exceptions associated with errors when using the ldap3 module

from pprint import pprint from ldap3 import Connection, Server, NTLM, ALL, ALL_ATTRIBUTES def search(ip,bind_name, bind_pwd, search_base, search_filter, attrs=None): server = Server(f'ldap://{ip}', get_info=ALL) try: with Connection(server, bind_name, bind_pwd, auto_bind=True,) as conn: conn.search(search_base, search_filter, attributes=attrs) out = conn.entries return out except ldap3.core.exceptions as err: pprint (err) 

throws an error builtins.NameError: name 'ldap3' is not defined

  • And where did the error occur? Which line? Add a stack trace to the question. Shl, most likely you have a problem in ldap3.core.exceptions , because you did import from ldap3, and not import it, so the identifier ldap3 does not exist - gil9red
  • it seems like, and what then you need to import and how to raise exceptions so that it is correct? - Andrei
  • You better know what you want to import :) Well, so as not to swear at you like that, then at least import ldap3 - gil9red
  • import ldap3 except ldap3.core.exceptions.LDAPBindError as err: works, I can not figure out how to determine the import, so as not to import the entire module, tell me? - Andrei
  • one
    For example: from ldap3.core.exceptions import LDAPBindError , then except LDAPBindError as e - gil9red

1 answer 1

ldap3 was decided to import an exception from ldap3 directly, bypassing modules and nested modules:

 from ldap3.core.exceptions import LDAPBindError 

Then the exception handling would be:

 except LDAPBindError as err: