Foundations of Python Network Programming

(WallPaper) #1
Chapter 15 ■ IMap

279

else:
if reply in msgdict:
explore_message(c, reply)


c.close_folder()


def explore_message(c, uid):
"""Let the user view various parts of a given message."""


msgdict = c.fetch(uid, ['BODYSTRUCTURE', 'FLAGS'])


while True:
print()
print('Flags:', end=' ')
flaglist = msgdict[uid]['FLAGS']
if flaglist:
print(' '.join(flaglist))
else:
print('none')
print('Structure:')
display_structure(msgdict[uid]['BODYSTRUCTURE'])
print()
reply = input('Message %s - type a part name, or "q" to quit: '
% uid).strip()
print()
if reply.lower().startswith('q'):
break
key = 'BODY[%s]' % reply
try:
msgdict2 = c.fetch(uid, [key])
except c._imap.error:
print('Error - cannot fetch section %r' % reply)
else:
content = msgdict2[uid][key]
if content:
print(banner)
print(content.strip())
print(banner)
else:
print('(No such section)')


def display_structure(structure, parentparts=[]):
"""Attractively display a given message structure."""


The whole body of the message is named 'TEXT'.

Free download pdf