Last time has added and retrieved LDAP. This time, I will summarize how to add and get using Writer and Reader.
ldap3 has a Reader class with various functions. Use it to get LDAP information. Reader requires a connection, an object, and a cn (search path). The examples are basically listed in order from the top.
The object is created by passing the target object name and connection to the ObjectDef class. This time, we want to take the value of cn, so specify inetOrgPerson
. When you want to get ou, specify the target you want to get, such as organizationalUnit
.
main.py
from ldap3 import Server, Connection, ObjectDef, Reader
server = Server('localhost')
conn = Connection(server, 'cn=admin,dc=sample-ldap', password='LdapPass')
result = conn.bind()
#inetOrgPerson object creation
obj_cn_name = ObjectDef('inetOrgPerson', conn)
Create a Reader by giving the object, connection, and search path created earlier. The search path given at this time allows you to specify from which hierarchy information can be obtained. Here, the value is not entered because it is not searched only by generating Reader.
main.py
#Leader generation
data_reader = Reader(conn, obj_cn_name, 'ou=sample-unit,dc=sample-component,dc=sample-ldap')
You can get a list of LDAP values by using the reader `search ()`
.
main.py
#Search here
data = data_reader.search()
#All items can be taken
print(data)
result
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-27T20:50:15.470086
cn: sample-name
objectClass: inetOrgPerson
sn: sample1
sample2
st: test2
, DN: cn=sample-name1,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-27T20:50:15.478084
cn: sample-name1
objectClass: inetOrgPerson
sn: sample
]
Looking at the result, you can see that all the cn under ou = sample-unit, dc = sample-component, dc = sample-ldap
specified in Reader have been acquired. Furthermore, the respective attribute values sn and st have been acquired.
sample-You should be able to get the name.
#### **` main.py`**
```py
#Specify search conditions
data = data_reader.search('st')
print(data)
result
DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-27T20:50:15.470086
cn: sample-name
objectClass: inetOrgPerson
sn: sample1
sample2
st: test2
Looking at the result, I got the cn of sample-name as expected.
There is a function that can be obtained by converting the LDAP value to a json format string by using the reader ```entry_to_json () `` `.
main.py
#Can be obtained in json format
json_str = data[0].entry_to_json()
print(json_str)
print(type(json_str))
result
{
"attributes": {
"st": [
"test2"
]
},
"dn": "cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap"
}
<class 'str'>
There is a function that can convert the LDAP value to Dict format and get it by using the reader entry_attributes_as_dict
.
main.py
ldap_dict = data[0].entry_attributes_as_dict
print(ldap_dict)
print(type(ldap_dict))
result
{'st': ['test2']}
<class 'dict'>
By specifying cn, you can get information for only that one cn.
main.py
#If you use cn as a path, you can get only one
data_reader = Reader(conn, obj_cn_name, 'cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap')
data = data_reader.search()
print(data)
result
[DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-27T21:16:17.284094
cn: sample-name
objectClass: inetOrgPerson
sn: sample1
sample2
st: test2
]
You can write LDAP information to ldap3 using the Writer class, which has various functions. Writer can be generated using Reader.
Generate a Writer by giving a Reader that gets the LDAP value to the Writer's `from_cursor ()` `. Write the value by putting a value in the variable of the generated Writer and doing
commit ()
``.
main.py
from ldap3 import Server, Connection, ObjectDef, Reader, Writer
server = Server('localhost')
conn = Connection(server, 'cn=admin,dc=sample-ldap', password='LdapPass')
result = conn.bind()
#Get using reader
obj_cn_name = ObjectDef('inetOrgPerson', conn)
data_reader = Reader(conn, obj_cn_name, 'cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap')
data = data_reader.search()
#Display before updating
print(data[0])
#Let the writer read
data_writer = Writer.from_cursor(data_reader)
#Enter the value via writer
data_writer[0].sn = 'sample10'
data_writer[0].sn += 'sample20'
data_writer[0].st = 'test10'
#Reflection of change results
data_writer.commit()
#Display after update
data_reader2 = Reader(conn, obj_cn_name, 'cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap')
data2 = data_reader2.search()
print(data2[0])
result
DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-27T21:36:03.493031
cn: sample-name
objectClass: inetOrgPerson
sn: sample1
sample2
st: test1
DN: cn=sample-name,ou=sample-unit,dc=sample-component,dc=sample-ldap - STATUS: Read - READ TIME: 2020-03-27T21:36:03.686030
cn: sample-name
objectClass: inetOrgPerson
sn: sample10
sample20
st: test10
You can see that the values have changed as specified.
We have summarized the acquisition / modification / addition of LDAP using LDAP Reader and Writer. I feel that using Reader and Writer has made LDAP a little easier to handle. This has made it practical to use LDAP instead of RDB. This time, I wrote the source in a solid manner without considering the structure to make it as simple as possible, but I think that the source will become more convenient if I devise a little more.
Recommended Posts