python ldap查询错误

问题描述 投票:0回答:1

我正在尝试使用python代码运行基本的ldap查询并获取错误。请帮助我解决此问题。我已经尝试将代码从Python 2转换为Python 3,因为有人说我是问题之一。

(project1_env) [root@localhost python-ldap]# cat script1.py

import ldap

## first you must open a connection to the server
try:
        l = ldap.open("127.0.0.1")
        ## searching doesn't require a bind in LDAP V3.  If you're using LDAP v2, set the next line appropriately
        ## and do a bind as shown in the above example.
        # you can also set this to ldap.VERSION2 if you're using a v2 directory
        # you should  set the next option to ldap.VERSION2 if you're using a v2 directory
        l.protocol_version = ldap.VERSION3
except ldap.LDAPError as e:
        print (e)
        # handle error however you like


## The next lines will also need to be changed to support your search requirements and directory
baseDN = "ou=Users, dc=afik, dc=com"
searchScope = ldap.SCOPE_SUBTREE
## retrieve all attributes - again adjust to your needs - see documentation for more options
retrieveAttributes = None
searchFilter = "cn=hr_user1"

try:
        ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
        result_set = []
        while 1:
                result_type, result_data = l.result(ldap_result_id, 0)
                if (result_data == []):
                        break
                else:
                        ## here you don't have to append to a list
                        ## you could do whatever you want with the individual entry
                        ## The appending to list is just for illustration.
                        if result_type == ldap.RES_SEARCH_ENTRY:
                                result_set.append(result_data)
        print (result_set)
except ldap.LDAPError as e:
        print (e)
(project1_env) [root@localhost python-ldap]# python script1.py
Traceback (most recent call last):
  File "script1.py", line 9, in <module>
    l = ldap.open("127.0.0.1")
AttributeError: module 'ldap' has no attribute 'open'
(project1_env) [root@localhost python-ldap]#
python openldap
1个回答
0
投票

ldap.open在3.1版之后被删除(请参见https://www.python-ldap.org/en/python-ldap-3.2.0/reference/ldap.html)。检查您使用的版本,然后降级到python-ldap的3.1之前的版本,或者更新您使用的功能。

© www.soinside.com 2019 - 2024. All rights reserved.