Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

error handling - Capture debug output from Python smtplib

How do I capture the debug output from the Python smtplib library?

Here is my test program:

import smtplib
s = smtplib.SMTP("mx10.comcast.com")
s.set_debuglevel(1)
s.sendmail("no-such-sender@comcast.com",["no-such-receiver@comcast.com"],"""
from: no-such-sender@comcast.com
to: no-such-receiver@comcast.com
subject: no such message

This message won't be delivered to anybody.
""")

Here is the output:

send: 'ehlo dance.local
'
reply: '250-mx10.comcast.com says EHLO to 129.6.220.67:57015
'
reply: '250-SIZE 40000000
'
reply: '250-PIPELINING
'
reply: '250-ENHANCEDSTATUSCODES
'
reply: '250-8BITMIME
'
reply: '250 XXXXXXXA
'
reply: retcode (250); Msg: mx10.comcast.com says EHLO to 129.6.220.67:57015
SIZE 40000000
PIPELINING
ENHANCEDSTATUSCODES
8BITMIME
XXXXXXXA
send: 'mail FROM:<no-such-sender@comcast.com> size=137
'
reply: '250 2.0.0 MAIL FROM accepted
'
reply: retcode (250); Msg: 2.0.0 MAIL FROM accepted
send: 'rcpt TO:<no-such-receiver@comcast.com>
'
reply: '550 5.1.1 Recipient address rejected: {Gateway}
'
reply: retcode (550); Msg: 5.1.1 Recipient address rejected: {Gateway}
send: 'rset
'
reply: '250 2.0.0 RSET OK
'
reply: retcode (250); Msg: 2.0.0 RSET OK
Traceback (most recent call last):
  File "/Users/simsong/x.py", line 11, in <module>
    """)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 742, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'no-such-receiver@comcast.com': (550, '5.1.1 Recipient address rejected: {Gateway}')}

I want the output in a variable, output. Specifically, I want all of the lines that begin with send: and reply:.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It can be done by redirecting stderr to a file:

import tempfile, smtplib, os, sys

# Find an available file descriptor                                                                                              
t = tempfile.TemporaryFile()
available_fd = t.fileno()
t.close()

# now make a copy of stderr                                                                                                      
os.dup2(2,available_fd)

# Now create a new tempfile and make Python's stderr go to that file                                                             
t = tempfile.TemporaryFile()
os.dup2(t.fileno(),2)

# Now run the task that logs to stderr                                                                                           
s = smtplib.SMTP("mx10.comcast.com")
s.set_debuglevel(1)
s.sendmail("no-such-sender@comcast.com",["no-such-receiver@comcast.com"],"""                                                     
from: no-such-sender@comcast.com                                                                                                 
to: no-such-receiver@comcast.com                                                                                                 
subject: no such message                                                                                                         

This message won't be delivered to anybody.                                                                                      
""")

# Grab the stderr from the temp file                                                                                             
sys.stderr.flush()
t.flush()
t.seek(0)
stderr_output = t.read()
t.close()

# Put back stderr                                                                                                                
os.dup2(available_fd,2)
os.close(available_fd)


# Finally, demonstrate that we have the output:                                                                                  
print("STDERR:")
count = 0
for line in stderr_output.decode('utf-8').split("
"):
    count += 1
    print("{:3} {}".format(count,line))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...