2000-02-04 15:10:34 +00:00
|
|
|
|
"""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.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# RFC 977 by Brian Kantor and Phil Lapsley.
|
|
|
|
|
# xover, xgtitle, xpath, date methods by Kevan Heydon
|
1995-09-22 00:52:38 +00:00
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
# Imports
|
1997-10-22 21:00:49 +00:00
|
|
|
|
import re
|
1992-11-05 10:43:02 +00:00
|
|
|
|
import socket
|
|
|
|
|
import string
|
|
|
|
|
|
|
|
|
|
|
2000-02-10 20:25:53 +00:00
|
|
|
|
|
|
|
|
|
# Exceptions raised when an error or invalid response is received
|
|
|
|
|
class NNTPError(Exception):
|
|
|
|
|
"""Base class for all nntplib exceptions"""
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
|
apply(Exception.__init__, (self,)+args)
|
|
|
|
|
try:
|
|
|
|
|
self.response = args[0]
|
|
|
|
|
except IndexError:
|
|
|
|
|
self.response = 'No response given'
|
|
|
|
|
|
|
|
|
|
class NNTPReplyError(NNTPError):
|
|
|
|
|
"""Unexpected [123]xx reply"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class NNTPTemporaryError(NNTPError):
|
|
|
|
|
"""4xx errors"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class NNTPPermanentError(NNTPError):
|
|
|
|
|
"""5xx errors"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class NNTPProtocolError(NNTPError):
|
|
|
|
|
"""Response does not begin with [1-5]"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
class NNTPDataError(NNTPError):
|
|
|
|
|
"""Error in response data"""
|
|
|
|
|
pass
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
2000-02-10 20:25:53 +00:00
|
|
|
|
# for backwards compatibility
|
|
|
|
|
error_reply = NNTPReplyError
|
|
|
|
|
error_temp = NNTPTemporaryError
|
|
|
|
|
error_perm = NNTPPermanentError
|
|
|
|
|
error_proto = NNTPProtocolError
|
|
|
|
|
error_data = NNTPDataError
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
2000-02-10 20:25:53 +00:00
|
|
|
|
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
# Standard port used by NNTP servers
|
|
|
|
|
NNTP_PORT = 119
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Response numbers that are followed by additional text (e.g. article)
|
1995-09-22 00:52:38 +00:00
|
|
|
|
LONGRESP = ['100', '215', '220', '221', '222', '224', '230', '231', '282']
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)
|
|
|
|
|
CRLF = '\r\n'
|
|
|
|
|
|
|
|
|
|
|
2000-02-10 20:25:53 +00:00
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
# The class itself
|
|
|
|
|
class NNTP:
|
2000-02-10 20:25:53 +00:00
|
|
|
|
def __init__(self, host, port=NNTP_PORT, user=None, password=None,
|
|
|
|
|
readermode=None):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Initialize an instance. Arguments:
|
|
|
|
|
- host: hostname to connect to
|
2000-02-10 20:25:53 +00:00
|
|
|
|
- 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 comamnds, such as `group'. If you get
|
|
|
|
|
unexpected NNTPPermanentErrors, you might need to set
|
|
|
|
|
readermode.
|
|
|
|
|
"""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
self.host = host
|
|
|
|
|
self.port = port
|
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2000-03-28 21:45:46 +00:00
|
|
|
|
self.sock.connect((self.host, self.port))
|
1996-02-14 16:06:24 +00:00
|
|
|
|
self.file = self.sock.makefile('rb')
|
1992-11-05 10:43:02 +00:00
|
|
|
|
self.debugging = 0
|
|
|
|
|
self.welcome = self.getresp()
|
2000-02-10 20:25:53 +00:00
|
|
|
|
if readermode:
|
|
|
|
|
try:
|
|
|
|
|
self.welcome = self.shortcmd('mode reader')
|
|
|
|
|
except NNTPPermanentError:
|
|
|
|
|
# error 500, probably 'not implemented'
|
|
|
|
|
pass
|
1997-10-20 23:29:44 +00:00
|
|
|
|
if user:
|
1998-03-26 20:56:10 +00:00
|
|
|
|
resp = self.shortcmd('authinfo user '+user)
|
|
|
|
|
if resp[:3] == '381':
|
|
|
|
|
if not password:
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1998-03-26 20:56:10 +00:00
|
|
|
|
else:
|
|
|
|
|
resp = self.shortcmd(
|
|
|
|
|
'authinfo pass '+password)
|
|
|
|
|
if resp[:3] != '281':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPPermanentError(resp)
|
|
|
|
|
|
|
|
|
|
# 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
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
def getwelcome(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""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."""
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
if self.debugging: print '*welcome*', `self.welcome`
|
|
|
|
|
return self.welcome
|
|
|
|
|
|
1995-03-30 10:42:34 +00:00
|
|
|
|
def set_debuglevel(self, level):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""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"""
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
self.debugging = level
|
1995-03-30 10:42:34 +00:00
|
|
|
|
debug = set_debuglevel
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
def putline(self, line):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: send one line to the server, appending CRLF."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
line = line + CRLF
|
|
|
|
|
if self.debugging > 1: print '*put*', `line`
|
|
|
|
|
self.sock.send(line)
|
|
|
|
|
|
|
|
|
|
def putcmd(self, line):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: send one command to the server (through putline())."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
if self.debugging: print '*cmd*', `line`
|
|
|
|
|
self.putline(line)
|
|
|
|
|
|
|
|
|
|
def getline(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: return one line from the server, stripping CRLF.
|
|
|
|
|
Raise EOFError if the connection is closed."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
line = self.file.readline()
|
|
|
|
|
if self.debugging > 1:
|
|
|
|
|
print '*get*', `line`
|
|
|
|
|
if not line: raise EOFError
|
|
|
|
|
if line[-2:] == CRLF: line = line[:-2]
|
|
|
|
|
elif line[-1:] in CRLF: line = line[:-1]
|
|
|
|
|
return line
|
|
|
|
|
|
|
|
|
|
def getresp(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: get a response from the server.
|
|
|
|
|
Raise various errors if the response indicates an error."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp = self.getline()
|
|
|
|
|
if self.debugging: print '*resp*', `resp`
|
|
|
|
|
c = resp[:1]
|
|
|
|
|
if c == '4':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPTemporaryError(resp)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
if c == '5':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPPermanentError(resp)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
if c not in '123':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPProtocolError(resp)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
def getlongresp(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: get a response plus following text from the server.
|
|
|
|
|
Raise various errors if the response indicates an error."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp = self.getresp()
|
|
|
|
|
if resp[:3] not in LONGRESP:
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
list = []
|
|
|
|
|
while 1:
|
|
|
|
|
line = self.getline()
|
|
|
|
|
if line == '.':
|
|
|
|
|
break
|
1997-08-26 23:26:18 +00:00
|
|
|
|
if line[:2] == '..':
|
|
|
|
|
line = line[1:]
|
1992-11-05 10:43:02 +00:00
|
|
|
|
list.append(line)
|
|
|
|
|
return resp, list
|
|
|
|
|
|
|
|
|
|
def shortcmd(self, line):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: send a command and get the response."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
self.putcmd(line)
|
|
|
|
|
return self.getresp()
|
|
|
|
|
|
|
|
|
|
def longcmd(self, line):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: send a command and get the response plus following text."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
self.putcmd(line)
|
|
|
|
|
return self.getlongresp()
|
|
|
|
|
|
|
|
|
|
def newgroups(self, date, time):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a NEWGROUPS command. Arguments:
|
|
|
|
|
- date: string 'yymmdd' indicating the date
|
|
|
|
|
- time: string 'hhmmss' indicating the time
|
|
|
|
|
Return:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- list: list of newsgroup names"""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
2000-02-04 15:10:34 +00:00
|
|
|
|
return self.longcmd('NEWGROUPS ' + date + ' ' + time)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
def newnews(self, group, date, time):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a NEWNEWS command. Arguments:
|
|
|
|
|
- group: group name or '*'
|
|
|
|
|
- date: string 'yymmdd' indicating the date
|
|
|
|
|
- time: string 'hhmmss' indicating the time
|
|
|
|
|
Return:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- list: list of article ids"""
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time
|
|
|
|
|
return self.longcmd(cmd)
|
|
|
|
|
|
|
|
|
|
def list(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a LIST command. Return:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- list: list of (group, last, first, flag) (strings)"""
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp, list = self.longcmd('LIST')
|
|
|
|
|
for i in range(len(list)):
|
1995-01-10 10:35:55 +00:00
|
|
|
|
# Parse lines into "group last first flag"
|
1997-03-14 04:18:20 +00:00
|
|
|
|
list[i] = tuple(string.split(list[i]))
|
1992-11-05 10:43:02 +00:00
|
|
|
|
return resp, list
|
|
|
|
|
|
|
|
|
|
def group(self, name):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a GROUP command. Argument:
|
|
|
|
|
- group: the group name
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- count: number of articles (string)
|
|
|
|
|
- first: first article number (string)
|
|
|
|
|
- last: last article number (string)
|
|
|
|
|
- name: the group name"""
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp = self.shortcmd('GROUP ' + name)
|
|
|
|
|
if resp[:3] <> '211':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
words = string.split(resp)
|
|
|
|
|
count = first = last = 0
|
|
|
|
|
n = len(words)
|
|
|
|
|
if n > 1:
|
|
|
|
|
count = words[1]
|
|
|
|
|
if n > 2:
|
|
|
|
|
first = words[2]
|
|
|
|
|
if n > 3:
|
|
|
|
|
last = words[3]
|
|
|
|
|
if n > 4:
|
|
|
|
|
name = string.lower(words[4])
|
|
|
|
|
return resp, count, first, last, name
|
|
|
|
|
|
|
|
|
|
def help(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a HELP command. Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- list: list of strings"""
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
return self.longcmd('HELP')
|
|
|
|
|
|
|
|
|
|
def statparse(self, resp):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: parse the response of a STAT, NEXT or LAST command."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
if resp[:2] <> '22':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
words = string.split(resp)
|
|
|
|
|
nr = 0
|
|
|
|
|
id = ''
|
|
|
|
|
n = len(words)
|
|
|
|
|
if n > 1:
|
|
|
|
|
nr = words[1]
|
|
|
|
|
if n > 2:
|
1998-12-21 18:51:23 +00:00
|
|
|
|
id = words[2]
|
1992-11-05 10:43:02 +00:00
|
|
|
|
return resp, nr, id
|
|
|
|
|
|
|
|
|
|
def statcmd(self, line):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: process a STAT, NEXT or LAST command."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp = self.shortcmd(line)
|
|
|
|
|
return self.statparse(resp)
|
|
|
|
|
|
|
|
|
|
def stat(self, id):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a STAT command. Argument:
|
|
|
|
|
- id: article number or message id
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- nr: the article number
|
|
|
|
|
- id: the article id"""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
2000-02-04 15:10:34 +00:00
|
|
|
|
return self.statcmd('STAT ' + id)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
def next(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a NEXT command. No arguments. Return as for STAT."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
return self.statcmd('NEXT')
|
|
|
|
|
|
|
|
|
|
def last(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a LAST command. No arguments. Return as for STAT."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
return self.statcmd('LAST')
|
|
|
|
|
|
|
|
|
|
def artcmd(self, line):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Internal: process a HEAD, BODY or ARTICLE command."""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp, list = self.longcmd(line)
|
|
|
|
|
resp, nr, id = self.statparse(resp)
|
|
|
|
|
return resp, nr, id, list
|
|
|
|
|
|
|
|
|
|
def head(self, id):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a HEAD command. Argument:
|
|
|
|
|
- id: article number or message id
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- nr: article number
|
|
|
|
|
- id: message id
|
|
|
|
|
- list: the lines of the article's header"""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
2000-02-04 15:10:34 +00:00
|
|
|
|
return self.artcmd('HEAD ' + id)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
def body(self, id):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a BODY command. Argument:
|
|
|
|
|
- id: article number or message id
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- nr: article number
|
|
|
|
|
- id: message id
|
|
|
|
|
- list: the lines of the article's body"""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
2000-02-04 15:10:34 +00:00
|
|
|
|
return self.artcmd('BODY ' + id)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
def article(self, id):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process an ARTICLE command. Argument:
|
|
|
|
|
- id: article number or message id
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- nr: article number
|
|
|
|
|
- id: message id
|
|
|
|
|
- list: the lines of the article"""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
2000-02-04 15:10:34 +00:00
|
|
|
|
return self.artcmd('ARTICLE ' + id)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
def slave(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a SLAVE command. Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful"""
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
2000-02-04 15:10:34 +00:00
|
|
|
|
return self.shortcmd('SLAVE')
|
1992-11-05 10:43:02 +00:00
|
|
|
|
|
|
|
|
|
def xhdr(self, hdr, str):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""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:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- list: list of (nr, value) strings"""
|
|
|
|
|
|
1997-10-22 21:00:49 +00:00
|
|
|
|
pat = re.compile('^([0-9]+) ?(.*)\n?')
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str)
|
|
|
|
|
for i in range(len(lines)):
|
|
|
|
|
line = lines[i]
|
1997-10-22 21:00:49 +00:00
|
|
|
|
m = pat.match(line)
|
|
|
|
|
if m:
|
|
|
|
|
lines[i] = m.group(1, 2)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
return resp, lines
|
|
|
|
|
|
1995-09-22 00:52:38 +00:00
|
|
|
|
def xover(self,start,end):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process an XOVER command (optional server extension) Arguments:
|
|
|
|
|
- start: start of range
|
|
|
|
|
- end: end of range
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- list: list of (art-nr, subject, poster, date,
|
|
|
|
|
id, references, size, lines)"""
|
|
|
|
|
|
1995-09-22 00:52:38 +00:00
|
|
|
|
resp, lines = self.longcmd('XOVER ' + start + '-' + end)
|
|
|
|
|
xover_lines = []
|
|
|
|
|
for line in lines:
|
|
|
|
|
elem = string.splitfields(line,"\t")
|
|
|
|
|
try:
|
1997-07-17 15:21:52 +00:00
|
|
|
|
xover_lines.append((elem[0],
|
|
|
|
|
elem[1],
|
|
|
|
|
elem[2],
|
|
|
|
|
elem[3],
|
|
|
|
|
elem[4],
|
|
|
|
|
string.split(elem[5]),
|
|
|
|
|
elem[6],
|
|
|
|
|
elem[7]))
|
1995-09-22 00:52:38 +00:00
|
|
|
|
except IndexError:
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPDataError(line)
|
1995-09-22 00:52:38 +00:00
|
|
|
|
return resp,xover_lines
|
|
|
|
|
|
|
|
|
|
def xgtitle(self, group):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process an XGTITLE command (optional server extension) Arguments:
|
|
|
|
|
- group: group name wildcard (i.e. news.*)
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
- list: list of (name,title) strings"""
|
|
|
|
|
|
1997-10-22 21:00:49 +00:00
|
|
|
|
line_pat = re.compile("^([^ \t]+)[ \t]+(.*)$")
|
1995-09-22 00:52:38 +00:00
|
|
|
|
resp, raw_lines = self.longcmd('XGTITLE ' + group)
|
|
|
|
|
lines = []
|
|
|
|
|
for raw_line in raw_lines:
|
1997-10-22 21:00:49 +00:00
|
|
|
|
match = line_pat.search(string.strip(raw_line))
|
|
|
|
|
if match:
|
|
|
|
|
lines.append(match.group(1, 2))
|
1995-09-22 00:52:38 +00:00
|
|
|
|
return resp, lines
|
|
|
|
|
|
|
|
|
|
def xpath(self,id):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process an XPATH command (optional server extension) Arguments:
|
|
|
|
|
- id: Message id of article
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
path: directory path to article"""
|
|
|
|
|
|
1995-09-22 00:52:38 +00:00
|
|
|
|
resp = self.shortcmd("XPATH " + id)
|
|
|
|
|
if resp[:3] <> '223':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1995-09-22 00:52:38 +00:00
|
|
|
|
try:
|
|
|
|
|
[resp_num, path] = string.split(resp)
|
|
|
|
|
except ValueError:
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1995-09-22 00:52:38 +00:00
|
|
|
|
else:
|
|
|
|
|
return resp, path
|
|
|
|
|
|
|
|
|
|
def date (self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process the DATE command. Arguments:
|
|
|
|
|
None
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
date: Date suitable for newnews/newgroups commands etc.
|
|
|
|
|
time: Time suitable for newnews/newgroups commands etc."""
|
|
|
|
|
|
1995-09-22 00:52:38 +00:00
|
|
|
|
resp = self.shortcmd("DATE")
|
|
|
|
|
if resp[:3] <> '111':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1995-09-22 00:52:38 +00:00
|
|
|
|
elem = string.split(resp)
|
|
|
|
|
if len(elem) != 2:
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPDataError(resp)
|
1995-09-22 00:52:38 +00:00
|
|
|
|
date = elem[1][2:8]
|
|
|
|
|
time = elem[1][-6:]
|
|
|
|
|
if len(date) != 6 or len(time) != 6:
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPDataError(resp)
|
1995-09-22 00:52:38 +00:00
|
|
|
|
return resp, date, time
|
|
|
|
|
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
def post(self, f):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a POST command. Arguments:
|
|
|
|
|
- f: file containing the article
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful"""
|
2000-02-04 15:10:34 +00:00
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp = self.shortcmd('POST')
|
|
|
|
|
# Raises error_??? if posting is not allowed
|
|
|
|
|
if resp[0] <> '3':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
while 1:
|
|
|
|
|
line = f.readline()
|
|
|
|
|
if not line:
|
|
|
|
|
break
|
|
|
|
|
if line[-1] == '\n':
|
|
|
|
|
line = line[:-1]
|
1997-08-26 23:26:18 +00:00
|
|
|
|
if line[:1] == '.':
|
|
|
|
|
line = '.' + line
|
1992-11-05 10:43:02 +00:00
|
|
|
|
self.putline(line)
|
|
|
|
|
self.putline('.')
|
|
|
|
|
return self.getresp()
|
|
|
|
|
|
|
|
|
|
def ihave(self, id, f):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process an IHAVE command. Arguments:
|
|
|
|
|
- id: message-id of the article
|
|
|
|
|
- f: file containing the article
|
|
|
|
|
Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful
|
2000-02-04 15:10:34 +00:00
|
|
|
|
Note that if the server refuses the article an exception is raised."""
|
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp = self.shortcmd('IHAVE ' + id)
|
1992-11-26 09:17:19 +00:00
|
|
|
|
# Raises error_??? if the server already has it
|
1992-11-05 10:43:02 +00:00
|
|
|
|
if resp[0] <> '3':
|
2000-02-10 20:25:53 +00:00
|
|
|
|
raise NNTPReplyError(resp)
|
1992-11-05 10:43:02 +00:00
|
|
|
|
while 1:
|
|
|
|
|
line = f.readline()
|
|
|
|
|
if not line:
|
|
|
|
|
break
|
|
|
|
|
if line[-1] == '\n':
|
|
|
|
|
line = line[:-1]
|
1997-08-26 23:26:18 +00:00
|
|
|
|
if line[:1] == '.':
|
|
|
|
|
line = '.' + line
|
1992-11-05 10:43:02 +00:00
|
|
|
|
self.putline(line)
|
|
|
|
|
self.putline('.')
|
|
|
|
|
return self.getresp()
|
|
|
|
|
|
|
|
|
|
def quit(self):
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Process a QUIT command and close the socket. Returns:
|
2000-07-16 12:04:32 +00:00
|
|
|
|
- resp: server response if successful"""
|
2000-02-04 15:10:34 +00:00
|
|
|
|
|
1992-11-05 10:43:02 +00:00
|
|
|
|
resp = self.shortcmd('QUIT')
|
|
|
|
|
self.file.close()
|
|
|
|
|
self.sock.close()
|
|
|
|
|
del self.file, self.sock
|
|
|
|
|
return resp
|
1997-08-26 23:26:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _test():
|
2000-02-04 15:10:34 +00:00
|
|
|
|
"""Minimal test function."""
|
2000-02-10 20:25:53 +00:00
|
|
|
|
s = NNTP('news', readermode='reader')
|
1997-08-26 23:26:18 +00:00
|
|
|
|
resp, count, first, last, name = s.group('comp.lang.python')
|
|
|
|
|
print resp
|
|
|
|
|
print 'Group', name, 'has', count, 'articles, range', first, 'to', last
|
|
|
|
|
resp, subs = s.xhdr('subject', first + '-' + last)
|
|
|
|
|
print resp
|
|
|
|
|
for item in subs:
|
|
|
|
|
print "%7s %s" % item
|
|
|
|
|
resp = s.quit()
|
|
|
|
|
print resp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Run the test when run as a script
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
_test()
|