Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UWSGI是否影响来自底层Django应用程序的LDAPS请求?_Python_Django_Active Directory_Uwsgi_Python Ldap - Fatal编程技术网

Python UWSGI是否影响来自底层Django应用程序的LDAPS请求?

Python UWSGI是否影响来自底层Django应用程序的LDAPS请求?,python,django,active-directory,uwsgi,python-ldap,Python,Django,Active Directory,Uwsgi,Python Ldap,我在django中插入了这个类作为身份验证后端 class ActiveDirectoryBackend(object): logger = Messenger.infrastructure @classmethod def authenticate(cls, username=None, password=None): try: bind_dn = "%s@%s" % (username, settings.AD_DNS_N

我在django中插入了这个类作为身份验证后端

class ActiveDirectoryBackend(object):

    logger = Messenger.infrastructure

    @classmethod
    def authenticate(cls, username=None, password=None):

        try:
            bind_dn = "%s@%s" % (username, settings.AD_DNS_NAME)

            try:
                cls.logger.debug('Initializing: %s' % settings.AD_LDAP_URL)
                l = ldap.initialize(settings.AD_LDAP_URL)
                l.protocol_version = ldap.VERSION3
                l.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
                cls.logger.debug('Binding user %s' % bind_dn)
                l.simple_bind_s(bind_dn, password)
                result = l.search_ext_s(settings.AD_SEARCH_DN, ldap.SCOPE_SUBTREE,
                                        "sAMAccountName=%s" % username, settings.AD_SEARCH_FIELDS)[0][1]
                email = result.get('mail', (None,))[0]
            except ldap.SERVER_DOWN, ex:
                cls.logger.warning('LDAP-Sever Down (%s)' % ex.message)
                raise PermissionDenied
            except ldap.INVALID_CREDENTIALS:
                cls.logger.warning('LDAP-Server: Rejected login for user %s due to invalid credentials' % username)
                raise PermissionDenied

            cls.logger.debug('User %s was successfully authorized.' % username)

            l.unbind_s()

        except Exception:
            raise PermissionDenied

        try:
            user = User.objects.get(email=email)
            cls.logger.debug('User %s found for email %s ' % (user.username, email))

        except ObjectDoesNotExist, ex:
            cls.logger.debug('User for email %s could not be found.' % email)
            raise PermissionDenied

        return user
整个过程都在Apache+uWSGI下运行。 当我运行Django standalone(manage.py runserver)时,LDAP和LDAPS的一切都很好

但当在uWSGI和LDAPS下运行它时,它总是抛出“服务器关闭”。 LDAP(没有)工作正常。 使用tcpdump,我可以看到数据包在AD和我的服务器之间双向传输

uWSGI如何影响python ldap或底层库与Active Directory之间的LDAPS通信?

uWSGI在生成其工作程序时默认会进行一些操作

uWSGI尝试(ab)使用fork()调用的Copy-On-Write语义 只要可能。默认情况下,它将在加载 应用程序共享尽可能多的内存。如果这 由于某些原因,这种行为是不受欢迎的,请使用lazy apps选项。 这将指示uWSGI在每个工作人员完成任务后加载应用程序 fork()


根据我的经验,这确实会影响一些库(比如FreeTDS)。尝试上面提到的“惰性应用程序”选项。

通过使用命令行ldapsearch上的选项,我和一位同事找到了解决问题的方法,但不是问题的确切原因

通过显式设置主机选项,我们使它工作起来

在python中,它如下所示:

l.set_option(ldap.OPT_HOST_NAME, settings.AD_DNS_NAME)