[python3-ldap] issue extracting jpegPhoto from LDAP

python3ldap python3ldap at gmail.com
Mon Jul 22 12:22:41 CEST 2013

Hello Nikolay,
thanks for your suggestion, I'm aware of the raw bytes vs str problem. I'd
like to address it in two ways:

1. I'd like to have an "autoDiscover" in the server object) to find
attributes syntax mapping (accessing the schema on the server)

2. define an additional parameter in the attribute list to specify the
requested decoding of the attribute.

The latter is almost the same of your proposal.I think I will implement it
soon, now I'm working on referrarls. I should have a working alpha release
for tomorrow.

Thanks for your support and appreciation.


Date: Mon, 22 Jul 2013 10:54:43 +0400
From: ??????? ????? <kaisernikola at yandex.ru>
To: "python3-ldap at python.org" <python3-ldap at python.org>
Subject: Re: [python3-ldap] issue extracting jpegPhoto from LDAP
Message-ID: <676511374476083 at web15f.yandex.ru>
Content-Type: text/plain; charset=koi8-r

Sorry, there were errors in my previous code. It shall be like this:

line 286:
- def decodeAttributeList(attributeList):
+ def decodeAttributeList(attributeList, raw=False):
+     conv = bytes if raw else str

line 289
-          attributes.append((str(attribute['type']),
+         attributes.append((str(attribute['type']), [conv(val) for val in
attribute['vals'] if val] or None))

line 383-384
-                'attrs': decodeAttributeList(response['attributes'])
+               'attrs': decodeAttributeList(response['attributes']),
+               'byte_attrs': decodeAttributeList(response['attributes'],

21.07.2013, 23:42, "??????? ?????" <kaisernikola at yandex.ru>:
> Greetings!
> ldap3 module is very useful & convinient, great thanks for it!
> But I found problem when trying to extract "jpegPhoto" attr from LDAP
record. In my case it is raw bytes of jpeg image - but in ldap3 ALL data is
"str" (which is unicode-type in python3), not bytes.
> So, thanks to encoding from "utf-8" and back, jpegPhoto becomes stripped
from beginning & some bytes inside are corrupted too :(
> In most cases str instaed of bytes is quite convinient, but solution for
my case may be needed...
> I made a small hack to fix it without breaking other module`s logic: add
attribute in searchResultEntryResponseToDict with raw bytes copy of data:
> def searchResultEntryResponseToDict(response):
> ????return {
> ??????????????'dn': str(response['object']),
> ??????????????'attrs': decodeAttributeList(response['attributes']),
> + ???????????'byte_attrs': decodeAttributeList(response['attributes'],
> ??????????????}
> - def decodeAttributeList_b(attributeList):
> + def decodeAttributeList_b(attributeList, raw=False):
> ????attributes = []
> + ?conv = bytes if raw else str
> ????for attribute in attributeList:
> - ???????attributes.append((str(attribute['type']),
> + ??????attributes.append((str(attribute['type']),
[conv(attribute['vals']) for val in vals if val] or None))
> ????return attributes
> Best Regards,
> Nikolay Saiko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python3-ldap/attachments/20130722/92c73d52/attachment.html>

More information about the python3-ldap mailing list