email module, redirecting to stdout

Peter Otten __peter__ at web.de
Tue Oct 4 10:18:41 EDT 2005


Laszlo Zsolt Nagy wrote:

> I have this code:
> 
>         s = smtplib.SMTP()
>         s.set_debuglevel(1)
>         s.connect(host=smtp_host)
>         s.set_debuglevel(0)
>         log("Connected, sending e-mail")
>         sys.stdout.flush()
>         s.sendmail(
>             consts.EMAIL_FROMADDRESS,
>             [to],
>             msg.as_string()
>         )
>         log("E-mail sent OK")
>         s.quit()
> 
> The problem is that whenever I set the debuglevel to 1, messages will go
> to stderr. I would like them to go to stdout. Using
> 
> sys.stderr = sys.stdout
> 
> has no effect. Redirecting stderr to stdout from the shell is not an
> option for me, because I need to use stderr for other messages.

smtplib obtains a copy of stderr by

from sys import stderr

Therefore you have to do

smtplib.stderr = sys.stdout

to get the desired effect.

Peter







More information about the Python-list mailing list