ldap3 create
时间: 2023-07-19 20:15:13 浏览: 62
ldap的搭建
To create an LDAP connection using the ldap3 library in Python, you can follow these steps:
1. Install the ldap3 library using pip: `pip install ldap3`
2. Import the library: `import ldap3`
3. Set up the connection parameters, including the server address and port, the authentication method, and the credentials. For example:
```
server = ldap3.Server('ldap://my-ldap-server.com', port=389)
user_dn = 'cn=admin,dc=my-domain,dc=com'
password = 'my-password'
authentication = ldap3.SIMPLE, user_dn, password
```
4. Bind to the LDAP server using the connection parameters:
```
conn = ldap3.Connection(server, user=user_dn, password=password, authentication=authentication)
conn.bind()
```
If the bind is successful, you will get a response of `True`.
5. Once you have bound to the server, you can perform LDAP operations such as searching for entries, adding or modifying entries, and deleting entries.
For example, to search for all entries under the `ou=users,dc=my-domain,dc=com` node:
```
search_base = 'ou=users,dc=my-domain,dc=com'
search_filter = '(objectClass=*)'
conn.search(search_base, search_filter)
entries = conn.entries
```
The `entries` variable will contain a list of all the entries that match the search filter.
6. Finally, don't forget to unbind from the server when you are done using the connection:
```
conn.unbind()
```
阅读全文