[python3-ldap] issue extracting jpegPhoto from LDAP

Николай Сайко kaisernikola at yandex.ru
Sun Jul 21 21:42:26 CEST 2013


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'], raw=True)
              }

- 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']), decodeVals(attribute['vals'])))
+       attributes.append((str(attribute['type']), [conv(attribute['vals']) for val in vals if val] or None)) 
    return attributes


Best Regards, 
Nikolay Saiko


More information about the python3-ldap mailing list