Python:如何设置python-ldap以忽略引用? [英] Python: how to setup python-ldap to ignore referrals?

查看:160
本文介绍了Python:如何设置python-ldap以忽略引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how can I avoid getting (undocumented) exception in following code?

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

It happens that baseDN given in example is a referral. When I run this code I get referral as output.

我想要的是python-ldap会跳过它或忽略它而不会引发奇怪的异常(我找不到有关它的文档)?

What would I want is that python-ldap just would skip it or ignore without throwing strange exception (I cannot find documentation about it)?

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

(this may help or not) The problem happened when I was searching baseDN upper in a tree. When I was searching 'ou=xx,dc=xxx,dc=com' it started to freeze on my production env when on development env everything works great. When I started to looking at it I found that it freezing on referral branches. How can I tell python-ldap to ignore referrals? Code above does not work as I want.

推荐答案

这是一个有效的示例,请查看是否有帮助.

This is a working example, see if it helps.

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以忽略引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆