当在烧瓶中调用时,Python ldap函数的行为有所不同

问题描述:

所以我试图使用python-ldap和flask来自动完成。 这是一个测试脚本ldapA.py:当在烧瓶中调用时,Python ldap函数的行为有所不同

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) 

但我m难以理解为什么它在瓶子中调用时发生,而不是在用python ldapA.py运行时发生。 它似乎是ldap lib内部错误,但为什么它只发生在烧瓶中,我怎么修复它? 谢谢你们!

+0

您不会在查询方法中返回任何内容。在你的瓶子自动填充方法中,你正在返回查询的结果,这是什么?如果你想获得打印结果,把它放在一个变量中并返回。 – user1408786

+0

@ user1408786我可以在函数中添加一些返回值,但它仍然会给出相同的错误,这只是为了调试目的,它甚至从不会返回到返回值,因为它会在中间引发异常。 –

原来这是一个unicode问题,对于具有相同的错误的人: 转到ldpaobject.py(whererver你已经安装了Python-LDAP) 变化

if PY2: 
    text_type = unicode 
else: 
    text_type = str 

只是

text_type = str 

python-ldap现在应该在烧瓶中工作