email module, redirecting to stdout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laszlo Zsolt Nagy

    email module, redirecting to stdout


    Hello,

    I have this code:

    s = smtplib.SMTP()
    s.set_debugleve l(1)
    s.connect(host= smtp_host)
    s.set_debugleve l(0)
    log("Connected, sending e-mail")
    sys.stdout.flus h()
    s.sendmail(
    consts.EMAIL_FR OMADDRESS,
    [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

  • Peter Otten

    #2
    Re: email module, redirecting to stdout

    Laszlo Zsolt Nagy wrote:
    [color=blue]
    > I have this code:
    >
    > s = smtplib.SMTP()
    > s.set_debugleve l(1)
    > s.connect(host= smtp_host)
    > s.set_debugleve l(0)
    > log("Connected, sending e-mail")
    > sys.stdout.flus h()
    > s.sendmail(
    > consts.EMAIL_FR OMADDRESS,
    > [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.[/color]

    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




    Comment

    • Just

      #3
      Re: email module, redirecting to stdout

      In article <dhu2vh$ivo$00$ 1@news.t-online.com>,
      Peter Otten <__peter__@web. de> wrote:
      [color=blue]
      > Laszlo Zsolt Nagy wrote:
      >[color=green]
      > > I have this code:
      > >
      > > s = smtplib.SMTP()
      > > s.set_debugleve l(1)
      > > s.connect(host= smtp_host)
      > > s.set_debugleve l(0)
      > > log("Connected, sending e-mail")
      > > sys.stdout.flus h()
      > > s.sendmail(
      > > consts.EMAIL_FR OMADDRESS,
      > > [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.[/color]
      >
      > 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.[/color]

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

      Just

      Comment

      • Peter Otten

        #4
        Re: email module, redirecting to stdout

        Just wrote:
        [color=blue][color=green]
        >> smtplib obtains a copy of stderr by
        >> from sys import stderr[/color][/color]
        [color=blue]
        > Ouch. I'd consider this a bug. "from sys import stderr" should at least
        > be considered bad style.[/color]

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

        debug = logging.getLogg er("smtplib").d ebug

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

        Peter

        Comment

        Working...