Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Doc/library/poplib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,75 @@ retrieves and prints all messages::

At the end of the module, there is a test section that contains a more extensive
example of usage.


.. _poplib-cli:

Command-line usage
------------------

The :mod:`!poplib` module can be executed from the command line.

.. code-block:: sh

python -m poplib [-h] [--port PORT] [--timeout TIMEOUT] [-v] [--ssl | --tls] host user password

The following options are accepted:

.. program:: poplib

.. option:: -h, --help

Show the help message and exit.

.. versionadded:: next

.. option:: host

POP3 host.

.. option:: user

POP3 :meth:`user <POP3.user>`.

.. option:: password

POP3 :meth:`password <POP3.pass_>`.

.. option:: --port PORT

POP3 port.

.. versionadded:: next

.. option:: --timeout TIMEOUT

Connection timeout.

.. versionadded:: next

.. option:: --ssl

Use :class:`POP3_SSL` if it is available.
If not, this is a no-op.
Mutually exclusive with :option:`--tls`.

.. versionadded:: next

.. option:: --tls

Use :meth:`~POP3.stls` if it is available.
If not, this is a no-op.
Mutually exclusive with :option:`--ssl`.

.. versionadded:: next

.. option:: -v
--verbose

Set the output :meth:`verbosity <POP3.set_debuglevel>`.
Useful for debug purposes.
This option can be passed multiple times like:
``-vv`` or ``--vebose --verbose``.

.. versionadded:: next
103 changes: 91 additions & 12 deletions Lib/poplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,96 @@ def stls(self, context=None):

__all__.append("POP3_SSL")

if __name__ == "__main__":
a = POP3(sys.argv[1])
print(a.getwelcome())
a.user(sys.argv[2])
a.pass_(sys.argv[3])
a.list()
(numMsgs, totalSize) = a.stat()
for i in range(1, numMsgs + 1):
(header, msg, octets) = a.retr(i)
print("Message %d:" % i)

# CLI

def _parse_args(argv=None):
import argparse

parser = argparse.ArgumentParser()
# Params kept like this for the backward compatibility:
parser.add_argument('host', help='POP3 host')
parser.add_argument('user', help='POP3 user')
parser.add_argument('password', help='POP3 password')
# New ones:
parser.add_argument(
'--port',
type=int,
default=None,
help='port to be used for the connection',
)
parser.add_argument(
'--timeout',
type=int,
default=None,
help='timeout to be used for the connection',
)
parser.add_argument(
'-v',
'--verbose',
action='count',
default=0,
help='flag to show the verbose debug output, can be: 0 (default), 1, 2',
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--ssl',
action='store_true',
help='should SSL be used (when SSL is not available, it is a no-op)',
)
group.add_argument(
'--tls',
action='store_true',
help='should TLS be used (when SSL is not available, it is a no-op)',
)

return parser.parse_args(argv)


def _build_client(args, ssl_context=None):
kwargs = {}
if args.port is not None:
kwargs['port'] = args.port
if args.timeout is not None:
kwargs['timeout'] = args.timeout
if ssl_context is not None:
kwargs['context'] = ssl_context

if HAVE_SSL and args.ssl:
assert not args.tls # mutually exclusive
pop3 = POP3_SSL(args.host, **kwargs)
else:
pop3 = POP3(args.host, **kwargs)
pop3.set_debuglevel(args.verbose)
if HAVE_SSL and args.tls:
assert not args.ssl # mutually exclusive
pop3.stls()
return pop3


def _run_main(pop3, args):
print(pop3.getwelcome().decode(pop3.encoding))
pop3.user(args.user)
pop3.pass_(args.password)
pop3.list()

num_msgs, _total_size = pop3.stat()
for i in range(1, num_msgs + 1):
_header, msg, _octets = pop3.retr(i)
print(f"Message {i}:")
for line in msg:
print(' ' + line)
print(' ', line.decode(pop3.encoding))
print('-----------------------')
a.quit()


def _main(argv=None):
args = _parse_args(argv)
pop3 = _build_client(args)
try:
_run_main(pop3, args)
finally:
pop3.quit()


if __name__ == "__main__":
_main()
Loading
Loading