【问题标题】:Python: how to setup python-ldap to ignore referrals?Python:如何设置 python-ldap 以忽略推荐?
【发布时间】:2013-08-11 08:49:57
【问题描述】:

如何避免在以下代码中出现(未记录的)异常?

import ldap
import ldap.sasl

connection = ldap.initialize('ldaps://server:636', trace_level=0)
connection.set_option(ldap.OPT_REFERRALS, 0)
connection.protocol_version = 3
sasl_auth = ldap.sasl.external()
connection.sasl_interactive_bind_s('', sasl_auth)

baseDN = 'ou=org.com,ou=xx,dc=xxx,dc=com'
filter = 'objectclass=*'
try:
  result = connection.search_s(baseDN, ldap.SCOPE_SUBTREE, filter)
except ldap.REFERRAL, e:
  print "referral"
except ldap.LDAPError, e:
  print "Ldaperror"

示例中给出的 baseDN 恰好是一个引用。当我运行这段代码时,我得到referral 作为输出。

我想要的是 python-ldap 只是跳过它或忽略它而不抛出奇怪的异常(我找不到关于它的文档)?

(这可能有帮助或没有帮助)当我在树中搜索 baseDN 上层时发生了问题。当我搜索'ou = xx,dc = xxx,dc = com'时,它开始在我的生产环境中冻结,而在开发环境中一切正常。当我开始查看它时,我发现它在推荐分支上冻结了。如何告诉 python-ldap 忽略推荐?上面的代码不能按我的意愿工作。

【问题讨论】:

    标签: python python-2.7 ldap referrals python-ldap


    【解决方案1】:

    这是一个工作示例,看看它是否有帮助。

    def ldap_initialize(remote, port, user, password, use_ssl=False, timeout=None):
        prefix = 'ldap'
        if use_ssl is True:
            prefix = 'ldaps'
            # ask ldap to ignore certificate errors
            ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
    
        if timeout:
            ldap.set_option(ldap.OPT_NETWORK_TIMEOUT, timeout)
    
        ldap.set_option(ldap.OPT_REFERRALS, ldap.OPT_OFF)
        server = prefix + '://' + remote + ':' + '%s' % port
        l = ldap.initialize(server)
        l.simple_bind_s(user, password)
    

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多