blob: 8d0d6afd14cac005013de42f913d72769dc69e31 [file] [log] [blame]
Guido van Rossumc629d341992-11-05 10:43:02 +00001# An NNTP client class. Based on RFC 977: Network News Transfer
2# Protocol, by Brian Kantor and Phil Lapsley.
3
4
5# Example:
6#
Guido van Rossum18fc5691992-11-26 09:17:19 +00007# >>> from nntplib import NNTP
Guido van Rossum7bc817d1993-12-17 15:25:27 +00008# >>> s = NNTP('charon')
Guido van Rossumc629d341992-11-05 10:43:02 +00009# >>> resp, count, first, last, name = s.group('nlnet.misc')
10# >>> print 'Group', name, 'has', count, 'articles, range', first, 'to', last
11# Group nlnet.misc has 525 articles, range 6960 to 7485
12# >>> resp, subs = s.xhdr('subject', first + '-' + last)
13# >>> resp = s.quit()
14# >>>
15#
16# Here 'resp' is the server response line.
17# Error responses are turned into exceptions.
18#
19# To post an article from a file:
20# >>> f = open(filename, 'r') # file containing article, including header
21# >>> resp = s.post(f)
22# >>>
23#
24# For descriptions of all methods, read the comments in the code below.
25# Note that all arguments and return values representing article numbers
26# are strings, not numbers, since they are rarely used for calculations.
27
28
29# Imports
30import regex
31import socket
32import string
33
34
Guido van Rossum18fc5691992-11-26 09:17:19 +000035# Exception raised when an error or invalid response is received
Guido van Rossumc629d341992-11-05 10:43:02 +000036
Guido van Rossum18fc5691992-11-26 09:17:19 +000037error_reply = 'nntplib.error_reply' # unexpected [123]xx reply
38error_temp = 'nntplib.error_temp' # 4xx errors
39error_perm = 'nntplib.error_perm' # 5xx errors
40error_proto = 'nntplib.error_proto' # response does not begin with [1-5]
Guido van Rossumc629d341992-11-05 10:43:02 +000041
42
43# Standard port used by NNTP servers
44NNTP_PORT = 119
45
46
47# Response numbers that are followed by additional text (e.g. article)
48LONGRESP = ['100', '215', '220', '221', '222', '230', '231']
49
50
51# Line terminators (we always output CRLF, but accept any of CRLF, CR, LF)
52CRLF = '\r\n'
53
54
55# The class itself
56
57class NNTP:
58
59 # Initialize an instance. Arguments:
60 # - host: hostname to connect to
61 # - port: port to connect to (default the standard NNTP port)
62
Guido van Rossumb6775db1994-08-01 11:34:53 +000063 def __init__(self, host, port = NNTP_PORT):
Guido van Rossumc629d341992-11-05 10:43:02 +000064 self.host = host
65 self.port = port
66 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
67 self.sock.connect(self.host, self.port)
68 self.file = self.sock.makefile('r')
69 self.debugging = 0
70 self.welcome = self.getresp()
Guido van Rossumc629d341992-11-05 10:43:02 +000071
72 # Get the welcome message from the server
Guido van Rossum7bc817d1993-12-17 15:25:27 +000073 # (this is read and squirreled away by __init__()).
Guido van Rossumc629d341992-11-05 10:43:02 +000074 # If the response code is 200, posting is allowed;
75 # if it 201, posting is not allowed
76
77 def getwelcome(self):
78 if self.debugging: print '*welcome*', `self.welcome`
79 return self.welcome
80
81 # Set the debugging level. Argument level means:
82 # 0: no debugging output (default)
83 # 1: print commands and responses but not body text etc.
84 # 2: also print raw lines read and sent before stripping CR/LF
85
Guido van Rossumcf5394f1995-03-30 10:42:34 +000086 def set_debuglevel(self, level):
Guido van Rossumc629d341992-11-05 10:43:02 +000087 self.debugging = level
Guido van Rossumcf5394f1995-03-30 10:42:34 +000088 debug = set_debuglevel
Guido van Rossumc629d341992-11-05 10:43:02 +000089
90 # Internal: send one line to the server, appending CRLF
91 def putline(self, line):
92 line = line + CRLF
93 if self.debugging > 1: print '*put*', `line`
94 self.sock.send(line)
95
96 # Internal: send one command to the server (through putline())
97 def putcmd(self, line):
98 if self.debugging: print '*cmd*', `line`
99 self.putline(line)
100
101 # Internal: return one line from the server, stripping CRLF.
102 # Raise EOFError if the connection is closed
103 def getline(self):
104 line = self.file.readline()
105 if self.debugging > 1:
106 print '*get*', `line`
107 if not line: raise EOFError
108 if line[-2:] == CRLF: line = line[:-2]
109 elif line[-1:] in CRLF: line = line[:-1]
110 return line
111
112 # Internal: get a response from the server.
113 # Raise various errors if the response indicates an error
114 def getresp(self):
115 resp = self.getline()
116 if self.debugging: print '*resp*', `resp`
117 c = resp[:1]
118 if c == '4':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000119 raise error_temp, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000120 if c == '5':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000121 raise error_perm, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000122 if c not in '123':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000123 raise error_proto, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000124 return resp
125
126 # Internal: get a response plus following text from the server.
127 # Raise various errors if the response indicates an error
128 def getlongresp(self):
129 resp = self.getresp()
130 if resp[:3] not in LONGRESP:
131 raise error_reply, resp
132 list = []
133 while 1:
134 line = self.getline()
135 if line == '.':
136 break
137 list.append(line)
138 return resp, list
139
140 # Internal: send a command and get the response
141 def shortcmd(self, line):
142 self.putcmd(line)
143 return self.getresp()
144
145 # Internal: send a command and get the response plus following text
146 def longcmd(self, line):
147 self.putcmd(line)
148 return self.getlongresp()
149
150 # Process a NEWGROUPS command. Arguments:
151 # - date: string 'yymmdd' indicating the date
152 # - time: string 'hhmmss' indicating the time
153 # Return:
154 # - resp: server response if succesful
155 # - list: list of newsgroup names
156
157 def newgroups(self, date, time):
158 return self.longcmd('NEWGROUPS ' + date + ' ' + time)
159
160 # Process a NEWNEWS command. Arguments:
161 # - group: group name or '*'
162 # - date: string 'yymmdd' indicating the date
163 # - time: string 'hhmmss' indicating the time
164 # Return:
165 # - resp: server response if succesful
166 # - list: list of article ids
167
168 def newnews(self, group, date, time):
169 cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time
170 return self.longcmd(cmd)
171
172 # Process a LIST command. Return:
173 # - resp: server response if succesful
Guido van Rossumbe9f2121995-01-10 10:35:55 +0000174 # - list: list of (group, last, first, flag) (strings)
Guido van Rossumc629d341992-11-05 10:43:02 +0000175
176 def list(self):
177 resp, list = self.longcmd('LIST')
178 for i in range(len(list)):
Guido van Rossumbe9f2121995-01-10 10:35:55 +0000179 # Parse lines into "group last first flag"
Guido van Rossumc629d341992-11-05 10:43:02 +0000180 list[i] = string.split(list[i])
181 return resp, list
182
183 # Process a GROUP command. Argument:
184 # - group: the group name
185 # Returns:
186 # - resp: server response if succesful
187 # - count: number of articles (string)
188 # - first: first article number (string)
189 # - last: last article number (string)
190 # - name: the group name
191
192 def group(self, name):
193 resp = self.shortcmd('GROUP ' + name)
194 if resp[:3] <> '211':
195 raise error_reply, resp
196 words = string.split(resp)
197 count = first = last = 0
198 n = len(words)
199 if n > 1:
200 count = words[1]
201 if n > 2:
202 first = words[2]
203 if n > 3:
204 last = words[3]
205 if n > 4:
206 name = string.lower(words[4])
207 return resp, count, first, last, name
208
209 # Process a HELP command. Returns:
210 # - resp: server response if succesful
211 # - list: list of strings
212
213 def help(self):
214 return self.longcmd('HELP')
215
216 # Internal: parse the response of a STAT, NEXT or LAST command
217 def statparse(self, resp):
218 if resp[:2] <> '22':
219 raise error_reply, resp
220 words = string.split(resp)
221 nr = 0
222 id = ''
223 n = len(words)
224 if n > 1:
225 nr = words[1]
226 if n > 2:
227 id = string.lower(words[2])
228 return resp, nr, id
229
230 # Internal: process a STAT, NEXT or LAST command
231 def statcmd(self, line):
232 resp = self.shortcmd(line)
233 return self.statparse(resp)
234
235 # Process a STAT command. Argument:
236 # - id: article number or message id
237 # Returns:
238 # - resp: server response if succesful
239 # - nr: the article number
240 # - id: the article id
241
242 def stat(self, id):
243 return self.statcmd('STAT ' + id)
244
245 # Process a NEXT command. No arguments. Return as for STAT
246
247 def next(self):
248 return self.statcmd('NEXT')
249
250 # Process a LAST command. No arguments. Return as for STAT
251
252 def last(self):
253 return self.statcmd('LAST')
254
255 # Internal: process a HEAD, BODY or ARTICLE command
256 def artcmd(self, line):
257 resp, list = self.longcmd(line)
258 resp, nr, id = self.statparse(resp)
259 return resp, nr, id, list
260
261 # Process a HEAD command. Argument:
262 # - id: article number or message id
263 # Returns:
264 # - resp: server response if succesful
265 # - list: the lines of the article's header
266
267 def head(self, id):
268 return self.artcmd('HEAD ' + id)
269
270 # Process a BODY command. Argument:
271 # - id: article number or message id
272 # Returns:
273 # - resp: server response if succesful
274 # - list: the lines of the article's body
275
276 def body(self, id):
277 return self.artcmd('BODY ' + id)
278
279 # Process an ARTICLE command. Argument:
280 # - id: article number or message id
281 # Returns:
282 # - resp: server response if succesful
283 # - list: the lines of the article
284
285 def article(self, id):
286 return self.artcmd('ARTICLE ' + id)
287
288 # Process a SLAVE command. Returns:
289 # - resp: server response if succesful
290
291 def slave(self):
292 return self.shortcmd('SLAVE')
293
294 # Process an XHDR command (optional server extension). Arguments:
295 # - hdr: the header type (e.g. 'subject')
296 # - str: an article nr, a message id, or a range nr1-nr2
297 # Returns:
298 # - resp: server response if succesful
299 # - list: list of (nr, value) strings
300
301 def xhdr(self, hdr, str):
302 resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str)
303 for i in range(len(lines)):
304 line = lines[i]
305 n = regex.match('^[0-9]+', line)
306 nr = line[:n]
307 if n < len(line) and line[n] == ' ': n = n+1
308 lines[i] = (nr, line[n:])
309 return resp, lines
310
311 # Process a POST command. Arguments:
312 # - f: file containing the article
313 # Returns:
314 # - resp: server response if succesful
315
316 def post(self, f):
317 resp = self.shortcmd('POST')
318 # Raises error_??? if posting is not allowed
319 if resp[0] <> '3':
320 raise error_reply, resp
321 while 1:
322 line = f.readline()
323 if not line:
324 break
325 if line[-1] == '\n':
326 line = line[:-1]
327 if line == '.':
328 line = '..'
329 self.putline(line)
330 self.putline('.')
331 return self.getresp()
332
333 # Process an IHAVE command. Arguments:
334 # - id: message-id of the article
335 # - f: file containing the article
336 # Returns:
337 # - resp: server response if succesful
338 # Note that if the server refuses the article an exception is raised
339
340 def ihave(self, id, f):
341 resp = self.shortcmd('IHAVE ' + id)
Guido van Rossum18fc5691992-11-26 09:17:19 +0000342 # Raises error_??? if the server already has it
Guido van Rossumc629d341992-11-05 10:43:02 +0000343 if resp[0] <> '3':
344 raise error_reply, resp
345 while 1:
346 line = f.readline()
347 if not line:
348 break
349 if line[-1] == '\n':
350 line = line[:-1]
351 if line == '.':
352 line = '..'
353 self.putline(line)
354 self.putline('.')
355 return self.getresp()
356
357 # Process a QUIT command and close the socket. Returns:
358 # - resp: server response if succesful
359
360 def quit(self):
361 resp = self.shortcmd('QUIT')
362 self.file.close()
363 self.sock.close()
364 del self.file, self.sock
365 return resp