对于client链接ldap server的策略,ldap3提供了4种选择,能够经过client_strategy设置Connection object应用哪一种策略:数据库
l SYNCjson
l ASYNC安全
l RESTARTABLEdom
l REUSABLE异步
同步策略(SYNC, RESTARTABLE),全部的ldap操做返回True/Falsesocket
异步策略(ASYNC, REUSABLE)返回一个msgid(一个数值),异步策略发送请求后不用等待响应,须要响应的时候直接使用get_response(message_id)获取结果。等待响应的超时时间能够经过get_response的timeout参数指定,默认10s。若是使用get_request=True in the get_response(),同时会返回发送的请求字典。ide
创建链接:ui
创建Server对象时使用get_info=ldap3.ALL参数,创建Connection链接以后能够获取到server信息(匿名获取),从中能够获取到域名信息,域控计算机名,ldap server支持的Extension和Controlspa
创建Server时指定 active=True,创建链接前会先检查ldap server的可用性;active=5指定抛出 LDAPServerPoolExhaustedError异常以前重试的次数orm
exhaust=True : 若是ldap server不时active,server将会从pool中移除。exhaust=10:设置为数值,表示认为server 10s不可达,则认为它为offline,
When all servers in a pool are not available the strategy will wait for the number of seconds specified in ldap.
POOLING_LOOP_TIMEOUT before starting a new cycle. This defaults to 10 seconds.
The pool can have different HA strategies:
• FIRST: gets the first server in the pool, if ‘active’ is set to True gets the first available server
• ROUND_ROBIN: each time the connection is open the subsequent server in the pool is used. If active is set to
True unavailable servers will be discarded
• RANDOM: each time the connection is open a random server is chosen in the pool. If active is set to True
unavailable servers will be discarded
A server pool can be defined in different ways:
server1 = Server('server1')
server2 = Server('server2')
server3 = Server('server1', port=636, use_ssl=True)
• explicitly with Server objects in the init:
server_pool = ServerPool([server1, server2, server3], POOLING_STRATEGY_ROUND_
˓→ ROBIN, active=True, exhaust=True)
• explicitly with an add operation in the pool object:
server_pool = ServerPool(None, POOLING_STRATEGY_ROUND_ROBIN_ACTIVE)
server_pool.add(server1)
server_pool.add(server2)
server_pool.add(server3)
44 Chapter 1. Contents
ldap3 Documentation, Release 2.5
• implicitly directly in the Connection object init (passing a list of servers):
conn = Connection([server1, server2, server3]) # the ServerPool object is
˓→ defined with the default pooling strategy
Pools can be dynamically changed. You can add and remove Server objects from pools even if they are already used
in Connection:
server4 = Server('server2', port=636, use_ssl=True)
server_pool.remove(server2)
server_pool.add(server4)
Connections are notified of the change and can reopen the socket to the new server at next open() operation.
You can also save the schema and info in a json string:
json_info = server.info.to_json()
json_schema = server.schema.to_json()
or can have them saved on file:
server.info.to_file('server-info.json')
server.schema.to_file('server-schema.json')
to build a new server object with the saved json files you can retrieve them with:
from ldap3 import DsaInfo, SchemaInfo
dsa_info = DsaInfo.from_file('server-info.json')
schema_info = SchemaInfo.from_file('server-schema.json')
server = Server('hostname', dsa_info, schema_info)
ldap server的Schema数据库中存储了ldap server中的对象的已知类型信息,能够经过server.schema获取到(微软AD须要鉴权,匿名用户没法获取),里面存储了ldap server理解那些数据类型,同时也指定,哪些属性被ldap server中的对象支持
使用鉴权用户链接ldap server后能够查看server.shema等高级别操做。查看当前鉴权用户信息。如下链接使用的不安全的链接,密码信息明文传输,能够被抓取。使用authentication=ldap3.NTLM的鉴权方式没法显示的看到鉴权信息。
可使用如下方式创建安全链接,2种方式都是创建TLS链接:
l LDAP over TLS
l the StartTLS extended operation ##微软AD不支持
ldap查询
ldap查询基于search_base和search_filter,filter是个表达式:
l 查询全部显示名叫John而且email以‘@example.org’结尾的用户:(&(givenName=John)(mail=*@example.org))
l 查询显示名为Jhon或者Fred而且邮箱以@example.org结尾的用户
(&
(|
(GivenName=Jhon)
(givenName=Fred)
)
( mail=*@example.org)
)
搜索search_base下的全部用户,默认search_scope='SUBTREE',没有指定请求任何attribute,只返回entries的distinguished Name,请求成功(同步strategy)返回True,conn.entries获取查询到的结果:
conn.search(base_search,'(objectclass=person)')
conn.entries
可使用访问字典或者访问对象属性的方式访问从server上获取到的attribute值,有些属性不区分大小写,raw_values获取到的是从server返回的原始的值:
返回的entry能够格式化为json字符串
若是查询的属性的值为空,返回的entries中将不包含此属性,除非在Connection中指定return_empty_attributes=False,微软AD中貌似不起做用。
对ldap server进行search操做以后,Connection有如下属性能够访问:
在AD上增长entry,第一个参数为增长的对象dn,第二个参数为object_class,指定建立的object的类型,第三个参数为object提供的个性化attribute:
域控支持的objectclass能够经过server.schema获取到,建立不一样类型的objectclass支持哪些attribute能够经过server.schema.object_classes['user']方式获取到,大多数attribute在建立object的时候都是可选的,必选参数会单独列出:
重命名一个dn,利用modify_dn提供的参数new_superior=new_dn,还能够将dn从一个ou移动到另外一个ou:
检查object的属性是否和给定值同样。