Back to module index

Go to module by name

nntplib

An NNTP client class based on:
- RFC 977: Network News Transfer Protocol
- RFC 2980: Common NNTP Extensions
- RFC 3977: Network News Transfer Protocol (version 2)

Example:

>>> from nntplib import NNTP
>>> s = NNTP('news')
>>> resp, count, first, last, name = s.group('comp.lang.python')
>>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
Group comp.lang.python has 51 articles, range 5770 to 5821
>>> resp, subs = s.xhdr('subject', '{0}-{1}'.format(first, last))
>>> resp = s.quit()
>>>

Here 'resp' is the server response line.
Error responses are turned into exceptions.

To post an article from a file:
>>> f = open(filename, 'rb') # file containing article, including header
>>> resp = s.post(f)
>>>

For descriptions of all methods, read the comments in the code below.
Note that all arguments and return values representing article numbers
are strings, not numbers, since they are rarely used for calculations.

Classes

ArticleInfo

ArticleInfo(number, message_id, lines)
count(self, value, /)

  Return number of occurrences of value.
index(self, value, start=0, stop=9223372036854775807, /)

  Return first index of value.

  Raises ValueError if the value is not present.
lines = _tuplegetter(2, 'Alias for field number 2')
  Alias for field number 2
message_id = _tuplegetter(1, 'Alias for field number 1')
  Alias for field number 1
number = _tuplegetter(0, 'Alias for field number 0')
  Alias for field number 0

GroupInfo

GroupInfo(group, last, first, flag)
count(self, value, /)

  Return number of occurrences of value.
index(self, value, start=0, stop=9223372036854775807, /)

  Return first index of value.

  Raises ValueError if the value is not present.
first = _tuplegetter(2, 'Alias for field number 2')
  Alias for field number 2
flag = _tuplegetter(3, 'Alias for field number 3')
  Alias for field number 3
group = _tuplegetter(0, 'Alias for field number 0')
  Alias for field number 0
last = _tuplegetter(1, 'Alias for field number 1')
  Alias for field number 1

NNTP

article(self, message_spec=None, *, file=None)

  Process an ARTICLE command.  Argument:
          - message_spec: article number or message id
          - file: filename string or file object to store the article in
          Returns:
          - resp: server response if successful
          - ArticleInfo: (article number, message id, list of article lines)
        
body(self, message_spec=None, *, file=None)

  Process a BODY command.  Argument:
          - message_spec: article number or message id
          - file: filename string or file object to store the body in
          Returns:
          - resp: server response if successful
          - ArticleInfo: (article number, message id, list of body lines)
        
capabilities(self)

  Process a CAPABILITIES command.  Not supported by all servers.
          Return:
          - resp: server response if successful
          - caps: a dictionary mapping capability names to lists of tokens
          (for example {'VERSION': ['2'], 'OVER': [], LIST: ['ACTIVE', 'HEADERS'] })
        
date(self)

  Process the DATE command.
          Returns:
          - resp: server response if successful
          - date: datetime object
        
set_debuglevel(self, level)

  Set the debugging level.  Argument 'level' means:
          0: no debugging output (default)
          1: print commands and responses but not body text etc.
          2: also print raw lines read and sent before stripping CR/LF
description(self, group)

  Get a description for a single group.  If more than one
          group matches ('group' is a pattern), return the first.  If no
          group matches, return an empty string.

          This elides the response code from the server, since it can
          only be '215' or '285' (for xgtitle) anyway.  If the response
          code is needed, use the 'descriptions' method.

          NOTE: This neither checks for a wildcard in 'group' nor does
          it check whether the group actually exists.
descriptions(self, group_pattern)

  Get descriptions for a range of groups.
getcapabilities(self)

  Get the server capabilities, as read by __init__().
          If the CAPABILITIES command is not supported, an empty dict is
          returned.
getwelcome(self)

  Get the welcome message from the server
          (this is read and squirreled away by __init__()).
          If the response code is 200, posting is allowed;
          if it 201, posting is not allowed.
group(self, name)

  Process a GROUP command.  Argument:
          - group: the group name
          Returns:
          - resp: server response if successful
          - count: number of articles
          - first: first article number
          - last: last article number
          - name: the group name
        
head(self, message_spec=None, *, file=None)

  Process a HEAD command.  Argument:
          - message_spec: article number or message id
          - file: filename string or file object to store the headers in
          Returns:
          - resp: server response if successful
          - ArticleInfo: (article number, message id, list of header lines)
        
help(self, *, file=None)

  Process a HELP command. Argument:
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of strings returned by the server in response to the
                  HELP command
        
ihave(self, message_id, data)

  Process an IHAVE command.  Arguments:
          - message_id: message-id of the article
          - data: file containing the article
          Returns:
          - resp: server response if successful
          Note that if the server refuses the article an exception is raised.
last(self)

  Process a LAST command.  No arguments.  Return as for STAT.
list(self, group_pattern=None, *, file=None)

  Process a LIST or LIST ACTIVE command. Arguments:
          - group_pattern: a pattern indicating which groups to query
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of (group, last, first, flag) (strings)
        
login(self, user=None, password=None, usenetrc=True)
newgroups(self, date, *, file=None)

  Process a NEWGROUPS command.  Arguments:
          - date: a date or datetime object
          Return:
          - resp: server response if successful
          - list: list of newsgroup names
        
newnews(self, group, date, *, file=None)

  Process a NEWNEWS command.  Arguments:
          - group: group name or '*'
          - date: a date or datetime object
          Return:
          - resp: server response if successful
          - list: list of message ids
        
next(self)

  Process a NEXT command.  No arguments.  Return as for STAT.
over(self, message_spec, *, file=None)

  Process an OVER command.  If the command isn't supported, fall
          back to XOVER. Arguments:
          - message_spec:
              - either a message id, indicating the article to fetch
                information about
              - or a (start, end) tuple, indicating a range of article numbers;
                if end is None, information up to the newest message will be
                retrieved
              - or None, indicating the current article number must be used
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of dicts containing the response fields

          NOTE: the "message id" form isn't supported by XOVER
        
post(self, data)

  Process a POST command.  Arguments:
          - data: bytes object, iterable or file containing the article
          Returns:
          - resp: server response if successful
quit(self)

  Process a QUIT command and close the socket.  Returns:
          - resp: server response if successful
set_debuglevel(self, level)

  Set the debugging level.  Argument 'level' means:
          0: no debugging output (default)
          1: print commands and responses but not body text etc.
          2: also print raw lines read and sent before stripping CR/LF
slave(self)

  Process a SLAVE command.  Returns:
          - resp: server response if successful
        
starttls(self, context=None)

  Process a STARTTLS command. Arguments:
              - context: SSL context to use for the encrypted connection
            
stat(self, message_spec=None)

  Process a STAT command.  Argument:
          - message_spec: article number or message id (if not specified,
            the current article is selected)
          Returns:
          - resp: server response if successful
          - art_num: the article number
          - message_id: the message id
        
xhdr(self, hdr, str, *, file=None)

  Process an XHDR command (optional server extension).  Arguments:
          - hdr: the header type (e.g. 'subject')
          - str: an article nr, a message id, or a range nr1-nr2
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of (nr, value) strings
        
xover(self, start, end, *, file=None)

  Process an XOVER command (optional server extension) Arguments:
          - start: start of range
          - end: end of range
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of dicts containing the response fields
        
encoding = 'utf-8'
errors = 'surrogateescape'

NNTPDataError

Error in response data
with_traceback(...)

  Exception.with_traceback(tb) --
      set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>

NNTPError

Base class for all nntplib exceptions
with_traceback(...)

  Exception.with_traceback(tb) --
      set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>

NNTPPermanentError

5xx errors
with_traceback(...)

  Exception.with_traceback(tb) --
      set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>

NNTPProtocolError

Response does not begin with [1-5]
with_traceback(...)

  Exception.with_traceback(tb) --
      set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>

NNTPReplyError

Unexpected [123]xx reply
with_traceback(...)

  Exception.with_traceback(tb) --
      set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>

NNTPTemporaryError

4xx errors
with_traceback(...)

  Exception.with_traceback(tb) --
      set self.__traceback__ to tb and return self.
args = <attribute 'args' of 'BaseException' objects>

NNTP_SSL

article(self, message_spec=None, *, file=None)

  Process an ARTICLE command.  Argument:
          - message_spec: article number or message id
          - file: filename string or file object to store the article in
          Returns:
          - resp: server response if successful
          - ArticleInfo: (article number, message id, list of article lines)
        
body(self, message_spec=None, *, file=None)

  Process a BODY command.  Argument:
          - message_spec: article number or message id
          - file: filename string or file object to store the body in
          Returns:
          - resp: server response if successful
          - ArticleInfo: (article number, message id, list of body lines)
        
capabilities(self)

  Process a CAPABILITIES command.  Not supported by all servers.
          Return:
          - resp: server response if successful
          - caps: a dictionary mapping capability names to lists of tokens
          (for example {'VERSION': ['2'], 'OVER': [], LIST: ['ACTIVE', 'HEADERS'] })
        
date(self)

  Process the DATE command.
          Returns:
          - resp: server response if successful
          - date: datetime object
        
set_debuglevel(self, level)

  Set the debugging level.  Argument 'level' means:
          0: no debugging output (default)
          1: print commands and responses but not body text etc.
          2: also print raw lines read and sent before stripping CR/LF
description(self, group)

  Get a description for a single group.  If more than one
          group matches ('group' is a pattern), return the first.  If no
          group matches, return an empty string.

          This elides the response code from the server, since it can
          only be '215' or '285' (for xgtitle) anyway.  If the response
          code is needed, use the 'descriptions' method.

          NOTE: This neither checks for a wildcard in 'group' nor does
          it check whether the group actually exists.
descriptions(self, group_pattern)

  Get descriptions for a range of groups.
getcapabilities(self)

  Get the server capabilities, as read by __init__().
          If the CAPABILITIES command is not supported, an empty dict is
          returned.
getwelcome(self)

  Get the welcome message from the server
          (this is read and squirreled away by __init__()).
          If the response code is 200, posting is allowed;
          if it 201, posting is not allowed.
group(self, name)

  Process a GROUP command.  Argument:
          - group: the group name
          Returns:
          - resp: server response if successful
          - count: number of articles
          - first: first article number
          - last: last article number
          - name: the group name
        
head(self, message_spec=None, *, file=None)

  Process a HEAD command.  Argument:
          - message_spec: article number or message id
          - file: filename string or file object to store the headers in
          Returns:
          - resp: server response if successful
          - ArticleInfo: (article number, message id, list of header lines)
        
help(self, *, file=None)

  Process a HELP command. Argument:
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of strings returned by the server in response to the
                  HELP command
        
ihave(self, message_id, data)

  Process an IHAVE command.  Arguments:
          - message_id: message-id of the article
          - data: file containing the article
          Returns:
          - resp: server response if successful
          Note that if the server refuses the article an exception is raised.
last(self)

  Process a LAST command.  No arguments.  Return as for STAT.
list(self, group_pattern=None, *, file=None)

  Process a LIST or LIST ACTIVE command. Arguments:
          - group_pattern: a pattern indicating which groups to query
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of (group, last, first, flag) (strings)
        
login(self, user=None, password=None, usenetrc=True)
newgroups(self, date, *, file=None)

  Process a NEWGROUPS command.  Arguments:
          - date: a date or datetime object
          Return:
          - resp: server response if successful
          - list: list of newsgroup names
        
newnews(self, group, date, *, file=None)

  Process a NEWNEWS command.  Arguments:
          - group: group name or '*'
          - date: a date or datetime object
          Return:
          - resp: server response if successful
          - list: list of message ids
        
next(self)

  Process a NEXT command.  No arguments.  Return as for STAT.
over(self, message_spec, *, file=None)

  Process an OVER command.  If the command isn't supported, fall
          back to XOVER. Arguments:
          - message_spec:
              - either a message id, indicating the article to fetch
                information about
              - or a (start, end) tuple, indicating a range of article numbers;
                if end is None, information up to the newest message will be
                retrieved
              - or None, indicating the current article number must be used
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of dicts containing the response fields

          NOTE: the "message id" form isn't supported by XOVER
        
post(self, data)

  Process a POST command.  Arguments:
          - data: bytes object, iterable or file containing the article
          Returns:
          - resp: server response if successful
quit(self)

  Process a QUIT command and close the socket.  Returns:
          - resp: server response if successful
set_debuglevel(self, level)

  Set the debugging level.  Argument 'level' means:
          0: no debugging output (default)
          1: print commands and responses but not body text etc.
          2: also print raw lines read and sent before stripping CR/LF
slave(self)

  Process a SLAVE command.  Returns:
          - resp: server response if successful
        
starttls(self, context=None)

  Process a STARTTLS command. Arguments:
              - context: SSL context to use for the encrypted connection
            
stat(self, message_spec=None)

  Process a STAT command.  Argument:
          - message_spec: article number or message id (if not specified,
            the current article is selected)
          Returns:
          - resp: server response if successful
          - art_num: the article number
          - message_id: the message id
        
xhdr(self, hdr, str, *, file=None)

  Process an XHDR command (optional server extension).  Arguments:
          - hdr: the header type (e.g. 'subject')
          - str: an article nr, a message id, or a range nr1-nr2
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of (nr, value) strings
        
xover(self, start, end, *, file=None)

  Process an XOVER command (optional server extension) Arguments:
          - start: start of range
          - end: end of range
          - file: Filename string or file object to store the result in
          Returns:
          - resp: server response if successful
          - list: list of dicts containing the response fields
        
encoding = 'utf-8'
errors = 'surrogateescape'

Functions

decode_header

decode_header(header_str)

  Takes a unicode string representing a munged header value
      and decodes it as a (possibly non-ASCII) readable value.

Other members

NNTP_PORT = 119
NNTP_SSL_PORT = 563

Modules

collections

datetime

re

socket

ssl

sys