如何使用 python-ldap 安装 LDAP 架构?

How to install a LDAP schema with python-ldap?

我在 .ldif 文件中有一个 LDAP 模式,我想使用 python-ldap 在我的 LDAP 服务器上安装该模式。我的上下文是需要一些自定义 LDAP 架构的 python 应用程序的安装过程。

python-ldap 中有一些关于 ldif and schemas 的文档,但我不知道从哪里开始。

如何使用 python-ldap 安装架构?

import ldap.modlist
import ldif


with open("myschema.ldif") as fd:
    parser = ldif.LDIFRecordList(fd)
    parser.parse()

for dn, entry in parser.all_records:
    add_modlist = ldap.modlist.addModlist(entry)
    conn.add_s(dn, add_modlist)  # where 'conn' is a LDAP connection

简短的回答是@azmeuk 之前写的。

长答案是:这取决于您的 LDIF 文件的格式。如果它包含修改语句,那么您无法使用 python-ldap.

轻松解析文件

请在 Process LDIF modify records with python-ldap 查看我的问题,看看我在说什么。