[python3-ldap] issue extracting jpegPhoto from LDAP

Николай Сайко kaisernikola at yandex.ru
Mon Jul 22 08:54:43 CEST 2013


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']), decodeVals(attribute['vals'])))
+         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'], raw=True)



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'], 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