+++ /dev/null
-"""An NNTP client class based on RFC 977: Network News Transfer Protocol.\r
-\r
-Example:\r
-\r
->>> from nntplib import NNTP\r
->>> s = NNTP('news')\r
->>> resp, count, first, last, name = s.group('comp.lang.python')\r
->>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last\r
-Group comp.lang.python has 51 articles, range 5770 to 5821\r
->>> resp, subs = s.xhdr('subject', first + '-' + last)\r
->>> resp = s.quit()\r
->>>\r
-\r
-Here 'resp' is the server response line.\r
-Error responses are turned into exceptions.\r
-\r
-To post an article from a file:\r
->>> f = open(filename, 'r') # file containing article, including header\r
->>> resp = s.post(f)\r
->>>\r
-\r
-For descriptions of all methods, read the comments in the code below.\r
-Note that all arguments and return values representing article numbers\r
-are strings, not numbers, since they are rarely used for calculations.\r
-"""\r
-\r
-# RFC 977 by Brian Kantor and Phil Lapsley.\r
-# xover, xgtitle, xpath, date methods by Kevan Heydon\r
-\r
-\r
-# Imports\r
-import re\r
-import socket\r
-\r
-__all__ = ["NNTP","NNTPReplyError","NNTPTemporaryError",\r
- "NNTPPermanentError","NNTPProtocolError","NNTPDataError",\r
- "error_reply","error_temp","error_perm","error_proto",\r
- "error_data",]\r
-\r
-# Exceptions raised when an error or invalid response is received\r
-class NNTPError(Exception):\r
- """Base class for all nntplib exceptions"""\r
- def __init__(self, *args):\r
- Exception.__init__(self, *args)\r
- try:\r
- self.response = args[0]\r
- except IndexError:\r
- self.response = 'No response given'\r
-\r
-class NNTPReplyError(NNTPError):\r
- """Unexpected [123]xx reply"""\r
- pass\r
-\r
-class NNTPTemporaryError(NNTPError):\r
- """4xx errors"""\r
- pass\r
-\r
-class NNTPPermanentError(NNTPError):\r
- """5xx errors"""\r
- pass\r
-\r
-class NNTPProtocolError(NNTPError):\r
- """Response does not begin with [1-5]"""\r
- pass\r
-\r
-class NNTPDataError(NNTPError):\r
- """Error in response data"""\r
- pass\r
-\r
-# for backwards compatibility\r
-error_reply = NNTPReplyError\r
-error_temp = NNTPTemporaryError\r
-error_perm = NNTPPermanentError\r
-error_proto = NNTPProtocolError\r
-error_data = NNTPDataError\r
-\r
-\r
-\r
-# Standard port used by NNTP servers\r
-NNTP_PORT = 119\r
-\r
-\r
-# Response numbers that are followed by additional text (e.g. article)\r
-LONGRESP = ['100', '215', '220', '221', '222', '224', '230', '231', '282']\r
-\r
-\r
-# Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)\r
-CRLF = '\r\n'\r
-\r
-\r
-\r
-# The class itself\r
-class NNTP:\r
- def __init__(self, host, port=NNTP_PORT, user=None, password=None,\r
- readermode=None, usenetrc=True):\r
- """Initialize an instance. Arguments:\r
- - host: hostname to connect to\r
- - port: port to connect to (default the standard NNTP port)\r
- - user: username to authenticate with\r
- - password: password to use with username\r
- - readermode: if true, send 'mode reader' command after\r
- connecting.\r
-\r
- readermode is sometimes necessary if you are connecting to an\r
- NNTP server on the local machine and intend to call\r
- reader-specific commands, such as `group'. If you get\r
- unexpected NNTPPermanentErrors, you might need to set\r
- readermode.\r
- """\r
- self.host = host\r
- self.port = port\r
- self.sock = socket.create_connection((host, port))\r
- self.file = self.sock.makefile('rb')\r
- self.debugging = 0\r
- self.welcome = self.getresp()\r
-\r
- # 'mode reader' is sometimes necessary to enable 'reader' mode.\r
- # However, the order in which 'mode reader' and 'authinfo' need to\r
- # arrive differs between some NNTP servers. Try to send\r
- # 'mode reader', and if it fails with an authorization failed\r
- # error, try again after sending authinfo.\r
- readermode_afterauth = 0\r
- if readermode:\r
- try:\r
- self.welcome = self.shortcmd('mode reader')\r
- except NNTPPermanentError:\r
- # error 500, probably 'not implemented'\r
- pass\r
- except NNTPTemporaryError, e:\r
- if user and e.response[:3] == '480':\r
- # Need authorization before 'mode reader'\r
- readermode_afterauth = 1\r
- else:\r
- raise\r
- # If no login/password was specified, try to get them from ~/.netrc\r
- # Presume that if .netc has an entry, NNRP authentication is required.\r
- try:\r
- if usenetrc and not user:\r
- import netrc\r
- credentials = netrc.netrc()\r
- auth = credentials.authenticators(host)\r
- if auth:\r
- user = auth[0]\r
- password = auth[2]\r
- except IOError:\r
- pass\r
- # Perform NNRP authentication if needed.\r
- if user:\r
- resp = self.shortcmd('authinfo user '+user)\r
- if resp[:3] == '381':\r
- if not password:\r
- raise NNTPReplyError(resp)\r
- else:\r
- resp = self.shortcmd(\r
- 'authinfo pass '+password)\r
- if resp[:3] != '281':\r
- raise NNTPPermanentError(resp)\r
- if readermode_afterauth:\r
- try:\r
- self.welcome = self.shortcmd('mode reader')\r
- except NNTPPermanentError:\r
- # error 500, probably 'not implemented'\r
- pass\r
-\r
-\r
- # Get the welcome message from the server\r
- # (this is read and squirreled away by __init__()).\r
- # If the response code is 200, posting is allowed;\r
- # if it 201, posting is not allowed\r
-\r
- def getwelcome(self):\r
- """Get the welcome message from the server\r
- (this is read and squirreled away by __init__()).\r
- If the response code is 200, posting is allowed;\r
- if it 201, posting is not allowed."""\r
-\r
- if self.debugging: print '*welcome*', repr(self.welcome)\r
- return self.welcome\r
-\r
- def set_debuglevel(self, level):\r
- """Set the debugging level. Argument 'level' means:\r
- 0: no debugging output (default)\r
- 1: print commands and responses but not body text etc.\r
- 2: also print raw lines read and sent before stripping CR/LF"""\r
-\r
- self.debugging = level\r
- debug = set_debuglevel\r
-\r
- def putline(self, line):\r
- """Internal: send one line to the server, appending CRLF."""\r
- line = line + CRLF\r
- if self.debugging > 1: print '*put*', repr(line)\r
- self.sock.sendall(line)\r
-\r
- def putcmd(self, line):\r
- """Internal: send one command to the server (through putline())."""\r
- if self.debugging: print '*cmd*', repr(line)\r
- self.putline(line)\r
-\r
- def getline(self):\r
- """Internal: return one line from the server, stripping CRLF.\r
- Raise EOFError if the connection is closed."""\r
- line = self.file.readline()\r
- if self.debugging > 1:\r
- print '*get*', repr(line)\r
- if not line: raise EOFError\r
- if line[-2:] == CRLF: line = line[:-2]\r
- elif line[-1:] in CRLF: line = line[:-1]\r
- return line\r
-\r
- def getresp(self):\r
- """Internal: get a response from the server.\r
- Raise various errors if the response indicates an error."""\r
- resp = self.getline()\r
- if self.debugging: print '*resp*', repr(resp)\r
- c = resp[:1]\r
- if c == '4':\r
- raise NNTPTemporaryError(resp)\r
- if c == '5':\r
- raise NNTPPermanentError(resp)\r
- if c not in '123':\r
- raise NNTPProtocolError(resp)\r
- return resp\r
-\r
- def getlongresp(self, file=None):\r
- """Internal: get a response plus following text from the server.\r
- Raise various errors if the response indicates an error."""\r
-\r
- openedFile = None\r
- try:\r
- # If a string was passed then open a file with that name\r
- if isinstance(file, str):\r
- openedFile = file = open(file, "w")\r
-\r
- resp = self.getresp()\r
- if resp[:3] not in LONGRESP:\r
- raise NNTPReplyError(resp)\r
- list = []\r
- while 1:\r
- line = self.getline()\r
- if line == '.':\r
- break\r
- if line[:2] == '..':\r
- line = line[1:]\r
- if file:\r
- file.write(line + "\n")\r
- else:\r
- list.append(line)\r
- finally:\r
- # If this method created the file, then it must close it\r
- if openedFile:\r
- openedFile.close()\r
-\r
- return resp, list\r
-\r
- def shortcmd(self, line):\r
- """Internal: send a command and get the response."""\r
- self.putcmd(line)\r
- return self.getresp()\r
-\r
- def longcmd(self, line, file=None):\r
- """Internal: send a command and get the response plus following text."""\r
- self.putcmd(line)\r
- return self.getlongresp(file)\r
-\r
- def newgroups(self, date, time, file=None):\r
- """Process a NEWGROUPS command. Arguments:\r
- - date: string 'yymmdd' indicating the date\r
- - time: string 'hhmmss' indicating the time\r
- Return:\r
- - resp: server response if successful\r
- - list: list of newsgroup names"""\r
-\r
- return self.longcmd('NEWGROUPS ' + date + ' ' + time, file)\r
-\r
- def newnews(self, group, date, time, file=None):\r
- """Process a NEWNEWS command. Arguments:\r
- - group: group name or '*'\r
- - date: string 'yymmdd' indicating the date\r
- - time: string 'hhmmss' indicating the time\r
- Return:\r
- - resp: server response if successful\r
- - list: list of message ids"""\r
-\r
- cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time\r
- return self.longcmd(cmd, file)\r
-\r
- def list(self, file=None):\r
- """Process a LIST command. Return:\r
- - resp: server response if successful\r
- - list: list of (group, last, first, flag) (strings)"""\r
-\r
- resp, list = self.longcmd('LIST', file)\r
- for i in range(len(list)):\r
- # Parse lines into "group last first flag"\r
- list[i] = tuple(list[i].split())\r
- return resp, list\r
-\r
- def description(self, group):\r
-\r
- """Get a description for a single group. If more than one\r
- group matches ('group' is a pattern), return the first. If no\r
- group matches, return an empty string.\r
-\r
- This elides the response code from the server, since it can\r
- only be '215' or '285' (for xgtitle) anyway. If the response\r
- code is needed, use the 'descriptions' method.\r
-\r
- NOTE: This neither checks for a wildcard in 'group' nor does\r
- it check whether the group actually exists."""\r
-\r
- resp, lines = self.descriptions(group)\r
- if len(lines) == 0:\r
- return ""\r
- else:\r
- return lines[0][1]\r
-\r
- def descriptions(self, group_pattern):\r
- """Get descriptions for a range of groups."""\r
- line_pat = re.compile("^(?P<group>[^ \t]+)[ \t]+(.*)$")\r
- # Try the more std (acc. to RFC2980) LIST NEWSGROUPS first\r
- resp, raw_lines = self.longcmd('LIST NEWSGROUPS ' + group_pattern)\r
- if resp[:3] != "215":\r
- # Now the deprecated XGTITLE. This either raises an error\r
- # or succeeds with the same output structure as LIST\r
- # NEWSGROUPS.\r
- resp, raw_lines = self.longcmd('XGTITLE ' + group_pattern)\r
- lines = []\r
- for raw_line in raw_lines:\r
- match = line_pat.search(raw_line.strip())\r
- if match:\r
- lines.append(match.group(1, 2))\r
- return resp, lines\r
-\r
- def group(self, name):\r
- """Process a GROUP command. Argument:\r
- - group: the group name\r
- Returns:\r
- - resp: server response if successful\r
- - count: number of articles (string)\r
- - first: first article number (string)\r
- - last: last article number (string)\r
- - name: the group name"""\r
-\r
- resp = self.shortcmd('GROUP ' + name)\r
- if resp[:3] != '211':\r
- raise NNTPReplyError(resp)\r
- words = resp.split()\r
- count = first = last = 0\r
- n = len(words)\r
- if n > 1:\r
- count = words[1]\r
- if n > 2:\r
- first = words[2]\r
- if n > 3:\r
- last = words[3]\r
- if n > 4:\r
- name = words[4].lower()\r
- return resp, count, first, last, name\r
-\r
- def help(self, file=None):\r
- """Process a HELP command. Returns:\r
- - resp: server response if successful\r
- - list: list of strings"""\r
-\r
- return self.longcmd('HELP',file)\r
-\r
- def statparse(self, resp):\r
- """Internal: parse the response of a STAT, NEXT or LAST command."""\r
- if resp[:2] != '22':\r
- raise NNTPReplyError(resp)\r
- words = resp.split()\r
- nr = 0\r
- id = ''\r
- n = len(words)\r
- if n > 1:\r
- nr = words[1]\r
- if n > 2:\r
- id = words[2]\r
- return resp, nr, id\r
-\r
- def statcmd(self, line):\r
- """Internal: process a STAT, NEXT or LAST command."""\r
- resp = self.shortcmd(line)\r
- return self.statparse(resp)\r
-\r
- def stat(self, id):\r
- """Process a STAT command. Argument:\r
- - id: article number or message id\r
- Returns:\r
- - resp: server response if successful\r
- - nr: the article number\r
- - id: the message id"""\r
-\r
- return self.statcmd('STAT ' + id)\r
-\r
- def next(self):\r
- """Process a NEXT command. No arguments. Return as for STAT."""\r
- return self.statcmd('NEXT')\r
-\r
- def last(self):\r
- """Process a LAST command. No arguments. Return as for STAT."""\r
- return self.statcmd('LAST')\r
-\r
- def artcmd(self, line, file=None):\r
- """Internal: process a HEAD, BODY or ARTICLE command."""\r
- resp, list = self.longcmd(line, file)\r
- resp, nr, id = self.statparse(resp)\r
- return resp, nr, id, list\r
-\r
- def head(self, id):\r
- """Process a HEAD command. Argument:\r
- - id: article number or message id\r
- Returns:\r
- - resp: server response if successful\r
- - nr: article number\r
- - id: message id\r
- - list: the lines of the article's header"""\r
-\r
- return self.artcmd('HEAD ' + id)\r
-\r
- def body(self, id, file=None):\r
- """Process a BODY command. Argument:\r
- - id: article number or message id\r
- - file: Filename string or file object to store the article in\r
- Returns:\r
- - resp: server response if successful\r
- - nr: article number\r
- - id: message id\r
- - list: the lines of the article's body or an empty list\r
- if file was used"""\r
-\r
- return self.artcmd('BODY ' + id, file)\r
-\r
- def article(self, id):\r
- """Process an ARTICLE command. Argument:\r
- - id: article number or message id\r
- Returns:\r
- - resp: server response if successful\r
- - nr: article number\r
- - id: message id\r
- - list: the lines of the article"""\r
-\r
- return self.artcmd('ARTICLE ' + id)\r
-\r
- def slave(self):\r
- """Process a SLAVE command. Returns:\r
- - resp: server response if successful"""\r
-\r
- return self.shortcmd('SLAVE')\r
-\r
- def xhdr(self, hdr, str, file=None):\r
- """Process an XHDR command (optional server extension). Arguments:\r
- - hdr: the header type (e.g. 'subject')\r
- - str: an article nr, a message id, or a range nr1-nr2\r
- Returns:\r
- - resp: server response if successful\r
- - list: list of (nr, value) strings"""\r
-\r
- pat = re.compile('^([0-9]+) ?(.*)\n?')\r
- resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str, file)\r
- for i in range(len(lines)):\r
- line = lines[i]\r
- m = pat.match(line)\r
- if m:\r
- lines[i] = m.group(1, 2)\r
- return resp, lines\r
-\r
- def xover(self, start, end, file=None):\r
- """Process an XOVER command (optional server extension) Arguments:\r
- - start: start of range\r
- - end: end of range\r
- Returns:\r
- - resp: server response if successful\r
- - list: list of (art-nr, subject, poster, date,\r
- id, references, size, lines)"""\r
-\r
- resp, lines = self.longcmd('XOVER ' + start + '-' + end, file)\r
- xover_lines = []\r
- for line in lines:\r
- elem = line.split("\t")\r
- try:\r
- xover_lines.append((elem[0],\r
- elem[1],\r
- elem[2],\r
- elem[3],\r
- elem[4],\r
- elem[5].split(),\r
- elem[6],\r
- elem[7]))\r
- except IndexError:\r
- raise NNTPDataError(line)\r
- return resp,xover_lines\r
-\r
- def xgtitle(self, group, file=None):\r
- """Process an XGTITLE command (optional server extension) Arguments:\r
- - group: group name wildcard (i.e. news.*)\r
- Returns:\r
- - resp: server response if successful\r
- - list: list of (name,title) strings"""\r
-\r
- line_pat = re.compile("^([^ \t]+)[ \t]+(.*)$")\r
- resp, raw_lines = self.longcmd('XGTITLE ' + group, file)\r
- lines = []\r
- for raw_line in raw_lines:\r
- match = line_pat.search(raw_line.strip())\r
- if match:\r
- lines.append(match.group(1, 2))\r
- return resp, lines\r
-\r
- def xpath(self,id):\r
- """Process an XPATH command (optional server extension) Arguments:\r
- - id: Message id of article\r
- Returns:\r
- resp: server response if successful\r
- path: directory path to article"""\r
-\r
- resp = self.shortcmd("XPATH " + id)\r
- if resp[:3] != '223':\r
- raise NNTPReplyError(resp)\r
- try:\r
- [resp_num, path] = resp.split()\r
- except ValueError:\r
- raise NNTPReplyError(resp)\r
- else:\r
- return resp, path\r
-\r
- def date (self):\r
- """Process the DATE command. Arguments:\r
- None\r
- Returns:\r
- resp: server response if successful\r
- date: Date suitable for newnews/newgroups commands etc.\r
- time: Time suitable for newnews/newgroups commands etc."""\r
-\r
- resp = self.shortcmd("DATE")\r
- if resp[:3] != '111':\r
- raise NNTPReplyError(resp)\r
- elem = resp.split()\r
- if len(elem) != 2:\r
- raise NNTPDataError(resp)\r
- date = elem[1][2:8]\r
- time = elem[1][-6:]\r
- if len(date) != 6 or len(time) != 6:\r
- raise NNTPDataError(resp)\r
- return resp, date, time\r
-\r
-\r
- def post(self, f):\r
- """Process a POST command. Arguments:\r
- - f: file containing the article\r
- Returns:\r
- - resp: server response if successful"""\r
-\r
- resp = self.shortcmd('POST')\r
- # Raises error_??? if posting is not allowed\r
- if resp[0] != '3':\r
- raise NNTPReplyError(resp)\r
- while 1:\r
- line = f.readline()\r
- if not line:\r
- break\r
- if line[-1] == '\n':\r
- line = line[:-1]\r
- if line[:1] == '.':\r
- line = '.' + line\r
- self.putline(line)\r
- self.putline('.')\r
- return self.getresp()\r
-\r
- def ihave(self, id, f):\r
- """Process an IHAVE command. Arguments:\r
- - id: message-id of the article\r
- - f: file containing the article\r
- Returns:\r
- - resp: server response if successful\r
- Note that if the server refuses the article an exception is raised."""\r
-\r
- resp = self.shortcmd('IHAVE ' + id)\r
- # Raises error_??? if the server already has it\r
- if resp[0] != '3':\r
- raise NNTPReplyError(resp)\r
- while 1:\r
- line = f.readline()\r
- if not line:\r
- break\r
- if line[-1] == '\n':\r
- line = line[:-1]\r
- if line[:1] == '.':\r
- line = '.' + line\r
- self.putline(line)\r
- self.putline('.')\r
- return self.getresp()\r
-\r
- def quit(self):\r
- """Process a QUIT command and close the socket. Returns:\r
- - resp: server response if successful"""\r
-\r
- resp = self.shortcmd('QUIT')\r
- self.file.close()\r
- self.sock.close()\r
- del self.file, self.sock\r
- return resp\r
-\r
-\r
-# Test retrieval when run as a script.\r
-# Assumption: if there's a local news server, it's called 'news'.\r
-# Assumption: if user queries a remote news server, it's named\r
-# in the environment variable NNTPSERVER (used by slrn and kin)\r
-# and we want readermode off.\r
-if __name__ == '__main__':\r
- import os\r
- newshost = 'news' and os.environ["NNTPSERVER"]\r
- if newshost.find('.') == -1:\r
- mode = 'readermode'\r
- else:\r
- mode = None\r
- s = NNTP(newshost, readermode=mode)\r
- resp, count, first, last, name = s.group('comp.lang.python')\r
- print resp\r
- print 'Group', name, 'has', count, 'articles, range', first, 'to', last\r
- resp, subs = s.xhdr('subject', first + '-' + last)\r
- print resp\r
- for item in subs:\r
- print "%7s %s" % item\r
- resp = s.quit()\r
- print resp\r