/Doc/library/poplib.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 197 lines · 115 code · 82 blank · 0 comment · 0 complexity · ef8d1836e77759e7d51c6e04e4a509f8 MD5 · raw file

  1. :mod:`poplib` --- POP3 protocol client
  2. ======================================
  3. .. module:: poplib
  4. :synopsis: POP3 protocol client (requires sockets).
  5. .. sectionauthor:: Andrew T. Csillag
  6. .. revised by ESR, January 2000
  7. .. index:: pair: POP3; protocol
  8. This module defines a class, :class:`POP3`, which encapsulates a connection to a
  9. POP3 server and implements the protocol as defined in :rfc:`1725`. The
  10. :class:`POP3` class supports both the minimal and optional command sets.
  11. Additionally, this module provides a class :class:`POP3_SSL`, which provides
  12. support for connecting to POP3 servers that use SSL as an underlying protocol
  13. layer.
  14. Note that POP3, though widely supported, is obsolescent. The implementation
  15. quality of POP3 servers varies widely, and too many are quite poor. If your
  16. mailserver supports IMAP, you would be better off using the
  17. :class:`imaplib.IMAP4` class, as IMAP servers tend to be better implemented.
  18. A single class is provided by the :mod:`poplib` module:
  19. .. class:: POP3(host[, port[, timeout]])
  20. This class implements the actual POP3 protocol. The connection is created when
  21. the instance is initialized. If *port* is omitted, the standard POP3 port (110)
  22. is used. The optional *timeout* parameter specifies a timeout in seconds for the
  23. connection attempt (if not specified, the global default timeout setting will
  24. be used).
  25. .. versionchanged:: 2.6
  26. *timeout* was added.
  27. .. class:: POP3_SSL(host[, port[, keyfile[, certfile]]])
  28. This is a subclass of :class:`POP3` that connects to the server over an SSL
  29. encrypted socket. If *port* is not specified, 995, the standard POP3-over-SSL
  30. port is used. *keyfile* and *certfile* are also optional - they can contain a
  31. PEM formatted private key and certificate chain file for the SSL connection.
  32. .. versionadded:: 2.4
  33. One exception is defined as an attribute of the :mod:`poplib` module:
  34. .. exception:: error_proto
  35. Exception raised on any errors from this module (errors from :mod:`socket`
  36. module are not caught). The reason for the exception is passed to the
  37. constructor as a string.
  38. .. seealso::
  39. Module :mod:`imaplib`
  40. The standard Python IMAP module.
  41. `Frequently Asked Questions About Fetchmail <http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html>`_
  42. The FAQ for the :program:`fetchmail` POP/IMAP client collects information on
  43. POP3 server variations and RFC noncompliance that may be useful if you need to
  44. write an application based on the POP protocol.
  45. .. _pop3-objects:
  46. POP3 Objects
  47. ------------
  48. All POP3 commands are represented by methods of the same name, in lower-case;
  49. most return the response text sent by the server.
  50. An :class:`POP3` instance has the following methods:
  51. .. method:: POP3.set_debuglevel(level)
  52. Set the instance's debugging level. This controls the amount of debugging
  53. output printed. The default, ``0``, produces no debugging output. A value of
  54. ``1`` produces a moderate amount of debugging output, generally a single line
  55. per request. A value of ``2`` or higher produces the maximum amount of
  56. debugging output, logging each line sent and received on the control connection.
  57. .. method:: POP3.getwelcome()
  58. Returns the greeting string sent by the POP3 server.
  59. .. method:: POP3.user(username)
  60. Send user command, response should indicate that a password is required.
  61. .. method:: POP3.pass_(password)
  62. Send password, response includes message count and mailbox size. Note: the
  63. mailbox on the server is locked until :meth:`quit` is called.
  64. .. method:: POP3.apop(user, secret)
  65. Use the more secure APOP authentication to log into the POP3 server.
  66. .. method:: POP3.rpop(user)
  67. Use RPOP authentication (similar to UNIX r-commands) to log into POP3 server.
  68. .. method:: POP3.stat()
  69. Get mailbox status. The result is a tuple of 2 integers: ``(message count,
  70. mailbox size)``.
  71. .. method:: POP3.list([which])
  72. Request message list, result is in the form ``(response, ['mesg_num octets',
  73. ...], octets)``. If *which* is set, it is the message to list.
  74. .. method:: POP3.retr(which)
  75. Retrieve whole message number *which*, and set its seen flag. Result is in form
  76. ``(response, ['line', ...], octets)``.
  77. .. method:: POP3.dele(which)
  78. Flag message number *which* for deletion. On most servers deletions are not
  79. actually performed until QUIT (the major exception is Eudora QPOP, which
  80. deliberately violates the RFCs by doing pending deletes on any disconnect).
  81. .. method:: POP3.rset()
  82. Remove any deletion marks for the mailbox.
  83. .. method:: POP3.noop()
  84. Do nothing. Might be used as a keep-alive.
  85. .. method:: POP3.quit()
  86. Signoff: commit changes, unlock mailbox, drop connection.
  87. .. method:: POP3.top(which, howmuch)
  88. Retrieves the message header plus *howmuch* lines of the message after the
  89. header of message number *which*. Result is in form ``(response, ['line', ...],
  90. octets)``.
  91. The POP3 TOP command this method uses, unlike the RETR command, doesn't set the
  92. message's seen flag; unfortunately, TOP is poorly specified in the RFCs and is
  93. frequently broken in off-brand servers. Test this method by hand against the
  94. POP3 servers you will use before trusting it.
  95. .. method:: POP3.uidl([which])
  96. Return message digest (unique id) list. If *which* is specified, result contains
  97. the unique id for that message in the form ``'response mesgnum uid``, otherwise
  98. result is list ``(response, ['mesgnum uid', ...], octets)``.
  99. Instances of :class:`POP3_SSL` have no additional methods. The interface of this
  100. subclass is identical to its parent.
  101. .. _pop3-example:
  102. POP3 Example
  103. ------------
  104. Here is a minimal example (without error checking) that opens a mailbox and
  105. retrieves and prints all messages::
  106. import getpass, poplib
  107. M = poplib.POP3('localhost')
  108. M.user(getpass.getuser())
  109. M.pass_(getpass.getpass())
  110. numMessages = len(M.list()[1])
  111. for i in range(numMessages):
  112. for j in M.retr(i+1)[1]:
  113. print j
  114. At the end of the module, there is a test section that contains a more extensive
  115. example of usage.