Python Forum
Ldap3 Python print(conn.entries) doesnt work - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Ldap3 Python print(conn.entries) doesnt work (/thread-36653.html)

Pages: 1 2


Ldap3 Python print(conn.entries) doesnt work - ilknurg - Mar-14-2022

I have the code below for ldap search.
I want to save my search result as json.
why
print(conn.entries)
doesnt work?




from ldap3 import Server, Connection, ALL
from ldap3 import Server, Connection, SAFE_SYNC

username = "user1"
server = Server('192.168.1.24', get_info = ALL)
conn = Connection(server, '[email protected]', 'mypassword', client_strategy=SAFE_SYNC, auto_bind=True)
#print(conn)

result= conn.search('dc=my,dc=server', f'(sAMAccountName={username})')
print(conn.entries)



RE: Ldap3 Python print(conn.entries) doesnt work - Gribouillis - Mar-14-2022

What's in the 'result' object? Have you tried printing result, or dir(result)?


RE: Ldap3 Python print(conn.entries) doesnt work - ilknurg - Mar-15-2022

(Mar-14-2022, 05:54 PM)Gribouillis Wrote: What's in the 'result' object? Have you tried printing result, or dir(result)?

When i run print(result) ı get the search result that i want.
But i cannot parse it. I try to convert the result to json for parsing but i get typeerror: object of type 'bytes' is not json serializable


RE: Ldap3 Python print(conn.entries) doesnt work - Gribouillis - Mar-15-2022

So you want to convert a search result which is a bytes string to json. What does the bytes string look like? Can you post it here? Also how have you tried to convert it to json?


RE: Ldap3 Python print(conn.entries) doesnt work - ilknurg - Mar-15-2022

(Mar-15-2022, 07:25 AM)Gribouillis Wrote: So you want to convert a search result which is a bytes string to json. What does the bytes string look like? Can you post it here? Also how have you tried to convert it to json?

Here is my search result:


(True, {'result': 0, 'description': 'success', 'dn': '', 'message': '', 'referrals': None, 'type': 'searchResDone'}, [{'raw_dn': b'CN=user G\xc3\xbc\xc3\xa7l\xc3\xbc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local', 'dn': 'CN=User1,OU=DEVELOPER,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local', 'raw_attributes': {}, 'attributes': {}, 'type': 'searchResEntry'}, {'uri': ['ldap://ForestDnsZones.TRD.local/DC=ForestDnsZones,DC=TRD,DC=local'], 'type': 'searchResRef'}, {'uri': ['ldap://DomainDnsZones.TRD.local/DC=DomainDnsZones,DC=TRD,DC=local'], 'type': 'searchResRef'}, {'uri': ['ldap://TRD.local/CN=Configuration,DC=TRD,DC=local'], 'type': 'searchResRef'}], {'base': 'dc=my,dc=server', 'scope': 2, 'dereferenceAlias': 3, 'sizeLimit': 0, 'timeLimit': 0, 'typesOnly': False, 'filter': '(sAMAccountName=user1)', 'attributes': ['1.1'], 'type': 'searchRequest', 'controls': None})
I've try:

result= conn.search('dc=my,dc=server', f'(sAMAccountName={username})')
print(result)
result2 = json.dump(result)
print(result2)
I got:

TypeError: Object of type 'bytes' is not JSON serializable


RE: Ldap3 Python print(conn.entries) doesnt work - Gribouillis - Mar-15-2022

It fails because the dictionary in the result contains a byte string b'CN=user G\xc3\xbc\xc3\xa7l\xc3\xbc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local'. A solution is to decode it to unicode with the utf8 encoding. Here is working code that I found with my search engine
result = (True, {'result': 0, 'description': 'success', 'dn': '', 'message': '', 'referrals': None, 'type': 'searchResDone'}, [{'raw_dn': b'CN=user G\xc3\xbc\xc3\xa7l\xc3\xbc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local', 'dn': 'CN=User1,OU=DEVELOPER,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local', 'raw_attributes': {}, 'attributes': {}, 'type': 'searchResEntry'}, {'uri': ['ldap://ForestDnsZones.TRD.local/DC=ForestDnsZones,DC=TRD,DC=local'], 'type': 'searchResRef'}, {'uri': ['ldap://DomainDnsZones.TRD.local/DC=DomainDnsZones,DC=TRD,DC=local'], 'type': 'searchResRef'}, {'uri': ['ldap://TRD.local/CN=Configuration,DC=TRD,DC=local'], 'type': 'searchResRef'}], {'base': 'dc=my,dc=server', 'scope': 2, 'dereferenceAlias': 3, 'sizeLimit': 0, 'timeLimit': 0, 'typesOnly': False, 'filter': '(sAMAccountName=user1)', 'attributes': ['1.1'], 'type': 'searchRequest', 'controls': None})

import json

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8');
        return json.JSONEncoder.default(self, obj)

s = json.dumps(result, cls=MyEncoder, indent=4)

print(s)
Output:
[ true, { "result": 0, "description": "success", "dn": "", "message": "", "referrals": null, "type": "searchResDone" }, [ { "raw_dn": "CN=user G\u00fc\u00e7l\u00fc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD -GLOBAL,DC=TRD,DC=local", "dn": "CN=User1,OU=DEVELOPER,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local", "raw_attributes": {}, "attributes": {}, "type": "searchResEntry" }, { "uri": [ "ldap://ForestDnsZones.TRD.local/DC=ForestDnsZones,DC=TRD,DC=local" ], "type": "searchResRef" }, { "uri": [ "ldap://DomainDnsZones.TRD.local/DC=DomainDnsZones,DC=TRD,DC=local" ], "type": "searchResRef" }, { "uri": [ "ldap://TRD.local/CN=Configuration,DC=TRD,DC=local" ], "type": "searchResRef" } ], { "base": "dc=my,dc=server", "scope": 2, "dereferenceAlias": 3, "sizeLimit": 0, "timeLimit": 0, "typesOnly": false, "filter": "(sAMAccountName=user1)", "attributes": [ "1.1" ], "type": "searchRequest", "controls": null } ]
You could perhaps serialize only result[1] because I think the True value at the start of the result is not part of the result's payload. Also note that this solution may encounter other issues if the result contains bytes data encoded in another encoding than utf8 or raw bytes data that don't represent unicode strings.


RE: Ldap3 Python print(conn.entries) doesnt work - ilknurg - Mar-15-2022

(Mar-15-2022, 08:35 AM)Gribouillis Wrote: It fails because the dictionary in the result contains a byte string b'CN=user G\xc3\xbc\xc3\xa7l\xc3\xbc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local'. A solution is to decode it to unicode with the utf8 encoding. Here is working code that I found with my search engine
result = (True, {'result': 0, 'description': 'success', 'dn': '', 'message': '', 'referrals': None, 'type': 'searchResDone'}, [{'raw_dn': b'CN=user G\xc3\xbc\xc3\xa7l\xc3\xbc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local', 'dn': 'CN=User1,OU=DEVELOPER,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local', 'raw_attributes': {}, 'attributes': {}, 'type': 'searchResEntry'}, {'uri': ['ldap://ForestDnsZones.TRD.local/DC=ForestDnsZones,DC=TRD,DC=local'], 'type': 'searchResRef'}, {'uri': ['ldap://DomainDnsZones.TRD.local/DC=DomainDnsZones,DC=TRD,DC=local'], 'type': 'searchResRef'}, {'uri': ['ldap://TRD.local/CN=Configuration,DC=TRD,DC=local'], 'type': 'searchResRef'}], {'base': 'dc=my,dc=server', 'scope': 2, 'dereferenceAlias': 3, 'sizeLimit': 0, 'timeLimit': 0, 'typesOnly': False, 'filter': '(sAMAccountName=user1)', 'attributes': ['1.1'], 'type': 'searchRequest', 'controls': None})

import json

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8');
        return json.JSONEncoder.default(self, obj)

s = json.dumps(result, cls=MyEncoder, indent=4)

print(s)
Output:
[ true, { "result": 0, "description": "success", "dn": "", "message": "", "referrals": null, "type": "searchResDone" }, [ { "raw_dn": "CN=user G\u00fc\u00e7l\u00fc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD -GLOBAL,DC=TRD,DC=local", "dn": "CN=User1,OU=DEVELOPER,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local", "raw_attributes": {}, "attributes": {}, "type": "searchResEntry" }, { "uri": [ "ldap://ForestDnsZones.TRD.local/DC=ForestDnsZones,DC=TRD,DC=local" ], "type": "searchResRef" }, { "uri": [ "ldap://DomainDnsZones.TRD.local/DC=DomainDnsZones,DC=TRD,DC=local" ], "type": "searchResRef" }, { "uri": [ "ldap://TRD.local/CN=Configuration,DC=TRD,DC=local" ], "type": "searchResRef" } ], { "base": "dc=my,dc=server", "scope": 2, "dereferenceAlias": 3, "sizeLimit": 0, "timeLimit": 0, "typesOnly": false, "filter": "(sAMAccountName=user1)", "attributes": [ "1.1" ], "type": "searchRequest", "controls": null } ]
You could perhaps serialize only result[1] because I think the True value at the start of the result is not part of the result's payload. Also note that this solution may encounter other issues if the result contains bytes data encoded in another encoding than utf8 or raw bytes data that don't represent unicode strings.


Thank you. I try the code i have an error message.

TypeError: Object of type 'CaseInsensitiveDict' is not JSON serializable


RE: Ldap3 Python print(conn.entries) doesnt work - Gribouillis - Mar-15-2022

Try to convert it to dict for example
from ldap3.utils.ciDict import CaseInsensitiveDict

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            obj = str(obj, encoding='utf-8')
        elif isinstance(obj, CaseInsensitiveDict):
            obj = dict(obj)
        return json.JSONEncoder.default(self, obj)



RE: Ldap3 Python print(conn.entries) doesnt work - ilknurg - Mar-15-2022

(Mar-15-2022, 09:45 AM)Gribouillis Wrote: Try to convert it to dict for example
from ldap3.utils.ciDict import CaseInsensitiveDict

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            obj = str(obj, encoding='utf-8')
        elif isinstance(obj, CaseInsensitiveDict):
            obj = dict(obj)
        return json.JSONEncoder.default(self, obj)

my mind is so confused. I run this

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8');
        elif isinstance(obj, CaseInsensitiveDict):
            return dict(obj)
        return json.JSONEncoder.default(self, obj) 
s = json.dumps(result, cls=MyEncoder, indent=4)
 
print(s)
I get this error
elif isinstance(obj, CaseInsensitiveDict):
TypeError: isinstance() arg 2 must be a type or tuple of types



RE: Ldap3 Python print(conn.entries) doesnt work - Gribouillis - Mar-15-2022

What is the type of CaseInsensitiveDict ?