PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Doc/IronPythonDocs/library/nntplib.rst

http://github.com/IronLanguages/main
ReStructuredText | 348 lines | 235 code | 113 blank | 0 comment | 0 complexity | f78197b9c99420974fe1bf84be05659f MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. :mod:`nntplib` --- NNTP protocol client
  2. =======================================
  3. .. module:: nntplib
  4. :synopsis: NNTP protocol client (requires sockets).
  5. .. index::
  6. pair: NNTP; protocol
  7. single: Network News Transfer Protocol
  8. This module defines the class :class:`NNTP` which implements the client side of
  9. the NNTP protocol. It can be used to implement a news reader or poster, or
  10. automated news processors. For more information on NNTP (Network News Transfer
  11. Protocol), see Internet :rfc:`977`.
  12. Here are two small examples of how it can be used. To list some statistics
  13. about a newsgroup and print the subjects of the last 10 articles::
  14. >>> s = NNTP('news.cwi.nl')
  15. >>> resp, count, first, last, name = s.group('comp.lang.python')
  16. >>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
  17. Group comp.lang.python has 59 articles, range 3742 to 3803
  18. >>> resp, subs = s.xhdr('subject', first + '-' + last)
  19. >>> for id, sub in subs[-10:]: print id, sub
  20. ...
  21. 3792 Re: Removing elements from a list while iterating...
  22. 3793 Re: Who likes Info files?
  23. 3794 Emacs and doc strings
  24. 3795 a few questions about the Mac implementation
  25. 3796 Re: executable python scripts
  26. 3797 Re: executable python scripts
  27. 3798 Re: a few questions about the Mac implementation
  28. 3799 Re: PROPOSAL: A Generic Python Object Interface for Python C Modules
  29. 3802 Re: executable python scripts
  30. 3803 Re: \POSIX{} wait and SIGCHLD
  31. >>> s.quit()
  32. '205 news.cwi.nl closing connection. Goodbye.'
  33. To post an article from a file (this assumes that the article has valid
  34. headers)::
  35. >>> s = NNTP('news.cwi.nl')
  36. >>> f = open('/tmp/article')
  37. >>> s.post(f)
  38. '240 Article posted successfully.'
  39. >>> s.quit()
  40. '205 news.cwi.nl closing connection. Goodbye.'
  41. The module itself defines the following items:
  42. .. class:: NNTP(host[, port [, user[, password [, readermode] [, usenetrc]]]])
  43. Return a new instance of the :class:`NNTP` class, representing a connection
  44. to the NNTP server running on host *host*, listening at port *port*. The
  45. default *port* is 119. If the optional *user* and *password* are provided,
  46. or if suitable credentials are present in :file:`/.netrc` and the optional
  47. flag *usenetrc* is true (the default), the ``AUTHINFO USER`` and ``AUTHINFO
  48. PASS`` commands are used to identify and authenticate the user to the server.
  49. If the optional flag *readermode* is true, then a ``mode reader`` command is
  50. sent before authentication is performed. Reader mode is sometimes necessary
  51. if you are connecting to an NNTP server on the local machine and intend to
  52. call reader-specific commands, such as ``group``. If you get unexpected
  53. :exc:`NNTPPermanentError`\ s, you might need to set *readermode*.
  54. *readermode* defaults to ``None``. *usenetrc* defaults to ``True``.
  55. .. versionchanged:: 2.4
  56. *usenetrc* argument added.
  57. .. exception:: NNTPError
  58. Derived from the standard exception :exc:`Exception`, this is the base class for
  59. all exceptions raised by the :mod:`nntplib` module.
  60. .. exception:: NNTPReplyError
  61. Exception raised when an unexpected reply is received from the server. For
  62. backwards compatibility, the exception ``error_reply`` is equivalent to this
  63. class.
  64. .. exception:: NNTPTemporaryError
  65. Exception raised when an error code in the range 400--499 is received. For
  66. backwards compatibility, the exception ``error_temp`` is equivalent to this
  67. class.
  68. .. exception:: NNTPPermanentError
  69. Exception raised when an error code in the range 500--599 is received. For
  70. backwards compatibility, the exception ``error_perm`` is equivalent to this
  71. class.
  72. .. exception:: NNTPProtocolError
  73. Exception raised when a reply is received from the server that does not begin
  74. with a digit in the range 1--5. For backwards compatibility, the exception
  75. ``error_proto`` is equivalent to this class.
  76. .. exception:: NNTPDataError
  77. Exception raised when there is some error in the response data. For backwards
  78. compatibility, the exception ``error_data`` is equivalent to this class.
  79. .. _nntp-objects:
  80. NNTP Objects
  81. ------------
  82. NNTP instances have the following methods. The *response* that is returned as
  83. the first item in the return tuple of almost all methods is the server's
  84. response: a string beginning with a three-digit code. If the server's response
  85. indicates an error, the method raises one of the above exceptions.
  86. .. method:: NNTP.getwelcome()
  87. Return the welcome message sent by the server in reply to the initial
  88. connection. (This message sometimes contains disclaimers or help information
  89. that may be relevant to the user.)
  90. .. method:: NNTP.set_debuglevel(level)
  91. Set the instance's debugging level. This controls the amount of debugging
  92. output printed. The default, ``0``, produces no debugging output. A value of
  93. ``1`` produces a moderate amount of debugging output, generally a single line
  94. per request or response. A value of ``2`` or higher produces the maximum amount
  95. of debugging output, logging each line sent and received on the connection
  96. (including message text).
  97. .. method:: NNTP.newgroups(date, time, [file])
  98. Send a ``NEWGROUPS`` command. The *date* argument should be a string of the
  99. form ``'yymmdd'`` indicating the date, and *time* should be a string of the form
  100. ``'hhmmss'`` indicating the time. Return a pair ``(response, groups)`` where
  101. *groups* is a list of group names that are new since the given date and time. If
  102. the *file* parameter is supplied, then the output of the ``NEWGROUPS`` command
  103. is stored in a file. If *file* is a string, then the method will open a file
  104. object with that name, write to it then close it. If *file* is a file object,
  105. then it will start calling :meth:`write` on it to store the lines of the command
  106. output. If *file* is supplied, then the returned *list* is an empty list.
  107. .. method:: NNTP.newnews(group, date, time, [file])
  108. Send a ``NEWNEWS`` command. Here, *group* is a group name or ``'*'``, and
  109. *date* and *time* have the same meaning as for :meth:`newgroups`. Return a pair
  110. ``(response, articles)`` where *articles* is a list of message ids. If the
  111. *file* parameter is supplied, then the output of the ``NEWNEWS`` command is
  112. stored in a file. If *file* is a string, then the method will open a file
  113. object with that name, write to it then close it. If *file* is a file object,
  114. then it will start calling :meth:`write` on it to store the lines of the command
  115. output. If *file* is supplied, then the returned *list* is an empty list.
  116. .. method:: NNTP.list([file])
  117. Send a ``LIST`` command. Return a pair ``(response, list)`` where *list* is a
  118. list of tuples. Each tuple has the form ``(group, last, first, flag)``, where
  119. *group* is a group name, *last* and *first* are the last and first article
  120. numbers (as strings), and *flag* is ``'y'`` if posting is allowed, ``'n'`` if
  121. not, and ``'m'`` if the newsgroup is moderated. (Note the ordering: *last*,
  122. *first*.) If the *file* parameter is supplied, then the output of the ``LIST``
  123. command is stored in a file. If *file* is a string, then the method will open
  124. a file object with that name, write to it then close it. If *file* is a file
  125. object, then it will start calling :meth:`write` on it to store the lines of the
  126. command output. If *file* is supplied, then the returned *list* is an empty
  127. list.
  128. .. method:: NNTP.descriptions(grouppattern)
  129. Send a ``LIST NEWSGROUPS`` command, where *grouppattern* is a wildmat string as
  130. specified in RFC2980 (it's essentially the same as DOS or UNIX shell wildcard
  131. strings). Return a pair ``(response, list)``, where *list* is a list of tuples
  132. containing ``(name, title)``.
  133. .. versionadded:: 2.4
  134. .. method:: NNTP.description(group)
  135. Get a description for a single group *group*. If more than one group matches
  136. (if 'group' is a real wildmat string), return the first match. If no group
  137. matches, return an empty string.
  138. This elides the response code from the server. If the response code is needed,
  139. use :meth:`descriptions`.
  140. .. versionadded:: 2.4
  141. .. method:: NNTP.group(name)
  142. Send a ``GROUP`` command, where *name* is the group name. Return a tuple
  143. ``(response, count, first, last, name)`` where *count* is the (estimated) number
  144. of articles in the group, *first* is the first article number in the group,
  145. *last* is the last article number in the group, and *name* is the group name.
  146. The numbers are returned as strings.
  147. .. method:: NNTP.help([file])
  148. Send a ``HELP`` command. Return a pair ``(response, list)`` where *list* is a
  149. list of help strings. If the *file* parameter is supplied, then the output of
  150. the ``HELP`` command is stored in a file. If *file* is a string, then the
  151. method will open a file object with that name, write to it then close it. If
  152. *file* is a file object, then it will start calling :meth:`write` on it to store
  153. the lines of the command output. If *file* is supplied, then the returned *list*
  154. is an empty list.
  155. .. method:: NNTP.stat(id)
  156. Send a ``STAT`` command, where *id* is the message id (enclosed in ``'<'`` and
  157. ``'>'``) or an article number (as a string). Return a triple ``(response,
  158. number, id)`` where *number* is the article number (as a string) and *id* is the
  159. message id (enclosed in ``'<'`` and ``'>'``).
  160. .. method:: NNTP.next()
  161. Send a ``NEXT`` command. Return as for :meth:`stat`.
  162. .. method:: NNTP.last()
  163. Send a ``LAST`` command. Return as for :meth:`stat`.
  164. .. method:: NNTP.head(id)
  165. Send a ``HEAD`` command, where *id* has the same meaning as for :meth:`stat`.
  166. Return a tuple ``(response, number, id, list)`` where the first three are the
  167. same as for :meth:`stat`, and *list* is a list of the article's headers (an
  168. uninterpreted list of lines, without trailing newlines).
  169. .. method:: NNTP.body(id,[file])
  170. Send a ``BODY`` command, where *id* has the same meaning as for :meth:`stat`.
  171. If the *file* parameter is supplied, then the body is stored in a file. If
  172. *file* is a string, then the method will open a file object with that name,
  173. write to it then close it. If *file* is a file object, then it will start
  174. calling :meth:`write` on it to store the lines of the body. Return as for
  175. :meth:`head`. If *file* is supplied, then the returned *list* is an empty list.
  176. .. method:: NNTP.article(id)
  177. Send an ``ARTICLE`` command, where *id* has the same meaning as for
  178. :meth:`stat`. Return as for :meth:`head`.
  179. .. method:: NNTP.slave()
  180. Send a ``SLAVE`` command. Return the server's *response*.
  181. .. method:: NNTP.xhdr(header, string, [file])
  182. Send an ``XHDR`` command. This command is not defined in the RFC but is a
  183. common extension. The *header* argument is a header keyword, e.g.
  184. ``'subject'``. The *string* argument should have the form ``'first-last'``
  185. where *first* and *last* are the first and last article numbers to search.
  186. Return a pair ``(response, list)``, where *list* is a list of pairs ``(id,
  187. text)``, where *id* is an article number (as a string) and *text* is the text of
  188. the requested header for that article. If the *file* parameter is supplied, then
  189. the output of the ``XHDR`` command is stored in a file. If *file* is a string,
  190. then the method will open a file object with that name, write to it then close
  191. it. If *file* is a file object, then it will start calling :meth:`write` on it
  192. to store the lines of the command output. If *file* is supplied, then the
  193. returned *list* is an empty list.
  194. .. method:: NNTP.post(file)
  195. Post an article using the ``POST`` command. The *file* argument is an open file
  196. object which is read until EOF using its :meth:`readline` method. It should be
  197. a well-formed news article, including the required headers. The :meth:`post`
  198. method automatically escapes lines beginning with ``.``.
  199. .. method:: NNTP.ihave(id, file)
  200. Send an ``IHAVE`` command. *id* is a message id (enclosed in ``'<'`` and
  201. ``'>'``). If the response is not an error, treat *file* exactly as for the
  202. :meth:`post` method.
  203. .. method:: NNTP.date()
  204. Return a triple ``(response, date, time)``, containing the current date and time
  205. in a form suitable for the :meth:`newnews` and :meth:`newgroups` methods. This
  206. is an optional NNTP extension, and may not be supported by all servers.
  207. .. method:: NNTP.xgtitle(name, [file])
  208. Process an ``XGTITLE`` command, returning a pair ``(response, list)``, where
  209. *list* is a list of tuples containing ``(name, title)``. If the *file* parameter
  210. is supplied, then the output of the ``XGTITLE`` command is stored in a file.
  211. If *file* is a string, then the method will open a file object with that name,
  212. write to it then close it. If *file* is a file object, then it will start
  213. calling :meth:`write` on it to store the lines of the command output. If *file*
  214. is supplied, then the returned *list* is an empty list. This is an optional NNTP
  215. extension, and may not be supported by all servers.
  216. RFC2980 says "It is suggested that this extension be deprecated". Use
  217. :meth:`descriptions` or :meth:`description` instead.
  218. .. method:: NNTP.xover(start, end, [file])
  219. Return a pair ``(resp, list)``. *list* is a list of tuples, one for each
  220. article in the range delimited by the *start* and *end* article numbers. Each
  221. tuple is of the form ``(article number, subject, poster, date, id, references,
  222. size, lines)``. If the *file* parameter is supplied, then the output of the
  223. ``XOVER`` command is stored in a file. If *file* is a string, then the method
  224. will open a file object with that name, write to it then close it. If *file*
  225. is a file object, then it will start calling :meth:`write` on it to store the
  226. lines of the command output. If *file* is supplied, then the returned *list* is
  227. an empty list. This is an optional NNTP extension, and may not be supported by
  228. all servers.
  229. .. method:: NNTP.xpath(id)
  230. Return a pair ``(resp, path)``, where *path* is the directory path to the
  231. article with message ID *id*. This is an optional NNTP extension, and may not
  232. be supported by all servers.
  233. .. method:: NNTP.quit()
  234. Send a ``QUIT`` command and close the connection. Once this method has been
  235. called, no other methods of the NNTP object should be called.