Python:如何设置 python-ldap 以忽略引用?

标签 python python-2.7 ldap referrals python-ldap

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

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 upper 时,问题发生了。当我搜索“ou=xx,dc=xxx,dc=com”时,它开始在我的生产环境中卡住,而在开发环境中一切正常。当我开始查看它时,我发现它在推荐分支上卡住了。我怎样才能告诉 python-ldap 忽略推荐?上面的代码无法正常工作。

最佳答案

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

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)

关于Python:如何设置 python-ldap 以忽略引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146390/

相关文章:

python - ComboBox 函数 currentIndexChanged 无法正常工作

python - 指数平滑平均

python - iPython Notebook 不将 Dataframe 打印为表格

python - django 中的类别和子类别

python - django-scarface 无法在 python 2.7 中工作

Python Matplotlib Twinx() 光标值

angularjs - $http.post Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers 错误

java - 如何匿名访问 Windows Active Directory?

java - 使用 JNDI 查找 RACF 用户数据

Java (Groovy) LDAP 非 ASCII 字符