1 """An NNTP client class based on RFC 977: Network News Transfer Protocol.
5 >>> from nntplib import NNTP
7 >>> resp, count, first, last, name = s.group('comp.lang.python')
8 >>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
9 Group comp.lang.python has 51 articles, range 5770 to 5821
10 >>> resp, subs = s.xhdr('subject', first + '-' + last)
14 Here 'resp' is the server response line.
15 Error responses are turned into exceptions.
17 To post an article from a file:
18 >>> f = open(filename, 'r') # file containing article, including header
22 For descriptions of all methods, read the comments in the code below.
23 Note that all arguments and return values representing article numbers
24 are strings, not numbers, since they are rarely used for calculations.
27 # RFC 977 by Brian Kantor and Phil Lapsley.
28 # xover, xgtitle, xpath, date methods by Kevan Heydon
35 __all__
= ["NNTP","NNTPReplyError","NNTPTemporaryError",
36 "NNTPPermanentError","NNTPProtocolError","NNTPDataError",
37 "error_reply","error_temp","error_perm","error_proto",
40 # Exceptions raised when an error or invalid response is received
41 class NNTPError(Exception):
42 """Base class for all nntplib exceptions"""
43 def __init__(self
, *args
):
44 apply(Exception.__init
__, (self
,)+args
)
46 self
.response
= args
[0]
48 self
.response
= 'No response given'
50 class NNTPReplyError(NNTPError
):
51 """Unexpected [123]xx reply"""
54 class NNTPTemporaryError(NNTPError
):
58 class NNTPPermanentError(NNTPError
):
62 class NNTPProtocolError(NNTPError
):
63 """Response does not begin with [1-5]"""
66 class NNTPDataError(NNTPError
):
67 """Error in response data"""
70 # for backwards compatibility
71 error_reply
= NNTPReplyError
72 error_temp
= NNTPTemporaryError
73 error_perm
= NNTPPermanentError
74 error_proto
= NNTPProtocolError
75 error_data
= NNTPDataError
79 # Standard port used by NNTP servers
83 # Response numbers that are followed by additional text (e.g. article)
84 LONGRESP
= ['100', '215', '220', '221', '222', '224', '230', '231', '282']
87 # Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)
94 def __init__(self
, host
, port
=NNTP_PORT
, user
=None, password
=None,
96 """Initialize an instance. Arguments:
97 - host: hostname to connect to
98 - port: port to connect to (default the standard NNTP port)
99 - user: username to authenticate with
100 - password: password to use with username
101 - readermode: if true, send 'mode reader' command after
104 readermode is sometimes necessary if you are connecting to an
105 NNTP server on the local machine and intend to call
106 reader-specific comamnds, such as `group'. If you get
107 unexpected NNTPPermanentErrors, you might need to set
112 self
.sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
113 self
.sock
.connect((self
.host
, self
.port
))
114 self
.file = self
.sock
.makefile('rb')
116 self
.welcome
= self
.getresp()
118 # 'mode reader' is sometimes necessary to enable 'reader' mode.
119 # However, the order in which 'mode reader' and 'authinfo' need to
120 # arrive differs between some NNTP servers. Try to send
121 # 'mode reader', and if it fails with an authorization failed
122 # error, try again after sending authinfo.
123 readermode_afterauth
= 0
126 self
.welcome
= self
.shortcmd('mode reader')
127 except NNTPPermanentError
:
128 # error 500, probably 'not implemented'
130 except NNTPTemporaryError
, e
:
131 if user
and e
.response
[:3] == '480':
132 # Need authorization before 'mode reader'
133 readermode_afterauth
= 1
136 # If no login/password was specified, try to get them from ~/.netrc
137 # Presume that if .netc has an entry, NNRP authentication is required.
141 credentials
= netrc
.netrc()
142 auth
= credentials
.authenticators(host
)
148 # Perform NNRP authentication if needed.
150 resp
= self
.shortcmd('authinfo user '+user
)
151 if resp
[:3] == '381':
153 raise NNTPReplyError(resp
)
155 resp
= self
.shortcmd(
156 'authinfo pass '+password
)
157 if resp
[:3] != '281':
158 raise NNTPPermanentError(resp
)
159 if readermode_afterauth
:
161 self
.welcome
= self
.shortcmd('mode reader')
162 except NNTPPermanentError
:
163 # error 500, probably 'not implemented'
167 # Get the welcome message from the server
168 # (this is read and squirreled away by __init__()).
169 # If the response code is 200, posting is allowed;
170 # if it 201, posting is not allowed
172 def getwelcome(self
):
173 """Get the welcome message from the server
174 (this is read and squirreled away by __init__()).
175 If the response code is 200, posting is allowed;
176 if it 201, posting is not allowed."""
178 if self
.debugging
: print '*welcome*', `self
.welcome`
181 def set_debuglevel(self
, level
):
182 """Set the debugging level. Argument 'level' means:
183 0: no debugging output (default)
184 1: print commands and responses but not body text etc.
185 2: also print raw lines read and sent before stripping CR/LF"""
187 self
.debugging
= level
188 debug
= set_debuglevel
190 def putline(self
, line
):
191 """Internal: send one line to the server, appending CRLF."""
193 if self
.debugging
> 1: print '*put*', `line`
194 self
.sock
.sendall(line
)
196 def putcmd(self
, line
):
197 """Internal: send one command to the server (through putline())."""
198 if self
.debugging
: print '*cmd*', `line`
202 """Internal: return one line from the server, stripping CRLF.
203 Raise EOFError if the connection is closed."""
204 line
= self
.file.readline()
205 if self
.debugging
> 1:
206 print '*get*', `line`
207 if not line
: raise EOFError
208 if line
[-2:] == CRLF
: line
= line
[:-2]
209 elif line
[-1:] in CRLF
: line
= line
[:-1]
213 """Internal: get a response from the server.
214 Raise various errors if the response indicates an error."""
215 resp
= self
.getline()
216 if self
.debugging
: print '*resp*', `resp`
219 raise NNTPTemporaryError(resp
)
221 raise NNTPPermanentError(resp
)
223 raise NNTPProtocolError(resp
)
226 def getlongresp(self
, file=None):
227 """Internal: get a response plus following text from the server.
228 Raise various errors if the response indicates an error."""
232 # If a string was passed then open a file with that name
233 if isinstance(file, str):
234 openedFile
= file = open(file, "w")
236 resp
= self
.getresp()
237 if resp
[:3] not in LONGRESP
:
238 raise NNTPReplyError(resp
)
241 line
= self
.getline()
247 file.write(line
+ "\n")
251 # If this method created the file, then it must close it
257 def shortcmd(self
, line
):
258 """Internal: send a command and get the response."""
260 return self
.getresp()
262 def longcmd(self
, line
, file=None):
263 """Internal: send a command and get the response plus following text."""
265 return self
.getlongresp(file)
267 def newgroups(self
, date
, time
):
268 """Process a NEWGROUPS command. Arguments:
269 - date: string 'yymmdd' indicating the date
270 - time: string 'hhmmss' indicating the time
272 - resp: server response if successful
273 - list: list of newsgroup names"""
275 return self
.longcmd('NEWGROUPS ' + date
+ ' ' + time
)
277 def newnews(self
, group
, date
, time
):
278 """Process a NEWNEWS command. Arguments:
279 - group: group name or '*'
280 - date: string 'yymmdd' indicating the date
281 - time: string 'hhmmss' indicating the time
283 - resp: server response if successful
284 - list: list of article ids"""
286 cmd
= 'NEWNEWS ' + group
+ ' ' + date
+ ' ' + time
287 return self
.longcmd(cmd
)
290 """Process a LIST command. Return:
291 - resp: server response if successful
292 - list: list of (group, last, first, flag) (strings)"""
294 resp
, list = self
.longcmd('LIST')
295 for i
in range(len(list)):
296 # Parse lines into "group last first flag"
297 list[i
] = tuple(list[i
].split())
300 def group(self
, name
):
301 """Process a GROUP command. Argument:
302 - group: the group name
304 - resp: server response if successful
305 - count: number of articles (string)
306 - first: first article number (string)
307 - last: last article number (string)
308 - name: the group name"""
310 resp
= self
.shortcmd('GROUP ' + name
)
311 if resp
[:3] != '211':
312 raise NNTPReplyError(resp
)
314 count
= first
= last
= 0
323 name
= words
[4].lower()
324 return resp
, count
, first
, last
, name
327 """Process a HELP command. Returns:
328 - resp: server response if successful
329 - list: list of strings"""
331 return self
.longcmd('HELP')
333 def statparse(self
, resp
):
334 """Internal: parse the response of a STAT, NEXT or LAST command."""
336 raise NNTPReplyError(resp
)
347 def statcmd(self
, line
):
348 """Internal: process a STAT, NEXT or LAST command."""
349 resp
= self
.shortcmd(line
)
350 return self
.statparse(resp
)
353 """Process a STAT command. Argument:
354 - id: article number or message id
356 - resp: server response if successful
357 - nr: the article number
358 - id: the article id"""
360 return self
.statcmd('STAT ' + id)
363 """Process a NEXT command. No arguments. Return as for STAT."""
364 return self
.statcmd('NEXT')
367 """Process a LAST command. No arguments. Return as for STAT."""
368 return self
.statcmd('LAST')
370 def artcmd(self
, line
, file=None):
371 """Internal: process a HEAD, BODY or ARTICLE command."""
372 resp
, list = self
.longcmd(line
, file)
373 resp
, nr
, id = self
.statparse(resp
)
374 return resp
, nr
, id, list
377 """Process a HEAD command. Argument:
378 - id: article number or message id
380 - resp: server response if successful
383 - list: the lines of the article's header"""
385 return self
.artcmd('HEAD ' + id)
387 def body(self
, id, file=None):
388 """Process a BODY command. Argument:
389 - id: article number or message id
390 - file: Filename string or file object to store the article in
392 - resp: server response if successful
395 - list: the lines of the article's body or an empty list
398 return self
.artcmd('BODY ' + id, file)
400 def article(self
, id):
401 """Process an ARTICLE command. Argument:
402 - id: article number or message id
404 - resp: server response if successful
407 - list: the lines of the article"""
409 return self
.artcmd('ARTICLE ' + id)
412 """Process a SLAVE command. Returns:
413 - resp: server response if successful"""
415 return self
.shortcmd('SLAVE')
417 def xhdr(self
, hdr
, str):
418 """Process an XHDR command (optional server extension). Arguments:
419 - hdr: the header type (e.g. 'subject')
420 - str: an article nr, a message id, or a range nr1-nr2
422 - resp: server response if successful
423 - list: list of (nr, value) strings"""
425 pat
= re
.compile('^([0-9]+) ?(.*)\n?')
426 resp
, lines
= self
.longcmd('XHDR ' + hdr
+ ' ' + str)
427 for i
in range(len(lines
)):
431 lines
[i
] = m
.group(1, 2)
434 def xover(self
,start
,end
):
435 """Process an XOVER command (optional server extension) Arguments:
436 - start: start of range
439 - resp: server response if successful
440 - list: list of (art-nr, subject, poster, date,
441 id, references, size, lines)"""
443 resp
, lines
= self
.longcmd('XOVER ' + start
+ '-' + end
)
446 elem
= line
.split("\t")
448 xover_lines
.append((elem
[0],
457 raise NNTPDataError(line
)
458 return resp
,xover_lines
460 def xgtitle(self
, group
):
461 """Process an XGTITLE command (optional server extension) Arguments:
462 - group: group name wildcard (i.e. news.*)
464 - resp: server response if successful
465 - list: list of (name,title) strings"""
467 line_pat
= re
.compile("^([^ \t]+)[ \t]+(.*)$")
468 resp
, raw_lines
= self
.longcmd('XGTITLE ' + group
)
470 for raw_line
in raw_lines
:
471 match
= line_pat
.search(raw_line
.strip())
473 lines
.append(match
.group(1, 2))
477 """Process an XPATH command (optional server extension) Arguments:
478 - id: Message id of article
480 resp: server response if successful
481 path: directory path to article"""
483 resp
= self
.shortcmd("XPATH " + id)
484 if resp
[:3] != '223':
485 raise NNTPReplyError(resp
)
487 [resp_num
, path
] = resp
.split()
489 raise NNTPReplyError(resp
)
494 """Process the DATE command. Arguments:
497 resp: server response if successful
498 date: Date suitable for newnews/newgroups commands etc.
499 time: Time suitable for newnews/newgroups commands etc."""
501 resp
= self
.shortcmd("DATE")
502 if resp
[:3] != '111':
503 raise NNTPReplyError(resp
)
506 raise NNTPDataError(resp
)
509 if len(date
) != 6 or len(time
) != 6:
510 raise NNTPDataError(resp
)
511 return resp
, date
, time
515 """Process a POST command. Arguments:
516 - f: file containing the article
518 - resp: server response if successful"""
520 resp
= self
.shortcmd('POST')
521 # Raises error_??? if posting is not allowed
523 raise NNTPReplyError(resp
)
534 return self
.getresp()
536 def ihave(self
, id, f
):
537 """Process an IHAVE command. Arguments:
538 - id: message-id of the article
539 - f: file containing the article
541 - resp: server response if successful
542 Note that if the server refuses the article an exception is raised."""
544 resp
= self
.shortcmd('IHAVE ' + id)
545 # Raises error_??? if the server already has it
547 raise NNTPReplyError(resp
)
558 return self
.getresp()
561 """Process a QUIT command and close the socket. Returns:
562 - resp: server response if successful"""
564 resp
= self
.shortcmd('QUIT')
567 del self
.file, self
.sock
571 # Test retrieval when run as a script.
572 # Assumption: if there's a local news server, it's called 'news'.
573 # Assumption: if user queries a remote news server, it's named
574 # in the environment variable NNTPSERVER (used by slrn and kin)
575 # and we want readermode off.
576 if __name__
== '__main__':
578 newshost
= 'news' and os
.environ
["NNTPSERVER"]
579 if newshost
.find('.') == -1:
583 s
= NNTP(newshost
, readermode
=mode
)
584 resp
, count
, first
, last
, name
= s
.group('comp.lang.python')
586 print 'Group', name
, 'has', count
, 'articles, range', first
, 'to', last
587 resp
, subs
= s
.xhdr('subject', first
+ '-' + last
)
590 print "%7s %s" % item