nntplib
index
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/nntplib.py
Module Docs

An NNTP client class based on RFC 977: Network News Transfer Protocol.
 
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', 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, 'r') # 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.

 
Modules
       
re
socket

 
Classes
       
NNTP
NNTPError(exceptions.Exception)
NNTPDataError
NNTPPermanentError
NNTPProtocolError
NNTPReplyError
NNTPTemporaryError

 
class NNTP
    # The class itself
 
  Methods defined here:
__init__(self, host, port=119, user=None, password=None, readermode=None, usenetrc=True)
Initialize an instance.  Arguments:
- host: hostname to connect to
- port: port to connect to (default the standard NNTP port)
- user: username to authenticate with
- password: password to use with username
- readermode: if true, send 'mode reader' command after
              connecting.
 
readermode is sometimes necessary if you are connecting to an
NNTP server on the local machine and intend to call
reader-specific commands, such as `group'.  If you get
unexpected NNTPPermanentErrors, you might need to set
readermode.
artcmd(self, line, file=None)
Internal: process a HEAD, BODY or ARTICLE command.
article(self, id)
Process an ARTICLE command.  Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the article
body(self, id, file=None)
Process a BODY command.  Argument:
- id: article number or message id
- file: Filename string or file object to store the article in
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the article's body or an empty list
        if file was used
date(self)
Process the DATE command. Arguments:
None
Returns:
resp: server response if successful
date: Date suitable for newnews/newgroups commands etc.
time: Time suitable for newnews/newgroups commands etc.
debug = set_debuglevel(self, level)
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.
getline(self)
Internal: return one line from the server, stripping CRLF.
Raise EOFError if the connection is closed.
getlongresp(self, file=None)
Internal: get a response plus following text from the server.
Raise various errors if the response indicates an error.
getresp(self)
Internal: get a response from the server.
Raise various errors if the response indicates an error.
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 (string)
- first: first article number (string)
- last: last article number (string)
- name: the group name
head(self, id)
Process a HEAD command.  Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the article's header
help(self, file=None)
Process a HELP command.  Returns:
- resp: server response if successful
- list: list of strings
ihave(self, id, f)
Process an IHAVE command.  Arguments:
- id: message-id of the article
- f:  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, file=None)
Process a LIST command.  Return:
- resp: server response if successful
- list: list of (group, last, first, flag) (strings)
longcmd(self, line, file=None)
Internal: send a command and get the response plus following text.
newgroups(self, date, time, file=None)
Process a NEWGROUPS command.  Arguments:
- date: string 'yymmdd' indicating the date
- time: string 'hhmmss' indicating the time
Return:
- resp: server response if successful
- list: list of newsgroup names
newnews(self, group, date, time, file=None)
Process a NEWNEWS command.  Arguments:
- group: group name or '*'
- date: string 'yymmdd' indicating the date
- time: string 'hhmmss' indicating the time
Return:
- resp: server response if successful
- list: list of message ids
next(self)
Process a NEXT command.  No arguments.  Return as for STAT.
post(self, f)
Process a POST command.  Arguments:
- f: file containing the article
Returns:
- resp: server response if successful
putcmd(self, line)
Internal: send one command to the server (through putline()).
putline(self, line)
Internal: send one line to the server, appending CRLF.
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
shortcmd(self, line)
Internal: send a command and get the response.
slave(self)
Process a SLAVE command.  Returns:
- resp: server response if successful
stat(self, id)
Process a STAT command.  Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr:   the article number
- id:   the message id
statcmd(self, line)
Internal: process a STAT, NEXT or LAST command.
statparse(self, resp)
Internal: parse the response of a STAT, NEXT or LAST command.
xgtitle(self, group, file=None)
Process an XGTITLE command (optional server extension) Arguments:
- group: group name wildcard (i.e. news.*)
Returns:
- resp: server response if successful
- list: list of (name,title) strings
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
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
Returns:
- resp: server response if successful
- list: list of (art-nr, subject, poster, date,
                 id, references, size, lines)
xpath(self, id)
Process an XPATH command (optional server extension) Arguments:
- id: Message id of article
Returns:
resp: server response if successful
path: directory path to article

 
class NNTPDataError(NNTPError)
    Error in response data
 
 
Method resolution order:
NNTPDataError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
class NNTPPermanentError(NNTPError)
    5xx errors
 
 
Method resolution order:
NNTPPermanentError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
class NNTPProtocolError(NNTPError)
    Response does not begin with [1-5]
 
 
Method resolution order:
NNTPProtocolError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
class NNTPReplyError(NNTPError)
    Unexpected [123]xx reply
 
 
Method resolution order:
NNTPReplyError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
class NNTPTemporaryError(NNTPError)
    4xx errors
 
 
Method resolution order:
NNTPTemporaryError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
error_data = class NNTPDataError(NNTPError)
    Error in response data
 
 
Method resolution order:
NNTPDataError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
error_perm = class NNTPPermanentError(NNTPError)
    5xx errors
 
 
Method resolution order:
NNTPPermanentError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
error_proto = class NNTPProtocolError(NNTPError)
    Response does not begin with [1-5]
 
 
Method resolution order:
NNTPProtocolError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
error_reply = class NNTPReplyError(NNTPError)
    Unexpected [123]xx reply
 
 
Method resolution order:
NNTPReplyError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
error_temp = class NNTPTemporaryError(NNTPError)
    4xx errors
 
 
Method resolution order:
NNTPTemporaryError
NNTPError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods inherited from NNTPError:
__init__(self, *args)

Data descriptors inherited from NNTPError:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
Data
        __all__ = ['NNTP', 'NNTPReplyError', 'NNTPTemporaryError', 'NNTPPermanentError', 'NNTPProtocolError', 'NNTPDataError', 'error_reply', 'error_temp', 'error_perm', 'error_proto', 'error_data']