Foundations of Python Network Programming

(WallPaper) #1
Chapter 14 ■ pOp

265

hostname, username = sys.argv[1:]
passwd = getpass.getpass()


p = poplib.POP3SSL(hostname)
try:
p.user(username)
p.pass
(passwd)
except poplib.error_proto as e:
print("Login failed:", e)
else:
visit_all_listings(p)
finally:
p.quit()


def visit_all_listings(p):
response, listings, octets = p.list()
for listing in listings:
visit_listing(p, listing)


def visit_listing(p, listing):
number, size = listing.decode('ascii').split()
print('Message', number, '(size is', size, 'bytes):')
print()
response, lines, octets = p.top(number, 0)
document = '\n'.join( line.decode('ascii') for line in lines )
message = email.message_from_string(document)
for header in 'From', 'To', 'Subject', 'Date':
if header in message:
print(header + ':', message[header])
print()
print('Read this message [ny]?')
answer = input()
if answer.lower().startswith('y'):
response, lines, octets = p.retr(number)
document = '\n'.join( line.decode('ascii') for line in lines )
message = email.message_from_string(document)
print('-' 72)
for part in message.walk():
if part.get_content_type() == 'text/plain':
print(part.get_payload())
print('-'
72)
print()
print('Delete this message [ny]?')
answer = input()
if answer.lower().startswith('y'):
p.dele(number)
print('Deleted.')


if name == 'main':
main()

Free download pdf