email module, redirecting to stdout

  • Thread starter Laszlo Zsolt Nagy
  • Start date
L

Laszlo Zsolt Nagy

Hello,

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.

Thanks,

Les
 
P

Peter Otten

Laszlo said:
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
 
J

Just

Peter Otten said:
Laszlo said:
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.

Ouch. I'd consider this a bug. "from sys import stderr" should at least
be considered bad style.

Just
 
P

Peter Otten

Ouch. I'd consider this a bug. "from sys import stderr" should at least
be considered bad style.

I'd rather say this is the low-tech equivalent to

debug = logging.getLogger("smtplib").debug

an approach which is currently only taken by the cookielib module.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top