【问题标题】:Getting names of groups in Windows Active Directory using Python使用 Python 在 Windows Active Directory 中获取组的名称
【发布时间】:2012-05-10 17:30:34
【问题描述】:

我正在使用这个库在 Python 中与 Active Directory 进行交互:

http://timgolden.me.uk/python/ad_cookbook.html

我正在尝试像这样访问组名:

groups = []
for group in active_directory.search(objectClass='group'):
    groups.append(str(group.cn))

我的第一个问题是group.cn 获取组的显示名称而不是实际的对象名称。如何获取对象名称?

我的第二个问题是运行此代码会占用大量内存。当 Active Directory 中有数千个组时,我的程序将使用数百兆甚至一两个演出的内存。当有组嵌套在其他组中时尤其如此。在我获得所有组名之后,我仍然使用所有这些内存有什么原因吗?

【问题讨论】:

  • 我会尝试ldap模块,看看内存性能是否更好python-ldap.org
  • @dm03514 我尝试使用ldap 模块,但无法连接到我的服务器。

标签: python active-directory python-2.7


【解决方案1】:

这就是我最终做的:

    results = None
    try:    
        connection = ldap.open(str(self.hostnameLineEdit.text()))
        connection.simple_bind_s(str(self.usernameLineEdit.text()), str(self.passwordLineEdit.text()))
        userDNSDomain = os.environ['USERDNSDOMAIN']
        userDNSDomain = userDNSDomain.split('.')
        base = ""
        for dc in userDNSDomain:
            base += "dc=" + dc + ","
        base = base[:-1]
        #print base
        resultID = connection.search(base,ldap.SCOPE_SUBTREE,'(objectClass=group)')
        resultTypes, results = connection.result(resultID, 0)
    except ldap.LDAPError, e:
        self.messageBox("LDAP Error: " + str(e))
    if results != None:
        while results[0][0] != None:
            #print results[0][1]['cn']
            self.groupsListWidget.addItem(QString(results[0][1]['cn'][0]))
            resultTypes, results = connection.result(resultID, 0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多