[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Self-Test Script


The last file in the mailtools package, Example 13-26, lists the self-test code for the
package. This code is a separate script file, in order to allow for import search path
manipulation—it emulates a real client, which is assumed to have a mailconfig.py
module in its own source directory (this module can vary per client).


Example 13-26. PP4E\Internet\Email\mailtools\selftest.py


"""
###############################################################################
self-test when this file is run as a program
###############################################################################
"""



mailconfig normally comes from the client's source directory or


sys.path; for testing, get it from Email directory one level up



import sys
sys.path.append('..')
import mailconfig
print('config:', mailconfig.file)


get these from init


from mailtools import (MailFetcherConsole,
MailSender, MailSenderAuthConsole,
MailParser)


if not mailconfig.smtpuser:
sender = MailSender(tracesize=5000)
else:
sender = MailSenderAuthConsole(tracesize=5000)


sender.sendMessage(From = mailconfig.myaddress,
To = [mailconfig.myaddress],
Subj = 'testing mailtools package',
extrahdrs = [('X-Mailer', 'mailtools')],
bodytext = 'Here is my source code\n',
attaches = ['selftest.py'],
)


bodytextEncoding='utf-8', # other tests to try


attachesEncodings=['latin-1'], # inspect text headers


attaches=['monkeys.jpg']) # verify Base64 encoded


to='i18n adddr list...', # test mime/unicode headers


change mailconfig to test fetchlimit


fetcher = MailFetcherConsole()
def status(*args): print(args)


hdrs, sizes, loadedall = fetcher.downloadAllHeaders(status)
for num, hdr in enumerate(hdrs[:5]):
print(hdr)


The mailtools Utility Package | 983
Free download pdf