我使用Python在MS Active Directory中搜索,我偶然发现了我得到的答案的编码问题。
我使用下面的代码递归遍历AD
import ldap
import pickle
class SearchAD():
def __init__(self):
self.l = ldap.initialize("ldap://ldap.example.com")
self.l.protocol_version = ldap.VERSION3
self.l.set_option(ldap.OPT_REFERRALS, 0)
bind = self.l.simple_bind_s("user", "password")
self.base = "DC=example,DC=com"
self.all = list()
def searchmgr(self, criteria, m):
print criteria
m += 1
attributes = ['dn', 'title']
result = self.l.search_s(self.base, ldap.SCOPE_SUBTREE, u'manager='+criteria, attributes)
for u in result:
cn, t = u
if cn is not None and "Disabled Users" not in cn and t.get('title'):
self.all.append({'dn': cn, 'title': t['title'][0], 'm': m})
self.searchmgr(cn, m)
s = SearchAD()
s.searchmgr("CN=EXAMPLE Top,DC=example,DC=com", 0)
with open("dir.pickle", "wb") as f:
pickle.dump(s.all, f)
并获取
UnicodeDecodeError:'ascii'编解码器无法将字节0xc3解码到位 16:序数不在范围内(128)
致电
result = self.l.search_s(self.base, ldap.SCOPE_SUBTREE, 'manager='+criteria, attributes)
搜索工作正常(我通过print
得到预期的输出),直到字符串'CN=DOE Marie-h\xc3\xa9l\xc3\xa8ne,OU=User Accounts,...'
转换为'CN=DOE Marie-hélène,OU=User Accounts,...'
。
预计为AD returns its results in UTF-8
因此我尝试.encode('utf-8')
字符串并使用
result = self.l.search_s(self.base, ldap.SCOPE_SUBTREE, u'manager='+criteria.encode('utf-8'), attributes)
但我得到同样的错误。
我该如何处理AD返回的结果?