Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`nntplib` --- NNTP protocol client |
| 3 | ======================================= |
| 4 | |
| 5 | .. module:: nntplib |
| 6 | :synopsis: NNTP protocol client (requires sockets). |
| 7 | |
| 8 | |
| 9 | .. index:: |
| 10 | pair: NNTP; protocol |
| 11 | single: Network News Transfer Protocol |
| 12 | |
| 13 | This module defines the class :class:`NNTP` which implements the client side of |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 14 | the Network News Transfer Protocol. It can be used to implement a news reader |
| 15 | or poster, or automated news processors. It is compatible with :rfc:`3977` |
| 16 | as well as the older :rfc:`977` and :rfc:`2980`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
| 18 | Here are two small examples of how it can be used. To list some statistics |
| 19 | about a newsgroup and print the subjects of the last 10 articles:: |
| 20 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 21 | >>> s = nntplib.NNTP('news.gmane.org') |
Antoine Pitrou | e339651 | 2010-09-07 18:44:12 +0000 | [diff] [blame] | 22 | >>> resp, count, first, last, name = s.group('gmane.comp.python.committers') |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 23 | >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last) |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 24 | Group gmane.comp.python.committers has 1096 articles, range 1 to 1096 |
| 25 | >>> resp, overviews = s.over((last - 9, last)) |
| 26 | >>> for id, over in overviews: |
| 27 | ... print(id, nntplib.decode_header(over['subject'])) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 28 | ... |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 29 | 1087 Re: Commit privileges for Łukasz Langa |
| 30 | 1088 Re: 3.2 alpha 2 freeze |
| 31 | 1089 Re: 3.2 alpha 2 freeze |
| 32 | 1090 Re: Commit privileges for Łukasz Langa |
| 33 | 1091 Re: Commit privileges for Łukasz Langa |
| 34 | 1092 Updated ssh key |
| 35 | 1093 Re: Updated ssh key |
| 36 | 1094 Re: Updated ssh key |
| 37 | 1095 Hello fellow committers! |
| 38 | 1096 Re: Hello fellow committers! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | >>> s.quit() |
Antoine Pitrou | e339651 | 2010-09-07 18:44:12 +0000 | [diff] [blame] | 40 | '205 Bye!' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 42 | To post an article from a binary file (this assumes that the article has valid |
Antoine Pitrou | e339651 | 2010-09-07 18:44:12 +0000 | [diff] [blame] | 43 | headers, and that you have right to post on the particular newsgroup):: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 44 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 45 | >>> s = nntplib.NNTP('news.gmane.org') |
| 46 | >>> f = open('/tmp/article.txt', 'rb') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | >>> s.post(f) |
| 48 | '240 Article posted successfully.' |
| 49 | >>> s.quit() |
Antoine Pitrou | e339651 | 2010-09-07 18:44:12 +0000 | [diff] [blame] | 50 | '205 Bye!' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 52 | The module itself defines the following classes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
| 54 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 55 | .. class:: NNTP(host, port=119, user=None, password=None, readermode=None, usenetrc=True, [timeout]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
Antoine Pitrou | a078115 | 2010-11-05 19:16:37 +0000 | [diff] [blame^] | 57 | Return a new :class:`NNTP` object, representing a connection |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 58 | to the NNTP server running on host *host*, listening at port *port*. |
| 59 | An optional *timeout* can be specified for the socket connection. |
| 60 | If the optional *user* and *password* are provided, or if suitable |
| 61 | credentials are present in :file:`/.netrc` and the optional flag *usenetrc* |
| 62 | is true (the default), the ``AUTHINFO USER`` and ``AUTHINFO PASS`` commands |
| 63 | are used to identify and authenticate the user to the server. If the optional |
| 64 | flag *readermode* is true, then a ``mode reader`` command is sent before |
| 65 | authentication is performed. Reader mode is sometimes necessary if you are |
| 66 | connecting to an NNTP server on the local machine and intend to call |
| 67 | reader-specific commands, such as ``group``. If you get unexpected |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | :exc:`NNTPPermanentError`\ s, you might need to set *readermode*. |
| 69 | *readermode* defaults to ``None``. *usenetrc* defaults to ``True``. |
| 70 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
| 72 | .. exception:: NNTPError |
| 73 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 74 | Derived from the standard exception :exc:`Exception`, this is the base |
| 75 | class for all exceptions raised by the :mod:`nntplib` module. Instances |
| 76 | of this class have the following attribute: |
| 77 | |
| 78 | .. attribute:: response |
| 79 | |
| 80 | The response of the server if available, as a :class:`str` object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | .. exception:: NNTPReplyError |
| 84 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 85 | Exception raised when an unexpected reply is received from the server. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | .. exception:: NNTPTemporaryError |
| 89 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 90 | Exception raised when a response code in the range 400--499 is received. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | .. exception:: NNTPPermanentError |
| 94 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 95 | Exception raised when a response code in the range 500--599 is received. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | .. exception:: NNTPProtocolError |
| 99 | |
| 100 | Exception raised when a reply is received from the server that does not begin |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 101 | with a digit in the range 1--5. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | .. exception:: NNTPDataError |
| 105 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 106 | Exception raised when there is some error in the response data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | .. _nntp-objects: |
| 110 | |
| 111 | NNTP Objects |
| 112 | ------------ |
| 113 | |
Antoine Pitrou | a078115 | 2010-11-05 19:16:37 +0000 | [diff] [blame^] | 114 | When connected, :class:`NNTP` objects support the following methods and |
| 115 | attributes. |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | a078115 | 2010-11-05 19:16:37 +0000 | [diff] [blame^] | 117 | Attributes |
| 118 | ^^^^^^^^^^ |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 119 | |
Antoine Pitrou | a078115 | 2010-11-05 19:16:37 +0000 | [diff] [blame^] | 120 | .. attribute:: NNTP.nntp_version |
| 121 | |
| 122 | An integer representing the version of the NNTP protocol supported by the |
| 123 | server. In practice, this should be ``2`` for servers advertising |
| 124 | :rfc:`3977` compliance and ``1`` for others. |
| 125 | |
| 126 | .. versionadded:: 3.2 |
| 127 | |
| 128 | .. attribute:: NNTP.nntp_implementation |
| 129 | |
| 130 | A string describing the software name and version of the NNTP server, |
| 131 | or :const:`None` if not advertised by the server. |
| 132 | |
| 133 | .. versionadded:: 3.2 |
| 134 | |
| 135 | Methods |
| 136 | ^^^^^^^ |
| 137 | |
| 138 | The *response* that is returned as the first item in the return tuple of almost |
| 139 | all methods is the server's response: a string beginning with a three-digit |
| 140 | code. If the server's response indicates an error, the method raises one of |
| 141 | the above exceptions. |
| 142 | |
| 143 | Many of the following methods take an optional keyword-only argument *file*. |
| 144 | When the *file* argument is supplied, it must be either a :term:`file object` |
| 145 | opened for binary writing, or the name of an on-disk file to be written to. |
| 146 | The method will then write any data returned by the server (except for the |
| 147 | response line and the terminating dot) to the file; any list of lines, |
| 148 | tuples or objects that the method normally returns will be empty. |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 149 | |
| 150 | .. versionchanged:: 3.2 |
| 151 | Many of the following methods have been reworked and fixed, which makes |
| 152 | them incompatible with their 3.1 counterparts. |
| 153 | |
| 154 | |
| 155 | .. method:: NNTP.quit() |
| 156 | |
| 157 | Send a ``QUIT`` command and close the connection. Once this method has been |
| 158 | called, no other methods of the NNTP object should be called. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | |
| 160 | |
| 161 | .. method:: NNTP.getwelcome() |
| 162 | |
| 163 | Return the welcome message sent by the server in reply to the initial |
| 164 | connection. (This message sometimes contains disclaimers or help information |
| 165 | that may be relevant to the user.) |
| 166 | |
| 167 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 168 | .. method:: NNTP.getcapabilities() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 170 | Return the :rfc:`3977` capabilities advertised by the server, as a |
| 171 | :class:`dict` instance mapping capability names to (possibly empty) lists |
| 172 | of values. On legacy servers which don't understand the ``CAPABILITIES`` |
| 173 | command, an empty dictionary is returned instead. |
| 174 | |
| 175 | >>> s = NNTP('news.gmane.org') |
| 176 | >>> 'POST' in s.getcapabilities() |
| 177 | True |
| 178 | |
| 179 | .. versionadded:: 3.2 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
| 181 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 182 | .. method:: NNTP.newgroups(date, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 184 | Send a ``NEWGROUPS`` command. The *date* argument should be a |
| 185 | :class:`datetime.date` or :class:`datetime.datetime` object. |
| 186 | Return a pair ``(response, groups)`` where *groups* is a list representing |
| 187 | the groups that are new since the given *date*. If *file* is supplied, |
| 188 | though, then *groups* will be empty. |
| 189 | |
| 190 | >>> from datetime import date, timedelta |
| 191 | >>> resp, groups = s.newgroups(date.today() - timedelta(days=3)) |
| 192 | >>> len(groups) |
| 193 | 85 |
| 194 | >>> groups[0] |
| 195 | GroupInfo(group='gmane.network.tor.devel', last='4', first='1', flag='m') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 196 | |
| 197 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 198 | .. method:: NNTP.newnews(group, date, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | |
| 200 | Send a ``NEWNEWS`` command. Here, *group* is a group name or ``'*'``, and |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 201 | *date* has the same meaning as for :meth:`newgroups`. Return a pair |
| 202 | ``(response, articles)`` where *articles* is a list of message ids. |
| 203 | |
| 204 | This command is frequently disabled by NNTP server administrators. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 205 | |
| 206 | |
Antoine Pitrou | 08eeada | 2010-11-04 21:36:15 +0000 | [diff] [blame] | 207 | .. method:: NNTP.list(group_pattern=None, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | 08eeada | 2010-11-04 21:36:15 +0000 | [diff] [blame] | 209 | Send a ``LIST`` or ``LIST ACTIVE`` command. Return a pair |
| 210 | ``(response, list)`` where *list* is a list of tuples representing all |
| 211 | the groups available from this NNTP server, optionally matching the |
| 212 | pattern string *group_pattern*. Each tuple has the form |
| 213 | ``(group, last, first, flag)``, where *group* is a group name, *last* |
| 214 | and *first* are the last and first article numbers, and *flag* usually |
| 215 | takes one of these values: |
Antoine Pitrou | 7dd1af0 | 2010-11-03 18:32:54 +0000 | [diff] [blame] | 216 | |
| 217 | * ``y``: Local postings and articles from peers are allowed. |
| 218 | * ``m``: The group is moderated and all postings must be approved. |
| 219 | * ``n``: No local postings are allowed, only articles from peers. |
| 220 | * ``j``: Articles from peers are filed in the junk group instead. |
| 221 | * ``x``: No local postings, and articles from peers are ignored. |
| 222 | * ``=foo.bar``: Articles are filed in the ``foo.bar`` group instead. |
| 223 | |
| 224 | If *flag* has another value, then the status of the newsgroup should be |
| 225 | considered unknown. |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 226 | |
Antoine Pitrou | 08eeada | 2010-11-04 21:36:15 +0000 | [diff] [blame] | 227 | This command can return very large results, especially if *group_pattern* |
| 228 | is not specified. It is best to cache the results offline unless you |
| 229 | really need to refresh them. |
| 230 | |
| 231 | .. versionchanged:: 3.2 |
| 232 | *group_pattern* was added. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | |
| 234 | |
| 235 | .. method:: NNTP.descriptions(grouppattern) |
| 236 | |
| 237 | Send a ``LIST NEWSGROUPS`` command, where *grouppattern* is a wildmat string as |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 238 | specified in :rfc:`3977` (it's essentially the same as DOS or UNIX shell wildcard |
| 239 | strings). Return a pair ``(response, descriptions)``, where *descriptions* |
| 240 | is a dictionary mapping group names to textual descriptions. |
| 241 | |
| 242 | >>> resp, descs = s.descriptions('gmane.comp.python.*') |
| 243 | >>> len(descs) |
| 244 | 295 |
| 245 | >>> descs.popitem() |
| 246 | ('gmane.comp.python.bio.general', 'BioPython discussion list (Moderated)') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 247 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | |
| 249 | .. method:: NNTP.description(group) |
| 250 | |
| 251 | Get a description for a single group *group*. If more than one group matches |
| 252 | (if 'group' is a real wildmat string), return the first match. If no group |
| 253 | matches, return an empty string. |
| 254 | |
| 255 | This elides the response code from the server. If the response code is needed, |
| 256 | use :meth:`descriptions`. |
| 257 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 258 | |
| 259 | .. method:: NNTP.group(name) |
| 260 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 261 | Send a ``GROUP`` command, where *name* is the group name. The group is |
| 262 | selected as the current group, if it exists. Return a tuple |
| 263 | ``(response, count, first, last, name)`` where *count* is the (estimated) |
| 264 | number of articles in the group, *first* is the first article number in |
| 265 | the group, *last* is the last article number in the group, and *name* |
| 266 | is the group name. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 267 | |
| 268 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 269 | .. method:: NNTP.over(message_spec, *, file=None) |
| 270 | |
| 271 | Send a ``OVER`` command, or a ``XOVER`` command on legacy servers. |
| 272 | *message_spec* can be either a string representing a message id, or |
| 273 | a ``(first, last)`` tuple of numbers indicating a range of articles in |
| 274 | the current group, or a ``(first, None)`` tuple indicating a range of |
| 275 | articles starting from *first* to the last article in the current group, |
| 276 | or :const:`None` to select the current article in the current group. |
| 277 | |
| 278 | Return a pair ``(response, overviews)``. *overviews* is a list of |
| 279 | ``(article_number, overview)`` tuples, one for each article selected |
| 280 | by *message_spec*. Each *overview* is a dictionary with the same number |
| 281 | of items, but this number depends on the server. These items are either |
| 282 | message headers (the key is then the lower-cased header name) or metadata |
| 283 | items (the key is then the metadata name prepended with ``":"``). The |
| 284 | following items are guaranteed to be present by the NNTP specification: |
| 285 | |
| 286 | * the ``subject``, ``from``, ``date``, ``message-id`` and ``references`` |
| 287 | headers |
| 288 | * the ``:bytes`` metadata: the number of bytes in the entire raw article |
| 289 | (including headers and body) |
| 290 | * the ``:lines`` metadata: the number of lines in the article body |
| 291 | |
Antoine Pitrou | 4103bc0 | 2010-11-03 18:18:43 +0000 | [diff] [blame] | 292 | The value of each item is either a string, or :const:`None` if not present. |
| 293 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 294 | It is advisable to use the :func:`decode_header` function on header |
| 295 | values when they may contain non-ASCII characters:: |
| 296 | |
| 297 | >>> _, _, first, last, _ = s.group('gmane.comp.python.devel') |
| 298 | >>> resp, overviews = s.over((last, last)) |
| 299 | >>> art_num, over = overviews[0] |
| 300 | >>> art_num |
| 301 | 117216 |
| 302 | >>> list(over.keys()) |
| 303 | ['xref', 'from', ':lines', ':bytes', 'references', 'date', 'message-id', 'subject'] |
| 304 | >>> over['from'] |
| 305 | '=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= <martin@v.loewis.de>' |
| 306 | >>> nntplib.decode_header(over['from']) |
| 307 | '"Martin v. Löwis" <martin@v.loewis.de>' |
| 308 | |
| 309 | .. versionadded:: 3.2 |
| 310 | |
| 311 | |
| 312 | .. method:: NNTP.help(*, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 313 | |
| 314 | Send a ``HELP`` command. Return a pair ``(response, list)`` where *list* is a |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 315 | list of help strings. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 316 | |
| 317 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 318 | .. method:: NNTP.stat(message_spec=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 320 | Send a ``STAT`` command, where *message_spec* is either a message id |
| 321 | (enclosed in ``'<'`` and ``'>'``) or an article number in the current group. |
| 322 | If *message_spec* is omitted or :const:`None`, the current article in the |
| 323 | current group is considered. Return a triple ``(response, number, id)`` |
| 324 | where *number* is the article number and *id* is the message id. |
| 325 | |
| 326 | >>> _, _, first, last, _ = s.group('gmane.comp.python.devel') |
| 327 | >>> resp, number, message_id = s.stat(first) |
| 328 | >>> number, message_id |
| 329 | (9099, '<20030112190404.GE29873@epoch.metaslash.com>') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 330 | |
| 331 | |
| 332 | .. method:: NNTP.next() |
| 333 | |
| 334 | Send a ``NEXT`` command. Return as for :meth:`stat`. |
| 335 | |
| 336 | |
| 337 | .. method:: NNTP.last() |
| 338 | |
| 339 | Send a ``LAST`` command. Return as for :meth:`stat`. |
| 340 | |
| 341 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 342 | .. method:: NNTP.article(message_spec=None, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 344 | Send an ``ARTICLE`` command, where *message_spec* has the same meaning as |
| 345 | for :meth:`stat`. Return a tuple ``(response, info)`` where *info* |
| 346 | is a :class:`~collections.namedtuple` with three members *number*, |
| 347 | *message_id* and *lines* (in that order). *number* is the article number |
| 348 | in the group (or 0 if the information is not available), *message_id* the |
| 349 | message id as a string, and *lines* a list of lines (without terminating |
| 350 | newlines) comprising the raw message including headers and body. |
| 351 | |
| 352 | >>> resp, info = s.article('<20030112190404.GE29873@epoch.metaslash.com>') |
| 353 | >>> info.number |
| 354 | 0 |
| 355 | >>> info.message_id |
| 356 | '<20030112190404.GE29873@epoch.metaslash.com>' |
| 357 | >>> len(info.lines) |
| 358 | 65 |
| 359 | >>> info.lines[0] |
| 360 | b'Path: main.gmane.org!not-for-mail' |
| 361 | >>> info.lines[1] |
| 362 | b'From: Neal Norwitz <neal@metaslash.com>' |
| 363 | >>> info.lines[-3:] |
| 364 | [b'There is a patch for 2.3 as well as 2.2.', b'', b'Neal'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 365 | |
| 366 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 367 | .. method:: NNTP.head(message_spec=None, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 368 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 369 | Same as :meth:`article()`, but sends a ``HEAD`` command. The *lines* |
| 370 | returned (or written to *file*) will only contain the message headers, not |
| 371 | the body. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 372 | |
| 373 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 374 | .. method:: NNTP.body(message_spec=None, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 375 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 376 | Same as :meth:`article()`, but sends a ``BODY`` command. The *lines* |
| 377 | returned (or written to *file*) will only contain the message body, not the |
| 378 | headers. |
| 379 | |
| 380 | |
| 381 | .. method:: NNTP.post(data) |
| 382 | |
| 383 | Post an article using the ``POST`` command. The *data* argument is either |
| 384 | a :term:`file object` opened for binary reading, or any iterable of bytes |
| 385 | objects (representing raw lines of the article to be posted). It should |
| 386 | represent a well-formed news article, including the required headers. The |
| 387 | :meth:`post` method automatically escapes lines beginning with ``.`` and |
| 388 | appends the termination line. |
| 389 | |
| 390 | If the method succeeds, the server's response is returned. If the server |
| 391 | refuses posting, a :class:`NNTPReplyError` is raised. |
| 392 | |
| 393 | |
| 394 | .. method:: NNTP.ihave(message_id, data) |
| 395 | |
| 396 | Send an ``IHAVE`` command. *message_id* is the id of the message to send |
| 397 | to the server (enclosed in ``'<'`` and ``'>'``). The *data* parameter |
| 398 | and the return value are the same as for :meth:`post()`. |
| 399 | |
| 400 | |
| 401 | .. method:: NNTP.date() |
| 402 | |
| 403 | Return a pair ``(response, date)``. *date* is a :class:`~datetime.datetime` |
| 404 | object containing the current date and time of the server. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 405 | |
| 406 | |
| 407 | .. method:: NNTP.slave() |
| 408 | |
| 409 | Send a ``SLAVE`` command. Return the server's *response*. |
| 410 | |
| 411 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 412 | .. method:: NNTP.set_debuglevel(level) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 414 | Set the instance's debugging level. This controls the amount of debugging |
| 415 | output printed. The default, ``0``, produces no debugging output. A value of |
| 416 | ``1`` produces a moderate amount of debugging output, generally a single line |
| 417 | per request or response. A value of ``2`` or higher produces the maximum amount |
| 418 | of debugging output, logging each line sent and received on the connection |
| 419 | (including message text). |
| 420 | |
| 421 | |
| 422 | The following are optional NNTP extensions defined in :rfc:`2980`. Some of |
| 423 | them have been superseded by newer commands in :rfc:`3977`. |
| 424 | |
| 425 | |
| 426 | .. method:: NNTP.xhdr(header, string, *, file=None) |
| 427 | |
| 428 | Send an ``XHDR`` command. The *header* argument is a header keyword, e.g. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 429 | ``'subject'``. The *string* argument should have the form ``'first-last'`` |
| 430 | where *first* and *last* are the first and last article numbers to search. |
| 431 | Return a pair ``(response, list)``, where *list* is a list of pairs ``(id, |
| 432 | text)``, where *id* is an article number (as a string) and *text* is the text of |
| 433 | the requested header for that article. If the *file* parameter is supplied, then |
| 434 | the output of the ``XHDR`` command is stored in a file. If *file* is a string, |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 435 | then the method will open a file with that name, write to it then close it. |
| 436 | If *file* is a :term:`file object`, then it will start calling :meth:`write` on |
| 437 | it to store the lines of the command output. If *file* is supplied, then the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 438 | returned *list* is an empty list. |
| 439 | |
| 440 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 441 | .. method:: NNTP.xover(start, end, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 443 | Send an ``XOVER`` command. *start* and *end* are article numbers |
| 444 | delimiting the range of articles to select. The return value is the |
| 445 | same of for :meth:`over()`. It is recommended to use :meth:`over()` |
| 446 | instead, since it will automatically use the newer ``OVER`` command |
| 447 | if available. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 448 | |
| 449 | |
| 450 | .. method:: NNTP.xpath(id) |
| 451 | |
| 452 | Return a pair ``(resp, path)``, where *path* is the directory path to the |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 453 | article with message ID *id*. Most of the time, this extension is not |
| 454 | enabled by NNTP server administrators. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 455 | |
| 456 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 457 | .. XXX deprecated: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 458 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 459 | .. method:: NNTP.xgtitle(name, *, file=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | 69ab951 | 2010-09-29 15:03:40 +0000 | [diff] [blame] | 461 | Process an ``XGTITLE`` command, returning a pair ``(response, list)``, where |
| 462 | *list* is a list of tuples containing ``(name, title)``. If the *file* parameter |
| 463 | is supplied, then the output of the ``XGTITLE`` command is stored in a file. |
| 464 | If *file* is a string, then the method will open a file with that name, write |
| 465 | to it then close it. If *file* is a :term:`file object`, then it will start |
| 466 | calling :meth:`write` on it to store the lines of the command output. If *file* |
| 467 | is supplied, then the returned *list* is an empty list. This is an optional NNTP |
| 468 | extension, and may not be supported by all servers. |
| 469 | |
| 470 | RFC2980 says "It is suggested that this extension be deprecated". Use |
| 471 | :meth:`descriptions` or :meth:`description` instead. |
| 472 | |
| 473 | |
| 474 | Utility functions |
| 475 | ----------------- |
| 476 | |
| 477 | The module also defines the following utility function: |
| 478 | |
| 479 | |
| 480 | .. function:: decode_header(header_str) |
| 481 | |
| 482 | Decode a header value, un-escaping any escaped non-ASCII characters. |
| 483 | *header_str* must be a :class:`str` object. The unescaped value is |
| 484 | returned. Using this function is recommended to display some headers |
| 485 | in a human readable form:: |
| 486 | |
| 487 | >>> decode_header("Some subject") |
| 488 | 'Some subject' |
| 489 | >>> decode_header("=?ISO-8859-15?Q?D=E9buter_en_Python?=") |
| 490 | 'Débuter en Python' |
| 491 | >>> decode_header("Re: =?UTF-8?B?cHJvYmzDqG1lIGRlIG1hdHJpY2U=?=") |
| 492 | 'Re: problème de matrice' |