(solved) smtplib, capturing output from set_debuglevel ??

Tim Williams listserver at tdw.net
Fri Oct 8 14:32:53 EDT 2004


----- Original Message ----- 
From: "Tim Williams" <listserver at tdw.net>

> ----- Original Message ----- 
> From: "Steve Holden" <steve at holdenweb.com>
[snip]
> Yeah,  I did wonder about changing the code  (though it's not my first
> choice. I've done it before and caught a crab later),   or could I do it
> with Subclassing?
>
>  I'm still close to Newbie status, so I can't immediately imagine what  I
> could change the smptblib code to write to,    or  how to subclass to get
> the behaviour I might need.

Doh,  still thinking of myself as close to newbie  scares me off some things
!!

I decided to bite the bullet and have a look at the code for smtplib,
(after being scared of changing it before),   and of course its what I
should have done in the first place.

The debug output in smptlib is generated in the SMTP class with lines
similar to:

if self.debuglevel > 0: print "connect:", msg

so,  I added a new variable within the class

debug_out = []

and 2 new functions within the class

    def do_debug(self,msgs):
        if self.debuglevel > 0: print msgs
        self.debug_out.append(msgs)

    def debug(self):
        return self.debug_out

and changed

if self.debuglevel > 0: print "connect:", msg

to

self.do_debug( ('connect:', msg ) )

Took me about 4 minutes !!

now I can call s.debug()  and get a list containing the debug information.
s.debug() can be called at anytime including after s.quit()

As a side benefit,  I can use s.do_debug( msg ) at any time to get the
program to add (comment) lines to the debug information.  This will be
useful when reading the full retrieved debug information at a later date.

set_debuglevel() works exactly as it did before.

I have to tidy up the formatting/handling of the messages,   and I think I'm
going to add an option to retrieve  the debug information without the
message data (as its usually superfluous to the debugging process) , but
other than that it does just what I need already.

Is it worth asking for something like this to be added permanently to
smtplib,  I can't be the only one that needs to log outgoing smtp
conversations,  or debug to a file/log ?

Thanks for everyone's help

Tim















More information about the Python-list mailing list