• nntplib —- NNTP protocol client
    • NNTP Objects
      • Attributes
      • 方法
    • Utility functions

    nntplib —- NNTP protocol client

    Source code:Lib/nntplib.py


    This module defines the class NNTP which implements the client side ofthe Network News Transfer Protocol. It can be used to implement a news readeror poster, or automated news processors. It is compatible with RFC 3977as well as the older RFC 977 and RFC 2980.

    Here are two small examples of how it can be used. To list some statisticsabout a newsgroup and print the subjects of the last 10 articles:

    1. >>> s = nntplib.NNTP('news.gmane.org')
    2. >>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
    3. >>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
    4. Group gmane.comp.python.committers has 1096 articles, range 1 to 1096
    5. >>> resp, overviews = s.over((last - 9, last))
    6. >>> for id, over in overviews:
    7. ... print(id, nntplib.decode_header(over['subject']))
    8. ...
    9. 1087 Re: Commit privileges for Łukasz Langa
    10. 1088 Re: 3.2 alpha 2 freeze
    11. 1089 Re: 3.2 alpha 2 freeze
    12. 1090 Re: Commit privileges for Łukasz Langa
    13. 1091 Re: Commit privileges for Łukasz Langa
    14. 1092 Updated ssh key
    15. 1093 Re: Updated ssh key
    16. 1094 Re: Updated ssh key
    17. 1095 Hello fellow committers!
    18. 1096 Re: Hello fellow committers!
    19. >>> s.quit()
    20. '205 Bye!'

    To post an article from a binary file (this assumes that the article has validheaders, and that you have right to post on the particular newsgroup):

    1. >>> s = nntplib.NNTP('news.gmane.org')
    2. >>> f = open('article.txt', 'rb')
    3. >>> s.post(f)
    4. '240 Article posted successfully.'
    5. >>> s.quit()
    6. '205 Bye!'

    The module itself defines the following classes:

    • class nntplib.NNTP(host, port=119, user=None, password=None, readermode=None, usenetrc=False[, timeout])
    • Return a new NNTP object, representing a connectionto the NNTP server running on host host, listening at port port.An optional timeout can be specified for the socket connection.If the optional user and password are provided, or if suitablecredentials are present in /.netrc and the optional flag usenetrc_is true, the AUTHINFO USER and AUTHINFO PASS commands are usedto identify and authenticate the user to the server. If the optionalflag _readermode is true, then a mode reader command is sent beforeauthentication is performed. Reader mode is sometimes necessary if you areconnecting to an NNTP server on the local machine and intend to callreader-specific commands, such as group. If you get unexpectedNNTPPermanentErrors, you might need to set readermode.The NNTP class supports the with statement tounconditionally consume OSError exceptions and to close the NNTPconnection when done, e.g.:
    1. >>> from nntplib import NNTP
    2. >>> with NNTP('news.gmane.org') as n:
    3. ... n.group('gmane.comp.python.committers')
    4. ... # doctest: +SKIP
    5. ('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
    6. >>>

    Raises an auditing event nntplib.connect with arguments self, host, port.

    All commands will raise an auditing eventnntplib.putline with arguments self and line,where line is the bytes about to be sent to the remote host.

    在 3.2 版更改: usenetrc is now False by default.

    在 3.3 版更改: 支持了 with 语句。

    • class nntplib.NNTPSSL(_host, port=563, user=None, password=None, ssl_context=None, readermode=None, usenetrc=False[, timeout])
    • Return a new NNTP_SSL object, representing an encryptedconnection to the NNTP server running on host host, listening atport port. NNTP_SSL objects have the same methods asNNTP objects. If port is omitted, port 563 (NNTPS) is used.ssl_context is also optional, and is a SSLContext object.Please read Security considerations for best practices.All other parameters behave the same as for NNTP.

    Note that SSL-on-563 is discouraged per RFC 4642, in favor ofSTARTTLS as described below. However, some servers only support theformer.

    Raises an auditing event nntplib.connect with arguments self, host, port.

    All commands will raise an auditing eventnntplib.putline with arguments self and line,where line is the bytes about to be sent to the remote host.

    3.2 新版功能.

    在 3.4 版更改: The class now supports hostname check withssl.SSLContext.check_hostname and Server Name Indication (seessl.HAS_SNI).

    • exception nntplib.NNTPError
    • Derived from the standard exception Exception, this is the baseclass for all exceptions raised by the nntplib module. Instancesof this class have the following attribute:

      • response
      • The response of the server if available, as a str object.
    • exception nntplib.NNTPReplyError

    • Exception raised when an unexpected reply is received from the server.

    • exception nntplib.NNTPTemporaryError

    • Exception raised when a response code in the range 400—499 is received.

    • exception nntplib.NNTPPermanentError

    • Exception raised when a response code in the range 500—599 is received.

    • exception nntplib.NNTPProtocolError

    • Exception raised when a reply is received from the server that does not beginwith a digit in the range 1—5.

    • exception nntplib.NNTPDataError

    • Exception raised when there is some error in the response data.

    NNTP Objects

    When connected, NNTP and NNTP_SSL objects support thefollowing methods and attributes.

    Attributes

    • NNTP.nntp_version
    • An integer representing the version of the NNTP protocol supported by theserver. In practice, this should be 2 for servers advertisingRFC 3977 compliance and 1 for others.

    3.2 新版功能.

    • NNTP.nntp_implementation
    • A string describing the software name and version of the NNTP server,or None if not advertised by the server.

    3.2 新版功能.

    方法

    The response that is returned as the first item in the return tuple of almostall methods is the server's response: a string beginning with a three-digitcode. If the server's response indicates an error, the method raises one ofthe above exceptions.

    Many of the following methods take an optional keyword-only argument file.When the file argument is supplied, it must be either a file objectopened for binary writing, or the name of an on-disk file to be written to.The method will then write any data returned by the server (except for theresponse line and the terminating dot) to the file; any list of lines,tuples or objects that the method normally returns will be empty.

    在 3.2 版更改: Many of the following methods have been reworked and fixed, which makesthem incompatible with their 3.1 counterparts.

    • NNTP.quit()
    • Send a QUIT command and close the connection. Once this method has beencalled, no other methods of the NNTP object should be called.

    • NNTP.getwelcome()

    • Return the welcome message sent by the server in reply to the initialconnection. (This message sometimes contains disclaimers or help informationthat may be relevant to the user.)

    • NNTP.getcapabilities()

    • Return the RFC 3977 capabilities advertised by the server, as adict instance mapping capability names to (possibly empty) listsof values. On legacy servers which don't understand the CAPABILITIEScommand, an empty dictionary is returned instead.
    1. >>> s = NNTP('news.gmane.org')
    2. >>> 'POST' in s.getcapabilities()
    3. True

    3.2 新版功能.

    • NNTP.login(user=None, password=None, usenetrc=True)
    • Send AUTHINFO commands with the user name and password. If user_and _password are None and usenetrc is true, credentials from~/.netrc will be used if possible.

    Unless intentionally delayed, login is normally performed during theNNTP object initialization and separately calling this functionis unnecessary. To force authentication to be delayed, you must not setuser or password when creating the object, and must set usenetrc toFalse.

    3.2 新版功能.

    • NNTP.starttls(context=None)
    • Send a STARTTLS command. This will enable encryption on the NNTPconnection. The context argument is optional and should be assl.SSLContext object. Please read Security considerations for bestpractices.

    Note that this may not be done after authentication information hasbeen transmitted, and authentication occurs by default if possible during aNNTP object initialization. See NNTP.login() for informationon suppressing this behavior.

    3.2 新版功能.

    在 3.4 版更改: The method now supports hostname check withssl.SSLContext.check_hostname and Server Name Indication (seessl.HAS_SNI).

    • NNTP.newgroups(date, *, file=None)
    • Send a NEWGROUPS command. The date argument should be adatetime.date or datetime.datetime object.Return a pair (response, groups) where groups is a list representingthe groups that are new since the given date. If file is supplied,though, then groups will be empty.
    1. >>> from datetime import date, timedelta
    2. >>> resp, groups = s.newgroups(date.today() - timedelta(days=3))
    3. >>> len(groups) # doctest: +SKIP
    4. 85
    5. >>> groups[0] # doctest: +SKIP
    6. GroupInfo(group='gmane.network.tor.devel', last='4', first='1', flag='m')
    • NNTP.newnews(group, date, *, file=None)
    • Send a NEWNEWS command. Here, group is a group name or '*', anddate has the same meaning as for newgroups(). Return a pair(response, articles) where articles is a list of message ids.

    This command is frequently disabled by NNTP server administrators.

    • NNTP.list(group_pattern=None, *, file=None)
    • Send a LIST or LIST ACTIVE command. Return a pair(response, list) where list is a list of tuples representing allthe groups available from this NNTP server, optionally matching thepattern string group_pattern. Each tuple has the form(group, last, first, flag), where group is a group name, last_and _first are the last and first article numbers, and flag usuallytakes one of these values:

      • y: Local postings and articles from peers are allowed.

      • m: The group is moderated and all postings must be approved.

      • n: No local postings are allowed, only articles from peers.

      • j: Articles from peers are filed in the junk group instead.

      • x: No local postings, and articles from peers are ignored.

      • =foo.bar: Articles are filed in the foo.bar group instead.

    If flag has another value, then the status of the newsgroup should beconsidered unknown.

    This command can return very large results, especially if _group_pattern_is not specified. It is best to cache the results offline unless youreally need to refresh them.

    在 3.2 版更改: group_pattern was added.

    • NNTP.descriptions(grouppattern)
    • Send a LIST NEWSGROUPS command, where grouppattern is a wildmat string asspecified in RFC 3977 (it's essentially the same as DOS or UNIX shell wildcardstrings). Return a pair (response, descriptions), where _descriptions_is a dictionary mapping group names to textual descriptions.
    1. >>> resp, descs = s.descriptions('gmane.comp.python.*')
    2. >>> len(descs) # doctest: +SKIP
    3. 295
    4. >>> descs.popitem() # doctest: +SKIP
    5. ('gmane.comp.python.bio.general', 'BioPython discussion list (Moderated)')
    • NNTP.description(group)
    • Get a description for a single group group. If more than one group matches(if 'group' is a real wildmat string), return the first match. If no groupmatches, return an empty string.

    This elides the response code from the server. If the response code is needed,use descriptions().

    • NNTP.group(name)
    • Send a GROUP command, where name is the group name. The group isselected as the current group, if it exists. Return a tuple(response, count, first, last, name) where count is the (estimated)number of articles in the group, first is the first article number inthe group, last is the last article number in the group, and _name_is the group name.

    • NNTP.over(message_spec, *, file=None)

    • Send an OVER command, or an XOVER command on legacy servers.message_spec can be either a string representing a message id, ora (first, last) tuple of numbers indicating a range of articles inthe current group, or a (first, None) tuple indicating a range ofarticles starting from first to the last article in the current group,or None to select the current article in the current group.

    Return a pair (response, overviews). overviews is a list of(articlenumber, overview) tuples, one for each article selectedby _message_spec. Each overview is a dictionary with the same numberof items, but this number depends on the server. These items are eithermessage headers (the key is then the lower-cased header name) or metadataitems (the key is then the metadata name prepended with ":"). Thefollowing items are guaranteed to be present by the NNTP specification:

    • the subject, from, date, message-id and referencesheaders

    • the :bytes metadata: the number of bytes in the entire raw article(including headers and body)

    • the :lines metadata: the number of lines in the article body

    The value of each item is either a string, or None if not present.

    It is advisable to use the decode_header() function on headervalues when they may contain non-ASCII characters:

    1. >>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
    2. >>> resp, overviews = s.over((last, last))
    3. >>> art_num, over = overviews[0]
    4. >>> art_num
    5. 117216
    6. >>> list(over.keys())
    7. ['xref', 'from', ':lines', ':bytes', 'references', 'date', 'message-id', 'subject']
    8. >>> over['from']
    9. '=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= <martin@v.loewis.de>'
    10. >>> nntplib.decode_header(over['from'])
    11. '"Martin v. Löwis" <martin@v.loewis.de>'

    3.2 新版功能.

    • NNTP.help(*, file=None)
    • Send a HELP command. Return a pair (response, list) where list is alist of help strings.

    • NNTP.stat(message_spec=None)

    • Send a STAT command, where message_spec is either a message id(enclosed in '<' and '>') or an article number in the current group.If message_spec is omitted or None, the current article in thecurrent group is considered. Return a triple (response, number, id)where number is the article number and id is the message id.
    1. >>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
    2. >>> resp, number, message_id = s.stat(first)
    3. >>> number, message_id
    4. (9099, '<20030112190404.GE29873@epoch.metaslash.com>')
    • NNTP.next()
    • Send a NEXT command. Return as for stat().

    • NNTP.last()

    • Send a LAST command. Return as for stat().

    • NNTP.article(message_spec=None, *, file=None)

    • Send an ARTICLE command, where message_spec has the same meaning asfor stat(). Return a tuple (response, info) where info_is a namedtuple with three attributes _number,message_id and lines (in that order). number is the article numberin the group (or 0 if the information is not available), message_id themessage id as a string, and lines a list of lines (without terminatingnewlines) comprising the raw message including headers and body.
    1. >>> resp, info = s.article('<20030112190404.GE29873@epoch.metaslash.com>')
    2. >>> info.number
    3. 0
    4. >>> info.message_id
    5. '<20030112190404.GE29873@epoch.metaslash.com>'
    6. >>> len(info.lines)
    7. 65
    8. >>> info.lines[0]
    9. b'Path: main.gmane.org!not-for-mail'
    10. >>> info.lines[1]
    11. b'From: Neal Norwitz <neal@metaslash.com>'
    12. >>> info.lines[-3:]
    13. [b'There is a patch for 2.3 as well as 2.2.', b'', b'Neal']
    • NNTP.head(message_spec=None, *, file=None)
    • Same as article(), but sends a HEAD command. The lines_returned (or written to _file) will only contain the message headers, notthe body.

    • NNTP.body(message_spec=None, *, file=None)

    • Same as article(), but sends a BODY command. The lines_returned (or written to _file) will only contain the message body, not theheaders.

    • NNTP.post(data)

    • Post an article using the POST command. The data argument is eithera file object opened for binary reading, or any iterable of bytesobjects (representing raw lines of the article to be posted). It shouldrepresent a well-formed news article, including the required headers. Thepost() method automatically escapes lines beginning with . andappends the termination line.

    If the method succeeds, the server's response is returned. If the serverrefuses posting, a NNTPReplyError is raised.

    • NNTP.ihave(message_id, data)
    • Send an IHAVE command. message_id is the id of the message to sendto the server (enclosed in '<' and '>'). The data parameterand the return value are the same as for post().

    • NNTP.date()

    • Return a pair (response, date). date is a datetimeobject containing the current date and time of the server.

    • NNTP.slave()

    • Send a SLAVE command. Return the server's response.

    • NNTP.setdebuglevel(_level)

    • Set the instance's debugging level. This controls the amount of debuggingoutput printed. The default, 0, produces no debugging output. A value of1 produces a moderate amount of debugging output, generally a single lineper request or response. A value of 2 or higher produces the maximum amountof debugging output, logging each line sent and received on the connection(including message text).

    The following are optional NNTP extensions defined in RFC 2980. Some ofthem have been superseded by newer commands in RFC 3977.

    • NNTP.xhdr(hdr, str, *, file=None)
    • Send an XHDR command. The hdr argument is a header keyword, e.g.'subject'. The str argument should have the form 'first-last'where first and last are the first and last article numbers to search.Return a pair (response, list), where list is a list of pairs (id,text), where id is an article number (as a string) and text is the text ofthe requested header for that article. If the file parameter is supplied, thenthe output of the XHDR command is stored in a file. If file is a string,then the method will open a file with that name, write to it then close it.If file is a file object, then it will start calling write() onit to store the lines of the command output. If file is supplied, then thereturned list is an empty list.

    • NNTP.xover(start, end, *, file=None)

    • Send an XOVER command. start and end are article numbersdelimiting the range of articles to select. The return value is thesame of for over(). It is recommended to use over()instead, since it will automatically use the newer OVER commandif available.

    • NNTP.xpath(id)

    • Return a pair (resp, path), where path is the directory path to thearticle with message ID id. Most of the time, this extension is notenabled by NNTP server administrators.

    3.3 版后已移除: The XPATH extension is not actively used.

    Utility functions

    The module also defines the following utility function:

    • nntplib.decodeheader(_header_str)
    • Decode a header value, un-escaping any escaped non-ASCII characters.header_str must be a str object. The unescaped value isreturned. Using this function is recommended to display some headersin a human readable form:
    1. >>> decode_header("Some subject")
    2. 'Some subject'
    3. >>> decode_header("=?ISO-8859-15?Q?D=E9buter_en_Python?=")
    4. 'Débuter en Python'
    5. >>> decode_header("Re: =?UTF-8?B?cHJvYmzDqG1lIGRlIG1hdHJpY2U=?=")
    6. 'Re: problème de matrice'