Python ldap函数在flask中调用时的行为不同

Python ldap函数在flask中调用时的行为不同,python,flask,ldap,python-ldap,Python,Flask,Ldap,Python Ldap,因此,我正在尝试使用python ldap和flask实现自动完成。 下面是一个测试脚本ldapA.py: import ldap #first you must open a connection to the server def query(): try: l = ldap.open("server.net") ## searching doesn't require a bind in LDAP V3. If you're using L

因此,我正在尝试使用python ldap和flask实现自动完成。 下面是一个测试脚本ldapA.py:

import ldap

#first you must open a connection to the server

def query():
    try:
        l = ldap.open("server.net")


        ## searching doesn't require a bind in LDAP V3.  If you're using LDAP v2, set the next line appropriately
        ## and do a bind as shown in the above example.
        # you can also set this to ldap.VERSION2 if you're using a v2 directory
        # you should  set the next option to ldap.VERSION2 if you're using a v2 directory
        l.protocol_version = ldap.VERSION3
        l.set_option(ldap.OPT_REFERRALS, 0)
        username="CN=user user,OU=bbbgbg,OU=bdbfd,DC=dsffd,DC=net"
        passw="adsada"

        l.simple_bind_s(username,passw)
    except ldap.LDAPError, e:
        print e
        # handle error however you like


    ## The next lines will also need to be changed to support your search requirements and directory
    baseDN = "ou=xds, ou=sd, dc=sd, dc=net"
    searchScope = ldap.SCOPE_SUBTREE
    ## retrieve all attributes - again adjust to your needs - see documentation for more options
    retrieveAttributes = ['name'] 
    searchFilter = "name=*jace*"

    try:
        ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
        result_set = []
        while 1:
            result_type, result_data = l.result(ldap_result_id, 0)
            if (result_data == []):
                break
            else:
                ## here you don't have to append to a list
                ## you could do whatever you want with the individual entry
                ## The appending to list is just for illustration. 
                if result_type == ldap.RES_SEARCH_ENTRY:
                    result_set.append(result_data)
                res = result_set[0]
                res1 = res[0]
                res2 = res1[1]
                res3 = res2["name"]
        print res3[0]
    except ldap.LDAPError, e:
        print e


query()
当我运行它时,它按预期工作。广告上写着我的名字

现在,当我从烧瓶中这样称呼它:

from flask import render_template
from app import app
from flask import request
from ldapA import query
@app.route('/')
@app.route('/index')
def index():
  return render_template("index.html")

@app.route('/autocomplete', methods=['GET'])
def autocomplete():
    return query()
我得到:

  127.0.0.1 - - [19/Jul/2017 14:08:58] "GET /autocomplete HTTP/1.1" 500 -
    Traceback (most recent call last):
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
        return self.wsgi_app(environ, start_response)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
        response = self.handle_exception(e)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
        reraise(exc_type, exc_value, tb)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
        response = self.full_dispatch_request()
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
        rv = self.dispatch_request()
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
      File "/home/jgar/receptionsignin/app/views.py", line 13, in autocomplete
        return query()
      File "/home/jgar/receptionsignin/ldapA.py", line 36, in query
        result_type, result_data = l.result(ldap_result_id, 0)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/ldap/ldapobject.py", line 703, in result
        resp_type, resp_data, resp_msgid = self.result2(msgid,all,timeout)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/ldap/ldapobject.py", line 707, in result2
        resp_type, resp_data, resp_msgid, resp_ctrls = self.result3(msgid,all,timeout)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/ldap/ldapobject.py", line 714, in result3
        resp_ctrl_classes=resp_ctrl_classes
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/ldap/ldapobject.py", line 734, in result4
        resp_data = self._bytesify_results(resp_data, with_ctrls=add_ctrls)
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/ldap/ldapobject.py", line 266, in _bytesify_results
        for (dn, fields) in results
      File "/home/jgar/receptionsignin/flask/lib/python2.7/site-packages/ldap/ldapobject.py", line 219, in _maybe_rebytesify_text
        assert isinstance(value, text_type), "Should return text, got bytes instead (%r)" % (value,)
    AssertionError: Should return text, got bytes instead ('CN=sdsddsc,OU=sds,OU=sds,DC=sdds,DC=net')
我知道这条线路引起了麻烦:

ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
但我不明白为什么在flask中调用时会发生这种情况,而在使用pythonldapa.py运行时不会发生这种情况。 这似乎是ldap lib内部错误,但为什么它只发生在flask中,我如何修复它?
谢谢大家

对于任何有相同错误的人来说,这都是一个unicode问题: 转到ldpaobject.py(安装了python ldap的服务器) 改变

公正

text_type = str

python ldap现在应该可以在flask中工作了

对于任何有相同错误的人来说,这都是一个unicode问题: 转到ldpaobject.py(安装了python ldap的服务器) 改变

公正

text_type = str

python ldap现在应该可以在flask中工作了

在查询方法中不返回任何内容。在flask autocomplete方法中,您返回的是查询结果,这是什么?如果你想得到打印结果,把它放在一个变量中并返回它。@ USER 140888.我可以在函数中添加一些返回,但是它仍然给出相同的错误,这只是出于调试的目的,它甚至不会返回,因为它在中间抛出异常。在查询方法中,你不返回任何东西。在flask autocomplete方法中,您返回的是查询结果,这是什么?如果你想得到打印结果,把它放在一个变量中并返回它。@ USER 140888.我可以在函数中添加一些返回,但是它仍然给出相同的错误,这只是出于调试的目的,它甚至不会返回,因为它在中间抛出异常。