Foundations of Python Network Programming

(WallPaper) #1
Chapter 14 ■ pOp

261

the pOp-3 prOtOCOL

purpose: allow download of e-mail from inbox

Standard: rFC 1939 (May 1996)

runs atop: tCp/Ip

Default port: 110 (cleartext), 995 (SSL)

Libraries: poplib

Listing 14-1. A Very Simple POP Session


#!/usr/bin/env python3


Foundations of Python Network Programming, Third Edition


https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter14/popconn.py


import getpass, poplib, sys


def main():
if len(sys.argv) != 3:
print('usage: %s hostname username' % sys.argv[0])
exit(2)


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


p = poplib.POP3SSL(hostname) # or "POP3" if SSL is not supported
try:
p.user(username)
p.pass
(passwd)
except poplib.error_proto as e:
print("Login failed:", e)
else:
status = p.stat()
print("You have %d messages totaling %d bytes" % status)
finally:
p.quit()


if name == 'main':
main()


■ Caution although this program does not alter any messages, some pOp servers will nonetheless alter mailbox flags


simply because you connected. running the examples in this chapter against a live mailbox could cause you to lose


information about which messages are read, unread, new, or old. Unfortunately, that behavior is server dependent and


beyond the control of pOp clients. I strongly recommend running these examples against a test mailbox rather than your


live mailbox!

Free download pdf