Newbie and encoding

Bjorn Ove Grotan bjorn.grotan at
Mon Feb 19 09:18:06 CET 2007

jphthierry at
> Hi,
> I was used to work with phpldapadmin, then discovered luma. Now I need something
> more specific to my needs, and thus interested in python-ldap.
> Starting was really easy :-) Unfortunately, I discovered a bug in my application
> this week-end: I can not use 8 bits chars in search or add functions while I
> have no issue with these characters in Luma :-( It seems to be related to string
> encoding. Luma makes an intensive use of the "unicode" function. So I tried that
> piece of code but without success:
> >>> import ldap
> >>>'localhost.localdomain')
> >>> CON.simple_bind_s('','')
> >>> CON.search_s('ou=amis,dc=localdomain',ldap.SCOPE_SUBTREE,u'(cn=th?)')


> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 6:
> ordinal not in range(128)

LDAP uses utf8 - so convert any umlauts or whatever to utf8 before doing a query.
Mostly anything can be converted _to_ utf8, but not will convert back to the 
encoding you came from. 

def utf8_encode(str):
    return unicode(str, "iso-8859-1").encode("utf-8")

def utf8_decode(str):
    return unicode(str, "utf-8").encode("iso-8859-1")

filter = utf8_encode('cn=Bjørn Ove Grøtan')

Just change from iso-8859-1 to whatever encoding you use by default.

Bjørn Ove Grøtan
Luma Debian Maintainer

More information about the python-ldap mailing list