Hi all,
I believe I am having a fundamental problem with my class and I can't
seem to figure out what I am doing wrong. Basically I want a class
which can do several specific ldap queries. So in my code I would have
multiple searches. But I can't figure out how to do it without it
barfing..
The error is straightforward ..
LDAP Version 2.0.8
Traceback (most recent call last):
File "./ldap-nsc.py", line 62, in ?
l.search()
File "./ldap-nsc.py", line 40, in search
ldap_result_id = l.search_s(base DN, searchScope, searchAttrs,
retrieveAttrs)
AttributeError: NSCLdap instance has no attribute 'search_s'
The code is also I believe straight forward..
import ldap
class NSCLdap:
def __init__(self,s erver="sc-ldap.nsc.com"):
who=""; cred=""
self.server=ser ver
try:
print "LDAP Version", ldap.__version_ _
l=ldap.open(ser ver)
l.simple_bind_s (who, cred)
l.protocol_vers ion=ldap.VERSIO N3
except ldap.LDAPError, error_message:
print "Couldn't Connect to %s %s " %
(server,error_m essage)
def search(self, baseDN="o=nsc.c om",
retrieveAttrs=N one,searchAttrs ="cn=*klass* " ):
searchScope = ldap.SCOPE_SUBT REE
try:
ldap_result_id = l.search_s(base DN, searchScope,
searchAttrs, retrieveAttrs)
result_set = []
while 1:
result_type, result_data = l.result(ldap_r esult_id, 0)
if (result_data == []):
break
else:
## here you don't have to append to a list
## you could do whatever you want with the
individual entry
## The appending to list is just for
illustration.
if result_type == ldap.RES_SEARCH _ENTRY:
result_set.appe nd(result_data)
print result_set
except ldap.LDAPError, error_message:
print "Errors on Search %s " % error_message
def setBaseDN(self, baseDN="o=nsc.c om"):
return baseDN
if __name__ == '__main__':
l = NSCLdap()
l.search()
I would love some pointers - clearly my code thinks that search_s is an
attribute of my class but it's not..
TIA 7 1356
rh0dium wrote: Hi all,
I believe I am having a fundamental problem with my class and I can't seem to figure out what I am doing wrong. Basically I want a class which can do several specific ldap queries. So in my code I would have multiple searches. But I can't figure out how to do it without it barfing..
The error is straightforward ..
LDAP Version 2.0.8 Traceback (most recent call last): File "./ldap-nsc.py", line 62, in ? l.search() File "./ldap-nsc.py", line 40, in search ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs) AttributeError: NSCLdap instance has no attribute 'search_s'
The code is also I believe straight forward..
import ldap
class NSCLdap:
def __init__(self,s erver="sc-ldap.nsc.com"): who=""; cred="" self.server=ser ver try: print "LDAP Version", ldap.__version_ _ l=ldap.open(ser ver) l.simple_bind_s (who, cred) l.protocol_vers ion=ldap.VERSIO N3 except ldap.LDAPError, error_message: print "Couldn't Connect to %s %s " % (server,error_m essage)
def search(self, baseDN="o=nsc.c om", retrieveAttrs=N one,searchAttrs ="cn=*klass* " ): searchScope = ldap.SCOPE_SUBT REE try:
If you had bothered to do some elementary debugging, like "print
repr(l)" here, just before the exception-triggering statement, ....
ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs) result_set = [] while 1: result_type, result_data = l.result(ldap_r esult_id, 0) if (result_data == []): break else: ## here you don't have to append to a list ## you could do whatever you want with the individual entry ## The appending to list is just for illustration. if result_type == ldap.RES_SEARCH _ENTRY: result_set.appe nd(result_data) print result_set except ldap.LDAPError, error_message: print "Errors on Search %s " % error_message
def setBaseDN(self, baseDN="o=nsc.c om"): return baseDN
if __name__ == '__main__':
l = NSCLdap() l.search()
I would love some pointers - clearly my code thinks that search_s is an attribute of my class but it's not..
You are confusing the bejaysus out of yourself and your audience by
using "l" as a name (1) at all (2) to represent two *different* things,
one in script-global scope -- l = NSCLdap() -- and one in the __init__
method of your class -- l=ldap.open(ser ver).
Use two different sensible names; then your real problem should become
apparent -- unless of course in the meantime some wally thinks it a good
idea to prevent your attaining a clue yourself by spoon-feeding you.
rh0dium wrote: Hi all,
I believe I am having a fundamental problem with my class and I can't seem to figure out what I am doing wrong. Basically I want a class which can do several specific ldap queries. So in my code I would have multiple searches. But I can't figure out how to do it without it barfing..
[snip] File "./ldap-nsc.py", line 40, in search ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs) AttributeError: NSCLdap instance has no attribute 'search_s'
The code is also I believe straight forward..
You're going to kick yourself when you see the mistake. import ldap
class NSCLdap:
def __init__(self,s erver="sc-ldap.nsc.com"): who=""; cred="" self.server=ser ver try: print "LDAP Version", ldap.__version_ _ l=ldap.open(ser ver)
^^^^^^^^^^^^^^^ ^^^
[big snip] if __name__ == '__main__':
l = NSCLdap() l.search()
I would love some pointers - clearly my code thinks that search_s is an attribute of my class but it's not..
Ah, but l -is- an instance of your class. You want l to refer to the
ldap connection, but you forgot do assign it to self.l -- in __init__,
you assign l to simply a local variable, which goes poof as soon as
__init__ returns. You forgot the self.l throughout both __init__ and
search.
You get the slighty misleading traceback because there is an "l" defined
-- it just happens to be the one in globals(), the l = NSCLdap() that
got assigned when you imported/ran the module.
Replace l = NSCLdap() with q = NSCLdap() (and l.search with q.search),
and you'll get a NameError instead.
rh0dium a écrit : Hi all,
I believe I am having a fundamental problem with my class and I can't seem to figure out what I am doing wrong. Basically I want a class which can do several specific ldap queries. So in my code I would have multiple searches. But I can't figure out how to do it without it barfing..
The error is straightforward ..
LDAP Version 2.0.8 Traceback (most recent call last): File "./ldap-nsc.py", line 62, in ? l.search() File "./ldap-nsc.py", line 40, in search ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs) AttributeError: NSCLdap instance has no attribute 'search_s'
The code is also I believe straight forward..
import ldap
class NSCLdap:
def __init__(self,s erver="sc-ldap.nsc.com"): who=""; cred="" self.server=ser ver try: print "LDAP Version", ldap.__version_ _ l=ldap.open(ser ver) l.simple_bind_s (who, cred) l.protocol_vers ion=ldap.VERSIO N3 except ldap.LDAPError, error_message: print "Couldn't Connect to %s %s " % (server,error_m essage)
And then you throw away the ldap connection...
def search(self, baseDN="o=nsc.c om", retrieveAttrs=N one,searchAttrs ="cn=*klass* " ): searchScope = ldap.SCOPE_SUBT REE try: ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs)
Now where is this 'l' coming from ?
result_set = [] while 1: result_type, result_data = l.result(ldap_r esult_id, 0) if (result_data == []): break else: ## here you don't have to append to a list ## you could do whatever you want with the individual entry ## The appending to list is just for illustration. if result_type == ldap.RES_SEARCH _ENTRY: result_set.appe nd(result_data) print result_set except ldap.LDAPError, error_message: print "Errors on Search %s " % error_message
def setBaseDN(self, baseDN="o=nsc.c om"): return baseDN
Err... this code is not 'setting' anything.
if __name__ == '__main__':
l = NSCLdap() l.search()
I would love some pointers - clearly my code thinks that search_s is an attribute of my class but it's not..
try with this instead :
q = NSCLdap()
q.search()
May I suggest a somewhat corrected version ?
class NSCLdap(object) :
def __init__(self,
server="sc-ldap.nsc.com",
baseDN="o=nsc.c om",
who=None,
cred=None):
self.server = server
self.baseDN = baseDN
if who is None:
self.who = ""
else:
self.who = who
if cred is None:
self.cred = ""
else:
self.cred = cred
self.connection = None
def connect(self):
try:
print "LDAP Version", ldap.__version_ _
self.connection = ldap.open(serve r)
self.connection .simple_bind_s( self.who, self.cred)
self.connection .protocol_versi on=ldap.VERSION 3
except ldap.LDAPError, error_message:
# I would not catch this. It's the caller's
# responsabilitie to handle this IMHO
print >> sys.stderr, "Couldn't Connect to %s %s " %
(server,error_m essage)
def search(self,
baseDN=None,
searchScope=lda p.SCOPE_SUBTREE ,
retrieveAttrs=N one,
searchAttrs="cn =*klass*" ):
cnx = self.connection
if baseDN is None:
baseDN = self.baseDN
try:
ldap_result_id = cnx.search_s(ba seDN,
searchScope,
searchAttrs,
retrieveAttrs)
result_set = []
while True:
result_type, result_data =cnx.result(lda p_result_id, 0)
#if (result_data == []):
if not result_data:
break
## here you don't have to append to a list
## you could do whatever you want with the
## individual entry
## The appending to list is just for
## illustration.
if result_type == ldap.RES_SEARCH _ENTRY:
result_set.appe nd(result_data)
print result_set
except ldap.LDAPError, error_message:
print >> sys.stderr, "Errors on Search %s " % error_message
if __name__ == '__main__':
truc = NSCLdap()
truc.search()
I knew it had to be something obvious - thanks so much!!
John Machin wrote: rh0dium wrote: Hi all,
I believe I am having a fundamental problem with my class and I can't seem to figure out what I am doing wrong. Basically I want a class which can do several specific ldap queries. So in my code I would have multiple searches. But I can't figure out how to do it without it barfing..
The error is straightforward ..
LDAP Version 2.0.8 Traceback (most recent call last): File "./ldap-nsc.py", line 62, in ? l.search() File "./ldap-nsc.py", line 40, in search ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs) AttributeError: NSCLdap instance has no attribute 'search_s'
The code is also I believe straight forward..
import ldap
class NSCLdap:
def __init__(self,s erver="sc-ldap.nsc.com"): who=""; cred="" self.server=ser ver try: print "LDAP Version", ldap.__version_ _ l=ldap.open(ser ver) l.simple_bind_s (who, cred) l.protocol_vers ion=ldap.VERSIO N3 except ldap.LDAPError, error_message: print "Couldn't Connect to %s %s " % (server,error_m essage)
def search(self, baseDN="o=nsc.c om", retrieveAttrs=N one,searchAttrs ="cn=*klass* " ): searchScope = ldap.SCOPE_SUBT REE try:
If you had bothered to do some elementary debugging, like "print repr(l)" here, just before the exception-triggering statement, ....
ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs) result_set = [] while 1: result_type, result_data = l.result(ldap_r esult_id, 0) if (result_data == []): break else: ## here you don't have to append to a list ## you could do whatever you want with the individual entry ## The appending to list is just for illustration. if result_type == ldap.RES_SEARCH _ENTRY: result_set.appe nd(result_data) print result_set except ldap.LDAPError, error_message: print "Errors on Search %s " % error_message
def setBaseDN(self, baseDN="o=nsc.c om"): return baseDN
if __name__ == '__main__':
l = NSCLdap() l.search()
I would love some pointers - clearly my code thinks that search_s is an attribute of my class but it's not..
You are confusing the bejaysus out of yourself and your audience by using "l" as a name (1) at all (2) to represent two *different* things, one in script-global scope -- l = NSCLdap() -- and one in the __init__ method of your class -- l=ldap.open(ser ver).
Use two different sensible names; then your real problem should become apparent -- unless of course in the meantime some wally thinks it a good idea to prevent your attaining a clue yourself by spoon-feeding you.
Thanks Bruno!!
Very much appreciated the modifications!!
Bruno Desthuilliers wrote: rh0dium a écrit : Hi all,
I believe I am having a fundamental problem with my class and I can't seem to figure out what I am doing wrong. Basically I want a class which can do several specific ldap queries. So in my code I would have multiple searches. But I can't figure out how to do it without it barfing..
The error is straightforward ..
LDAP Version 2.0.8 Traceback (most recent call last): File "./ldap-nsc.py", line 62, in ? l.search() File "./ldap-nsc.py", line 40, in search ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs) AttributeError: NSCLdap instance has no attribute 'search_s'
The code is also I believe straight forward..
import ldap
class NSCLdap:
def __init__(self,s erver="sc-ldap.nsc.com"): who=""; cred="" self.server=ser ver try: print "LDAP Version", ldap.__version_ _ l=ldap.open(ser ver) l.simple_bind_s (who, cred) l.protocol_vers ion=ldap.VERSIO N3 except ldap.LDAPError, error_message: print "Couldn't Connect to %s %s " % (server,error_m essage)
And then you throw away the ldap connection...
def search(self, baseDN="o=nsc.c om", retrieveAttrs=N one,searchAttrs ="cn=*klass* " ): searchScope = ldap.SCOPE_SUBT REE try: ldap_result_id = l.search_s(base DN, searchScope, searchAttrs, retrieveAttrs)
Now where is this 'l' coming from ?
result_set = [] while 1: result_type, result_data = l.result(ldap_r esult_id, 0) if (result_data == []): break else: ## here you don't have to append to a list ## you could do whatever you want with the individual entry ## The appending to list is just for illustration. if result_type == ldap.RES_SEARCH _ENTRY: result_set.appe nd(result_data) print result_set except ldap.LDAPError, error_message: print "Errors on Search %s " % error_message
def setBaseDN(self, baseDN="o=nsc.c om"): return baseDN
Err... this code is not 'setting' anything.
if __name__ == '__main__':
l = NSCLdap() l.search()
I would love some pointers - clearly my code thinks that search_s is an attribute of my class but it's not..
try with this instead : q = NSCLdap() q.search()
May I suggest a somewhat corrected version ?
class NSCLdap(object) : def __init__(self, server="sc-ldap.nsc.com", baseDN="o=nsc.c om", who=None, cred=None): self.server = server self.baseDN = baseDN if who is None: self.who = "" else: self.who = who if cred is None: self.cred = "" else: self.cred = cred self.connection = None
def connect(self): try: print "LDAP Version", ldap.__version_ _ self.connection = ldap.open(serve r) self.connection .simple_bind_s( self.who, self.cred) self.connection .protocol_versi on=ldap.VERSION 3
except ldap.LDAPError, error_message: # I would not catch this. It's the caller's # responsabilitie to handle this IMHO print >> sys.stderr, "Couldn't Connect to %s %s " % (server,error_m essage)
def search(self, baseDN=None, searchScope=lda p.SCOPE_SUBTREE , retrieveAttrs=N one, searchAttrs="cn =*klass*" ):
cnx = self.connection if baseDN is None: baseDN = self.baseDN
try: ldap_result_id = cnx.search_s(ba seDN, searchScope, searchAttrs, retrieveAttrs) result_set = [] while True: result_type, result_data =cnx.result(lda p_result_id, 0) #if (result_data == []): if not result_data: break ## here you don't have to append to a list ## you could do whatever you want with the ## individual entry ## The appending to list is just for ## illustration. if result_type == ldap.RES_SEARCH _ENTRY: result_set.appe nd(result_data) print result_set except ldap.LDAPError, error_message: print >> sys.stderr, "Errors on Search %s " % error_message
if __name__ == '__main__': truc = NSCLdap() truc.search()
Hi
I really like your approach but when do you actually get connected??
You never call the method connect? class NSCLdap(object) : def __init__(self, server="sc-ldap.nsc.com", baseDN="o=nsc.c om", who=None, cred=None): self.server = server self.baseDN = baseDN if who is None: self.who = "" else: self.who = who if cred is None: self.cred = "" else: self.cred = cred self.connection = None
def connect(self): try: print "LDAP Version", ldap.__version_ _ self.connection = ldap.open(serve r) self.connection .simple_bind_s( self.who, self.cred) self.connection .protocol_versi on=ldap.VERSION 3
except ldap.LDAPError, error_message: # I would not catch this. It's the caller's # responsabilitie to handle this IMHO print >> sys.stderr, "Couldn't Connect to %s %s " % (server,error_m essage)
def search(self, baseDN=None, searchScope=lda p.SCOPE_SUBTREE , retrieveAttrs=N one, searchAttrs="cn =*klass*" ):
cnx = self.connection if baseDN is None: baseDN = self.baseDN
try: ldap_result_id = cnx.search_s(ba seDN, searchScope, searchAttrs, retrieveAttrs) result_set = [] while True: result_type, result_data =cnx.result(lda p_result_id, 0) #if (result_data == []): if not result_data: break ## here you don't have to append to a list ## you could do whatever you want with the ## individual entry ## The appending to list is just for ## illustration. if result_type == ldap.RES_SEARCH _ENTRY: result_set.appe nd(result_data) print result_set except ldap.LDAPError, error_message: print >> sys.stderr, "Errors on Search %s " % error_message
if __name__ == '__main__': truc = NSCLdap() truc.search()
rh0dium a écrit : Hi
I really like your approach but when do you actually get connected?? You never call the method connect?
oops :(
(snip whole code) if __name__ == '__main__': truc = NSCLdap()
truc.connect() # was missing truc.search()
BTW, you'd better let exceptions propagate from connect(), and catch'em
in the calling code. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David MacQuigg |
last post by:
I have what looks like a bug trying to generate new style classes with
a factory function.
class Animal(object): pass
class Mammal(Animal): pass
def newAnimal(bases=(Animal,), dict={}):
class C(object): pass
C.__bases__ = bases
dict = 0
|
by: Axel Dahmen |
last post by:
I try to combine properties of several classes. This is done by assigning a
space separated list of class definitions to an element. However, IE shows a
kind of preference when choosing the right property which I think is
probably wrong. Here's what it does:
If two classes are defined in a stylesheet providing the same property, and
if these two classes are assigned to one single element, the preference
which class's property is used is...
|
by: Johno |
last post by:
I have written the two associated base classes below (Digital_camera and
Review) to manage digital camera and review objects. They are base classes
for which other derived classes can be written to provide more detail.
You'll notice that I've also declared the display functions as "virtual" to
allow for polymorphism. I now need to write a function to display all the
contents of an STL "deque" of pointers to Digital_camera objects. It needs...
|
by: Aleksei Guzev |
last post by:
Imagine one writing a class library CL1 for data storage.
He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains
a collection of the former.
And he derives class ‘IntItem’ from ‘DataItem’
public class DataItem
{
public DataItem() {}
|
by: Chris Szabo |
last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream
using the XmlSerializer. A stored procedure returns the following from SQL
Server:
<Student StudentId="1" Status="1" Gpa="3.50">
<Person Id="1" FirstName="FirstName0" LastName="LastName0"
MiddleInitial="W"/>
</Student>
In my code, person is the base class and student extends it. When I
| |
by: Manuel Bleichner |
last post by:
Hello list,
I have searched for some time now, but no result...
I'm having the following problem:
In a module I have a huge number of classes of the form:
class A(object):
connected_to =
<other attributes...>
|
by: outofmymind |
last post by:
Hi,
i've been practicing again......my exam is tomorrow :( and this is another practice question that my instructor told us to practice on, its not that easy, it was from a past final exam paper, so please help me out. I only did one little bit of it, and im 100% sure that its wrong......please bear with me :(
Here's how it goes:
public class BadStudentScores {
public BadStudentScores() {
scores = new int;
...
|
by: Chrissy |
last post by:
I took a C# class as an elective and received an incomplete in it and
am desparate for help. I have two assignments left (arrays and
inheritance) and would gladly pay anyone that can assist me with
this.
I want you to know that I'm a 41 year old mother of three boys (ages
9, 7, and 2) and have done my very best to muddle through this
course. Sadly, my old brain is not equipped to handle it. LOL
Chrissy
|
by: Allan Ebdrup |
last post by:
I would like to use reflection to find all classes that inherit from my
current class, even if they are in another assembly I want to find them if
the current project has a reference to that assembly.
The reason for this is that I want to check some attributes of all child
classes and have the base class react to this information in the child
classes.
Is this possible in an easy way, or do I have to traverse all loaded
assemblies and...
|
by: bharathreddy |
last post by:
Before going to that i want to say few thing on serialization :
Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream.
XML serialization serializes only the public fields and property values of an object...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |