smtplib, capturing output from set_debuglevel ??

Steve Holden steve at holdenweb.com
Thu Oct 7 22:13:16 EDT 2004


Tim Williams wrote:

> ----- Original Message ----- 
> From: "Steve Holden" <steve at holdenweb.com>
> 
> 
>>Tim Williams wrote:
>>
>>
>>>Can anyone suggest a way of capturing the output from smtplib's
>>>set_debuglevel
> 
> 
>>All smtplib debug output is produced using print statements to standard
>>ouput, so you could try creating a "file-like" object (such as a
>>cStringIO) and temporarily replacing sys.stdout with it (saving
>>sys.sdout and replacing it if other parts of your program need to use it).
>>
>>If you need help to work out the details, post again.
> 
> 
> Many thanks Steve
> 
> I have implemented your suggestion ,   but what is the best way to get the
> data out of the file-like object,   the only option that seems to work for
> me is
> 
> f.getvalue()
> 
> which returns a string,  but getvalue() doesn't "feel" right somehow.   Am I
> missing something ?
> 
If you want to use file primitives to read the value back, just create 
another one:

inpt = StringIO.StringIO(firstStringIO.getvalue())

then you can read the printed output from inpt just like a file.

regards
  Steve
-- 
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119



More information about the Python-list mailing list