本文介绍了LDAP / LDIF解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 全部, 我希望有人能够帮我解决问题。我在Linux机器上运行了一个 LDAP服务器,这个LDAP服务器在各种分组中包含一个 电话列表,其ldif文件是 - dn:dc = example,dc = com objectClass:top objectClass:dcObject objectClass:organization dc:示例 o:示例组织 dn:ou = groupa,dc = example,dc = com ou:groupa objectClass:top objectClass:organizationalUnit 描述:A组 dn:cn = johnsmith,ou = groupa,dc = example,dc = com cn:johnsmith objectClass:top objectClass:person sn:Smith telephoneNumber:112 dn:cn = davesteel,ou = groupa,dc = example ,dc = com cn:davesteel objectClass:top objectClass:person sn:Steel 电话号码:113 dn:ou = groupb,dc = example,dc = com ou:gro upb objectClass:top objectClass:organizationalUnit 描述:B组 dn:cn = williamdavis,ou = groupb,dc = example,dc = com cn:williamdavis objectClass:top objectClass:person sn:戴维斯 电话号码:122 dn:cn = jamesjarvis,ou = groupb,dc = example,dc = com cn:jamesjarvis objectClass:top objectClass:person sn:Jarvis telephoneNumber:123 我正在创建一个python客户端程序,它将在与LDAP服务器相同的目录结构中显示电话 列表(即它是 从所有组的按钮开始,当你点击一个组时它会显示所有可用的数字或组的按钮,然后你就可以了。 可以不断向下钻取。) 我想知道最好的方法吗?我已经安装并使用了 python-ldap库,这些允许我访问和搜索 服务器,但搜索总是返回一个可怕的列表嵌套, 元组和字典,下面是一个返回一个 记录的例子 - (''dc = example,dc = com '',{''objectClass'':[''top'',''dcObject'', ''组织''',''dc'':[''示例''' ],''o'':[''示例组织'']}) 基本上我认为我需要解析搜索结果来创建对象 并在此周围构建python按钮,但我希望有人 能够指出我如何做到这一点的正确方向? 是否有解析器可用? (有一个ldif库可用,但是 不明显它是如何工作的,我看不到太多的文档,并且 它似乎被弃用了......)。非常感谢。 Ian 解决方案 > 我想知道最好的方法吗?我已经安装并使用了 python-ldap库,这些允许我访问和搜索 服务器,但搜索总是返回一个可怕的列表嵌套, 元组和字典,下面是一个返回一个 记录的例子 - (''dc = example,dc = com '',{''objectClass'':[''top'',''dcObject'', ''组织''',''dc'':[''示例''' ],''o'':[''示例组织'']}) 但这正是您的LDAP记录所包含的内容。还有什么呢?是什么?不,你不需要解析器,因为上面的_是解析结果。 没有解析器可以给你任何东西。 你当然可以创建包装器对象,您可以根据''objectClass''中的 值进行实例化,并且可以方便地访问某些 属性。然而,这完全取决于你,因为没有其他人可以在_your_应用程序中看待事物的外观和工作方式。 Diez Cruelemortaécrit: 全部, 我是希望有人能够帮我解决问题。我在Linux机器上运行了一个 LDAP服务器,这个LDAP服务器在各种分组中包含一个 电话列表,其ldif文件是 - (剪辑) > 我正在创建一个将显示的python客户端程序电话 列表与LDAP服务器上的目录结构相同(即它是从所有组的按钮开始,当你点击一个组时它是 出现所有可用数字或组的按钮,你可以继续向下钻取。 我想知道最好的办法吗?我已经安装并使用了 python-ldap库,这些允许我访问和搜索 服务器,但搜索总是返回一个可怕的列表嵌套, 元组和字典,下面是一个返回一个 记录的例子 - (''dc = example,dc = com '',{''objectClass'':[''top'',''dcObject'', ''组织''',''dc'':[''示例''' ],''o'':[''示例组织'']}) 你的问题是什么?这正是你的ldap记录应该看起来像 之类的。一个(base_dn,记录)元组,其中记录是一个字典 attribute_name:[values,...] 基本上我我认为我需要解析搜索结果来创建对象 Q& D包装器: class LdapObject(object): def __init __(self,ldapentry): self.dn,self._record = ldapentry def __getattr __(self,名称): 试试: data = self._record [name] 除了KeyError之外的: 引发AttributeError ( "对象%s没有属性%s"%(自我,姓名) ) else: #默认情况下所有LDAP属性都是多值的, #即使模式显示它们是单值的 如果len(数据)== 1: 返回数据[0] 否则: 返回数据[:] def isa(self,objectClass ): 返回self.objectClass中的objectClass: root = LdapObje ct( (''dc = example,dc = com'', {''objectClass'':[''top'',''dcObject'' ,''组织''], ''dc'':[''示例''], ''o'':[''示例组织' '}} )) root.o =''示例组织'' root.objectClass = [''top'',''dcObject'',''organization''] root.isa(''organization'') =正确 FWIW,我曾经开始编写更高级别的LDAP api(某种类型的 Object-LDAP Mapper ...)使用描述符进行ldap属性访问,但 我从来没有完成该死的事情,而且它处于一个非常抱歉的状态。我会 有一天必须回到它... 并在此周围构建python按钮,但我是希望有人 能指出我如何做到这一点的正确方向吗? 有解析器吗? cf cf。 树层次结构由DN定义每个对象的类型,对象是由objectClass指定的类型。 只需收集所有项目(或通过调整范围动态完成) 搜索请求的基础) On1fév,18:22,Cruelemort < ian.ing ... @ gmail.comwrote: 全部, 我希望有人能够帮我解决问题。我在Linux机器上运行了一个 LDAP服务器,这个LDAP服务器在各种分组中包含一个 电话列表,其ldif文件是 - dn:dc = example,dc = com objectClass:top objectClass:dcObject objectClass:organization dc:示例 o:示例组织 dn:ou = groupa,dc = example,dc = com ou:groupa objectClass:top objectClass:organizationalUnit 描述:A组 dn:cn = johnsmith,ou = groupa,dc = example,dc = com cn:johnsmith objectClass:top objectClass:person sn:Smith telephoneNumber:112 dn:cn = davesteel,ou = groupa,dc = example ,dc = com cn:davesteel objectClass:top objectClass:person sn:Steel 电话号码:113 dn:ou = groupb,dc = example,dc = com ou:groupb objectClass:top objectClass:organizationalUnit 描述:B组 dn:cn = williamdavis ,ou = groupb,dc = example,dc = com cn:williamdavis objectClass:top objectClass:person sn:戴维斯 电话号码:122 dn:cn = jamesjarvis,ou = groupb,dc = example,dc = com cn:jamesjarvis objectClass:top objectClass:person sn:Jarvis telephoneNumber: 123 我正在创建一个python客户端程序,它将在与LDAP服务器相同的目录结构中显示电话 列表(即它是 从所有组的按钮开始,当你点击一个组时它会显示所有可用的数字或组的按钮,然后你就可以了。 可以不断向下钻取。) 我想知道最好的方法吗?我已经安装并使用了 python-ldap库,这些允许我访问和搜索 服务器,但搜索总是返回一个可怕的列表嵌套, 元组和字典,下面是一个返回一个 记录的例子 - (''dc = example,dc = com '',{''objectClass'':[''top'',''dcObject'', ''组织''',''dc'':[''示例''' ],''o'':[''示例组织'']}) 基本上我认为我需要解析搜索结果来创建对象 并在此周围构建python按钮,但我希望有人 能够指出我如何做到这一点的正确方向? 是否有解析器可用? (有一个ldif库可用,但是 不明显它是如何工作的,我看不到太多的文档,并且 它似乎被弃用了......)。 非常感谢。 Ian All, I am hoping someone would be able to help me with a problem. I have anLDAP server running on a linux box, this LDAP server contains atelephone list in various groupings, the ldif file of which is - dn: dc=example,dc=comobjectClass: topobjectClass: dcObjectobjectClass: organizationdc: exampleo: Example Organisation dn: ou=groupa,dc=example,dc=comou: groupaobjectClass: topobjectClass: organizationalUnitdescription: Group A dn: cn=johnsmith,ou=groupa,dc=example,dc=comcn: johnsmithobjectClass: topobjectClass: personsn: SmithtelephoneNumber: 112 dn: cn=davesteel,ou=groupa,dc=example,dc=comcn: davesteelobjectClass: topobjectClass: personsn: SteeltelephoneNumber: 113 dn: ou=groupb,dc=example,dc=comou: groupbobjectClass: topobjectClass: organizationalUnitdescription: Group B dn: cn=williamdavis,ou=groupb,dc=example,dc=comcn: williamdavisobjectClass: topobjectClass: personsn: DavistelephoneNumber: 122 dn: cn=jamesjarvis,ou=groupb,dc=example,dc=comcn: jamesjarvisobjectClass: topobjectClass: personsn: JarvistelephoneNumber: 123 I am creating a python client program that will display the telephonelist in the same directory structure as is on the LDAP server (i.e. itstarts with buttons of all the groups, when you click on a group itcomes up with buttons of all the numbers or groups available, and youcan continually drill down). I was wondering the best way to do this? I have installed and used thepython-ldap libraries and these allow me to access and search theserver, but the searches always return a horrible nesting of lists,tuples and dictionaries, below is an example of returning just onerecord - (''dc=example,dc=com'', {''objectClass'': [''top'', ''dcObject'',''organization''], ''dc'': [''example''], ''o'': [''Example Organisation'']}) Basically i think i need to parse the search results to create objectsand build the python buttons around this, but i was hoping someonewould be able to point me in the correct direction of how to do this?Is there a parser available? (there is an ldif library available butit is not obvious how this works, i cannot see much documentation, andit seems to be deprecated...). Many thanks. Ian 解决方案 >I was wondering the best way to do this? I have installed and used thepython-ldap libraries and these allow me to access and search theserver, but the searches always return a horrible nesting of lists,tuples and dictionaries, below is an example of returning just onerecord -(''dc=example,dc=com'', {''objectClass'': [''top'', ''dcObject'',''organization''], ''dc'': [''example''], ''o'': [''Example Organisation'']}) But this is exactly what your LDAP-record contains. What else should therebe? And no, you don''t need a parser, as the above _is_ the parsed result.No parser can possibly give you anything else. You can of course create wrapper-objects, that you instantiate based on thevalues in ''objectClass'', and that allow convenient access to certainproperties. Yet this is entirely up to you, as there is no one else who canforsee how things should look and work like in _your_ application. DiezCruelemort a écrit :All,I am hoping someone would be able to help me with a problem. I have anLDAP server running on a linux box, this LDAP server contains atelephone list in various groupings, the ldif file of which is - (snip)>I am creating a python client program that will display the telephonelist in the same directory structure as is on the LDAP server (i.e. itstarts with buttons of all the groups, when you click on a group itcomes up with buttons of all the numbers or groups available, and youcan continually drill down).I was wondering the best way to do this? I have installed and used thepython-ldap libraries and these allow me to access and search theserver, but the searches always return a horrible nesting of lists,tuples and dictionaries, below is an example of returning just onerecord -(''dc=example,dc=com'', {''objectClass'': [''top'', ''dcObject'',''organization''], ''dc'': [''example''], ''o'': [''Example Organisation'']})What''s your problem ? That''s exactly what your ldap record should looklike. A (base_dn, record) tuple, where the record is a dict ofattribute_name:[values, ...] Basically i think i need to parse the search results to create objectsQ&D wrapper: class LdapObject(object):def __init__(self, ldapentry):self.dn, self._record = ldapentry def __getattr__(self, name):try:data = self._record[name]except KeyError:raise AttributeError("object %s has no attribute %s" % (self, name))else:# all LDAP attribs are multivalued by default,# even when the schema says they are monovaluedif len(data) == 1:return data[0]else:return data[:] def isa(self, objectClass):return objectClass in self.objectClass: root = LdapObject((''dc=example,dc=com'',{''objectClass'': [''top'', ''dcObject'',''organization''],''dc'': [''example''],''o'': [''Example Organisation'']})) root.o=''Example Organisation''root.objectClass=[''top'', ''dcObject'',''organization'']root.isa(''organization'')=True FWIW, I once started writing an higher-level LDAP api (kind of anObject-LDAP Mapper...) using descriptors for ldap attribute access, butI never finished the damned thing, and it''s in a very sorry state. I''llhave to get back to it one day... and build the python buttons around this, but i was hoping someonewould be able to point me in the correct direction of how to do this?Is there a parser available?cf above.The tree hierarchy is defined by the DN of each object, the types ofthe object is specified by its objectClass.Just collect all items (or do it dynamically by tunning the scope andthe base of your search request)On 1 fév, 18:22, "Cruelemort" <[email protected]:All,I am hoping someone would be able to help me with a problem. I have anLDAP server running on a linux box, this LDAP server contains atelephone list in various groupings, the ldif file of which is -dn: dc=example,dc=comobjectClass: topobjectClass: dcObjectobjectClass: organizationdc: exampleo: Example Organisationdn: ou=groupa,dc=example,dc=comou: groupaobjectClass: topobjectClass: organizationalUnitdescription: Group Adn: cn=johnsmith,ou=groupa,dc=example,dc=comcn: johnsmithobjectClass: topobjectClass: personsn: SmithtelephoneNumber: 112dn: cn=davesteel,ou=groupa,dc=example,dc=comcn: davesteelobjectClass: topobjectClass: personsn: SteeltelephoneNumber: 113dn: ou=groupb,dc=example,dc=comou: groupbobjectClass: topobjectClass: organizationalUnitdescription: Group Bdn: cn=williamdavis,ou=groupb,dc=example,dc=comcn: williamdavisobjectClass: topobjectClass: personsn: DavistelephoneNumber: 122dn: cn=jamesjarvis,ou=groupb,dc=example,dc=comcn: jamesjarvisobjectClass: topobjectClass: personsn: JarvistelephoneNumber: 123I am creating a python client program that will display the telephonelist in the same directory structure as is on the LDAP server (i.e. itstarts with buttons of all the groups, when you click on a group itcomes up with buttons of all the numbers or groups available, and youcan continually drill down).I was wondering the best way to do this? I have installed and used thepython-ldap libraries and these allow me to access and search theserver, but the searches always return a horrible nesting of lists,tuples and dictionaries, below is an example of returning just onerecord -(''dc=example,dc=com'', {''objectClass'': [''top'', ''dcObject'',''organization''], ''dc'': [''example''], ''o'': [''Example Organisation'']})Basically i think i need to parse the search results to create objectsand build the python buttons around this, but i was hoping someonewould be able to point me in the correct direction of how to do this?Is there a parser available? (there is an ldif library available butit is not obvious how this works, i cannot see much documentation, andit seems to be deprecated...).Many thanks.Ian 这篇关于LDAP / LDIF解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 17:38