Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!ecngs!feeder.ecngs.de!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'context': 0.04; 'method,': 0.05; 'ldap': 0.09; 'method:': 0.09; 'password)': 0.09; 'solution,': 0.09; 'exception': 0.12; 'def': 0.13; 'thanks!': 0.14; 'edward': 0.15; '19:30': 0.16; '__del__': 0.16; 'cliff': 0.16; 'close()': 0.16; 'subject:ldap': 0.16; 'x-mailer:evolution 2.32.3': 0.16; 'this:': 0.16; 'wed,': 0.17; 'wrote:': 0.18; 'wrap': 0.18; 'seems': 0.20; 'cheers,': 0.20; "haven't": 0.20; 'wrote': 0.21; 'header:In-Reply-To:1': 0.22; 'code': 0.26; '"the': 0.26; 'import': 0.27; 'bit': 0.28; "i'm": 0.28; 'explicit': 0.29; 'class': 0.29; 'closing': 0.30; 'config': 0.30; 'subject:?': 0.31; 'regardless': 0.31; 'there': 0.33; 'done.': 0.34; 'done': 0.34; 'statement,': 0.34; 'to:addr:python-list': 0.35; 'connection': 0.36; 'question': 0.36; 'received:org': 0.36; 'before.': 0.37; 'but': 0.37; 'similar': 0.37; 'skip:_ 10': 0.38; 'received:192': 0.38; 'could': 0.38; 'subject:from': 0.39; 'application': 0.40; 'called': 0.40; 'being': 0.40; 'user': 0.40; 'to:addr:python.org': 0.40; 'john': 0.61; 'your': 0.61; 'manager.': 0.64; 'ever': 0.64; 'connection,': 0.73; 'connection.': 0.77; 'about?': 0.84; 'bears': 0.84; 'subject:Best': 0.93 Subject: Re: Best way to disconnect from ldap? From: "J. Cliff Dyer" To: python-list@python.org In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Date: Wed, 21 Mar 2012 16:21:52 -0400 Mime-Version: 1.0 X-Mailer: Evolution 2.32.3 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332361326 news.xs4all.nl 6867 [2001:888:2000:d::a6]:60300 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22001 Write a context manager. Then you just do with MyLDAPWrapper() as ldap ldap.this() ldap.that() and when you leave the scope of the with statement, your ldap __exit__ method will get called regardless of how you left. Cheers, Cliff On Wed, 2012-03-21 at 19:30 +0000, John Gordon wrote: > 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:LDAP > > 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() > > This seems like a much cleaner solution, as I don't ever have to worry > about closing the connection; it gets done automatically. > > I haven't ever used __del__ before. Are there any 'gotchas' I need to > worry about? > > Thanks! > > -- > 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" >