Python:如何设置pythonldap来忽略引用?

2024-09-26 22:43:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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作为输出。在

我想要的是pytholdap可以跳过它或忽略它而不抛出奇怪的异常(我找不到关于它的文档)?在

(这可能有帮助也可能没有)问题发生在我在树的上部搜索baseDN时。当我搜索'ou=xx,dc=xxx,dc=com'时,它开始冻结在我的生产环境中,而在开发env上一切都很好。当我开始看它时,我发现它冻结在转介分支机构。如何告诉pytholdap忽略引用?上面的代码没有我想要的那样工作。在


Tags: 代码importcomauthouconnectiondcfilter
1条回答
网友
1楼 · 发布于 2024-09-26 22:43:32

这是一个有效的例子,看看是否有用。在

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)

相关问题 更多 >

    热门问题