blob: c448d48241b2a74a179709f05f849e08d12c0bf1 [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 Rossumc629d341992-11-05 10:43:02 +00008# >>> s = NNTP().init('charon')
9# >>> 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
63 def init(self, host, *args):
64 if len(args) > 1: raise TypeError, 'too many args'
65 if args: port = args[0]
66 else: port = NNTP_PORT
67 self.host = host
68 self.port = port
69 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
70 self.sock.connect(self.host, self.port)
71 self.file = self.sock.makefile('r')
72 self.debugging = 0
73 self.welcome = self.getresp()
74 return self
75
76 # Get the welcome message from the server
77 # (this is read and squirreled away by init()).
78 # If the response code is 200, posting is allowed;
79 # if it 201, posting is not allowed
80
81 def getwelcome(self):
82 if self.debugging: print '*welcome*', `self.welcome`
83 return self.welcome
84
85 # Set the debugging level. Argument level means:
86 # 0: no debugging output (default)
87 # 1: print commands and responses but not body text etc.
88 # 2: also print raw lines read and sent before stripping CR/LF
89
90 def debug(self, level):
91 self.debugging = level
92
93 # Internal: send one line to the server, appending CRLF
94 def putline(self, line):
95 line = line + CRLF
96 if self.debugging > 1: print '*put*', `line`
97 self.sock.send(line)
98
99 # Internal: send one command to the server (through putline())
100 def putcmd(self, line):
101 if self.debugging: print '*cmd*', `line`
102 self.putline(line)
103
104 # Internal: return one line from the server, stripping CRLF.
105 # Raise EOFError if the connection is closed
106 def getline(self):
107 line = self.file.readline()
108 if self.debugging > 1:
109 print '*get*', `line`
110 if not line: raise EOFError
111 if line[-2:] == CRLF: line = line[:-2]
112 elif line[-1:] in CRLF: line = line[:-1]
113 return line
114
115 # Internal: get a response from the server.
116 # Raise various errors if the response indicates an error
117 def getresp(self):
118 resp = self.getline()
119 if self.debugging: print '*resp*', `resp`
120 c = resp[:1]
121 if c == '4':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000122 raise error_temp, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000123 if c == '5':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000124 raise error_perm, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000125 if c not in '123':
Guido van Rossum18fc5691992-11-26 09:17:19 +0000126 raise error_proto, resp
Guido van Rossumc629d341992-11-05 10:43:02 +0000127 return resp
128
129 # Internal: get a response plus following text from the server.
130 # Raise various errors if the response indicates an error
131 def getlongresp(self):
132 resp = self.getresp()
133 if resp[:3] not in LONGRESP:
134 raise error_reply, resp
135 list = []
136 while 1:
137 line = self.getline()
138 if line == '.':
139 break
140 list.append(line)
141 return resp, list
142
143 # Internal: send a command and get the response
144 def shortcmd(self, line):
145 self.putcmd(line)
146 return self.getresp()
147
148 # Internal: send a command and get the response plus following text
149 def longcmd(self, line):
150 self.putcmd(line)
151 return self.getlongresp()
152
153 # Process a NEWGROUPS command. Arguments:
154 # - date: string 'yymmdd' indicating the date
155 # - time: string 'hhmmss' indicating the time
156 # Return:
157 # - resp: server response if succesful
158 # - list: list of newsgroup names
159
160 def newgroups(self, date, time):
161 return self.longcmd('NEWGROUPS ' + date + ' ' + time)
162
163 # Process a NEWNEWS command. Arguments:
164 # - group: group name or '*'
165 # - date: string 'yymmdd' indicating the date
166 # - time: string 'hhmmss' indicating the time
167 # Return:
168 # - resp: server response if succesful
169 # - list: list of article ids
170
171 def newnews(self, group, date, time):
172 cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time
173 return self.longcmd(cmd)
174
175 # Process a LIST command. Return:
176 # - resp: server response if succesful
177 # - list: list of (group, first, last, flag) (strings)
178
179 def list(self):
180 resp, list = self.longcmd('LIST')
181 for i in range(len(list)):
182 # Parse lines into "group first last flag"
183 list[i] = string.split(list[i])
184 return resp, list
185
186 # Process a GROUP command. Argument:
187 # - group: the group name
188 # Returns:
189 # - resp: server response if succesful
190 # - count: number of articles (string)
191 # - first: first article number (string)
192 # - last: last article number (string)
193 # - name: the group name
194
195 def group(self, name):
196 resp = self.shortcmd('GROUP ' + name)
197 if resp[:3] <> '211':
198 raise error_reply, resp
199 words = string.split(resp)
200 count = first = last = 0
201 n = len(words)
202 if n > 1:
203 count = words[1]
204 if n > 2:
205 first = words[2]
206 if n > 3:
207 last = words[3]
208 if n > 4:
209 name = string.lower(words[4])
210 return resp, count, first, last, name
211
212 # Process a HELP command. Returns:
213 # - resp: server response if succesful
214 # - list: list of strings
215
216 def help(self):
217 return self.longcmd('HELP')
218
219 # Internal: parse the response of a STAT, NEXT or LAST command
220 def statparse(self, resp):
221 if resp[:2] <> '22':
222 raise error_reply, resp
223 words = string.split(resp)
224 nr = 0
225 id = ''
226 n = len(words)
227 if n > 1:
228 nr = words[1]
229 if n > 2:
230 id = string.lower(words[2])
231 return resp, nr, id
232
233 # Internal: process a STAT, NEXT or LAST command
234 def statcmd(self, line):
235 resp = self.shortcmd(line)
236 return self.statparse(resp)
237
238 # Process a STAT command. Argument:
239 # - id: article number or message id
240 # Returns:
241 # - resp: server response if succesful
242 # - nr: the article number
243 # - id: the article id
244
245 def stat(self, id):
246 return self.statcmd('STAT ' + id)
247
248 # Process a NEXT command. No arguments. Return as for STAT
249
250 def next(self):
251 return self.statcmd('NEXT')
252
253 # Process a LAST command. No arguments. Return as for STAT
254
255 def last(self):
256 return self.statcmd('LAST')
257
258 # Internal: process a HEAD, BODY or ARTICLE command
259 def artcmd(self, line):
260 resp, list = self.longcmd(line)
261 resp, nr, id = self.statparse(resp)
262 return resp, nr, id, list
263
264 # Process a HEAD command. Argument:
265 # - id: article number or message id
266 # Returns:
267 # - resp: server response if succesful
268 # - list: the lines of the article's header
269
270 def head(self, id):
271 return self.artcmd('HEAD ' + id)
272
273 # Process a BODY command. Argument:
274 # - id: article number or message id
275 # Returns:
276 # - resp: server response if succesful
277 # - list: the lines of the article's body
278
279 def body(self, id):
280 return self.artcmd('BODY ' + id)
281
282 # Process an ARTICLE command. Argument:
283 # - id: article number or message id
284 # Returns:
285 # - resp: server response if succesful
286 # - list: the lines of the article
287
288 def article(self, id):
289 return self.artcmd('ARTICLE ' + id)
290
291 # Process a SLAVE command. Returns:
292 # - resp: server response if succesful
293
294 def slave(self):
295 return self.shortcmd('SLAVE')
296
297 # Process an XHDR command (optional server extension). Arguments:
298 # - hdr: the header type (e.g. 'subject')
299 # - str: an article nr, a message id, or a range nr1-nr2
300 # Returns:
301 # - resp: server response if succesful
302 # - list: list of (nr, value) strings
303
304 def xhdr(self, hdr, str):
305 resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str)
306 for i in range(len(lines)):
307 line = lines[i]
308 n = regex.match('^[0-9]+', line)
309 nr = line[:n]
310 if n < len(line) and line[n] == ' ': n = n+1
311 lines[i] = (nr, line[n:])
312 return resp, lines
313
314 # Process a POST command. Arguments:
315 # - f: file containing the article
316 # Returns:
317 # - resp: server response if succesful
318
319 def post(self, f):
320 resp = self.shortcmd('POST')
321 # Raises error_??? if posting is not allowed
322 if resp[0] <> '3':
323 raise error_reply, resp
324 while 1:
325 line = f.readline()
326 if not line:
327 break
328 if line[-1] == '\n':
329 line = line[:-1]
330 if line == '.':
331 line = '..'
332 self.putline(line)
333 self.putline('.')
334 return self.getresp()
335
336 # Process an IHAVE command. Arguments:
337 # - id: message-id of the article
338 # - f: file containing the article
339 # Returns:
340 # - resp: server response if succesful
341 # Note that if the server refuses the article an exception is raised
342
343 def ihave(self, id, f):
344 resp = self.shortcmd('IHAVE ' + id)
Guido van Rossum18fc5691992-11-26 09:17:19 +0000345 # Raises error_??? if the server already has it
Guido van Rossumc629d341992-11-05 10:43:02 +0000346 if resp[0] <> '3':
347 raise error_reply, resp
348 while 1:
349 line = f.readline()
350 if not line:
351 break
352 if line[-1] == '\n':
353 line = line[:-1]
354 if line == '.':
355 line = '..'
356 self.putline(line)
357 self.putline('.')
358 return self.getresp()
359
360 # Process a QUIT command and close the socket. Returns:
361 # - resp: server response if succesful
362
363 def quit(self):
364 resp = self.shortcmd('QUIT')
365 self.file.close()
366 self.sock.close()
367 del self.file, self.sock
368 return resp