Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit4.readnews.com!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: Best way to disconnect from ldap? Date: Thu, 5 Apr 2012 18:38:35 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 51 Message-ID: References: NNTP-Posting-Host: panix3.panix.com X-Trace: reader1.panix.com 1333651115 21058 166.84.1.3 (5 Apr 2012 18:38:35 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Thu, 5 Apr 2012 18:38:35 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:22709 In John Gordon writes: > I'm writing an application that interacts with ldap, and I'm looking > for advice on how to handle the connection. Specifically, how to > close the ldap connection when the application is done. > I wrote a class to wrap an LDAP connection, similar to this: > import ldap > import ConfigParser > class MyLDAPWrapper(object): > def __init__(self): > config = ConfigParser.SafeConfigParser() > config.read('sample.conf') > > uri = config.get('LDAP', 'uri') > user = config.get('LDAP', 'user') > password = config.get('LDAP', 'password') > self.ldapClient = ldap.initialize(uri) > self.ldapClient.simple_bind_s(user, password) > My question is this: what is the best way to ensure the ldap connection > gets closed when it should? I could write an explicit close() method, > but that seems a bit messy; there would end up being lots of calls to > close() scattered around in my code (primarily inside exception handlers.) > Or I could write a __del__ method: > def __del__(self): > self.ldapClient.unbind_s() Thanks everyone for your input. I learned a lot! However, I just ran across this bit of documentation on python-ldap.org: class ldap.LDAPObject Instances of LDAPObject are returned by initialize() and open() (deprecated). The connection is automatically unbound and closed when the LDAP object is deleted. So, given that, do I need to do anything at all? -- John Gordon A is for Amy, who fell down the stairs gordon@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies"