Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22001

Re: Best way to disconnect from ldap?

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 <jcd@sdf.lonestar.org>
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" <jcd@sdf.lonestar.org>
To python-list@python.org
In-Reply-To <jkda88$mrk$1@reader1.panix.com>
References <jkda88$mrk$1@reader1.panix.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.873.1332361326.3037.python-list@python.org> (permalink)
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

Show key headers only | View raw


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"
> 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Best way to disconnect from ldap? John Gordon <gordon@panix.com> - 2012-03-21 19:30 +0000
  Re: Best way to disconnect from ldap? "J. Cliff Dyer" <jcd@sdf.lonestar.org> - 2012-03-21 16:21 -0400
  Re: Best way to disconnect from ldap? Chris Rebert <clp2@rebertia.com> - 2012-03-21 13:34 -0700
  Re: Best way to disconnect from ldap? Chris Kaynor <ckaynor@zindagigames.com> - 2012-03-21 13:54 -0700
  Re: Best way to disconnect from ldap? Tycho Andersen <tycho@tycho.ws> - 2012-03-22 08:14 -0500
    Re: Best way to disconnect from ldap? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-22 17:26 +0000
      Re: Best way to disconnect from ldap? Tim Chase <python.list@tim.thechases.com> - 2012-03-22 12:54 -0500
      Re: Best way to disconnect from ldap? Terry Reedy <tjreedy@udel.edu> - 2012-03-22 15:27 -0400
      Re: Best way to disconnect from ldap? Tycho Andersen <tycho@tycho.ws> - 2012-03-22 16:02 -0500
  Re: Best way to disconnect from ldap? Chris Rebert <clp2@rebertia.com> - 2012-03-22 06:27 -0700
  Re: Best way to disconnect from ldap? Tycho Andersen <tycho@tycho.ws> - 2012-03-22 09:00 -0500
  Re: Best way to disconnect from ldap? John Gordon <gordon@panix.com> - 2012-04-05 18:38 +0000
    Re: Best way to disconnect from ldap? Michael Ströder <michael@stroeder.com> - 2012-04-05 20:56 +0200

csiph-web