Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
NNTP Class Reference

Public Member Functions

def __init__
 
def getwelcome
 
def set_debuglevel
 
def putline
 
def putcmd
 
def getline
 
def getresp
 
def getlongresp
 
def shortcmd
 
def longcmd
 
def newgroups
 
def newnews
 
def list
 
def group
 
def help
 
def statparse
 
def statcmd
 
def stat
 
def next
 
def last
 
def artcmd
 
def head
 
def body
 
def article
 
def slave
 
def xhdr
 
def xover
 
def xgtitle
 
def xpath
 
def date
 
def post
 
def ihave
 
def quit
 

Data Fields

 host
 
 port
 
 sock
 
 file
 
 debugging
 
 welcome
 

Static Public Attributes

 debug = set_debuglevel
 

Detailed Description

Definition at line 94 of file nntplib.py.

Constructor & Destructor Documentation

def __init__ (   self,
  host,
  port = NNTP_PORT,
  user = None,
  password = None,
  readermode = None 
)
Initialize an instance.  Arguments:
- host: hostname to connect to
- 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.

Definition at line 96 of file nntplib.py.

96 
97  readermode=None):
98  """Initialize an instance. Arguments:
99  - host: hostname to connect to
100  - port: port to connect to (default the standard NNTP port)
101  - user: username to authenticate with
102  - password: password to use with username
103  - readermode: if true, send 'mode reader' command after
104  connecting.
105 
106  readermode is sometimes necessary if you are connecting to an
107  NNTP server on the local machine and intend to call
108  reader-specific comamnds, such as `group'. If you get
109  unexpected NNTPPermanentErrors, you might need to set
110  readermode.
111  """
112  self.host = host
113  self.port = port
114  self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
115  self.sock.connect((self.host, self.port))
116  self.file = self.sock.makefile('rb')
117  self.debugging = 0
118  self.welcome = self.getresp()
119 
120  # 'mode reader' is sometimes necessary to enable 'reader' mode.
121  # However, the order in which 'mode reader' and 'authinfo' need to
122  # arrive differs between some NNTP servers. Try to send
123  # 'mode reader', and if it fails with an authorization failed
124  # error, try again after sending authinfo.
125  readermode_afterauth = 0
126  if readermode:
127  try:
128  self.welcome = self.shortcmd('mode reader')
129  except NNTPPermanentError:
130  # error 500, probably 'not implemented'
131  pass
132  except NNTPTemporaryError, e:
133  if user and e.response[:3] == '480':
134  # Need authorization before 'mode reader'
135  readermode_afterauth = 1
136  else:
137  raise
138  if user:
139  resp = self.shortcmd('authinfo user '+user)
140  if resp[:3] == '381':
141  if not password:
142  raise NNTPReplyError(resp)
143  else:
144  resp = self.shortcmd(
145  'authinfo pass '+password)
146  if resp[:3] != '281':
147  raise NNTPPermanentError(resp)
148  if readermode_afterauth:
149  try:
150  self.welcome = self.shortcmd('mode reader')
151  except NNTPPermanentError:
152  # error 500, probably 'not implemented'
153  pass
154 

Member Function Documentation

def artcmd (   self,
  line,
  file = None 
)
Internal: process a HEAD, BODY or ARTICLE command.

Definition at line 358 of file nntplib.py.

359  def artcmd(self, line, file=None):
360  """Internal: process a HEAD, BODY or ARTICLE command."""
361  resp, list = self.longcmd(line, file)
362  resp, nr, id = self.statparse(resp)
363  return resp, nr, id, list
def article (   self,
  id 
)
Process an ARTICLE command.  Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the article

Definition at line 388 of file nntplib.py.

References NNTP.artcmd().

389  def article(self, id):
390  """Process an ARTICLE command. Argument:
391  - id: article number or message id
392  Returns:
393  - resp: server response if successful
394  - nr: article number
395  - id: message id
396  - list: the lines of the article"""
397 
398  return self.artcmd('ARTICLE ' + id)
def body (   self,
  id,
  file = None 
)
Process a BODY command.  Argument:
- id: article number or message id
- file: Filename string or file object to store the article in
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the article's body or an empty list
if file was used

Definition at line 375 of file nntplib.py.

References NNTP.artcmd().

376  def body(self, id, file=None):
377  """Process a BODY command. Argument:
378  - id: article number or message id
379  - file: Filename string or file object to store the article in
380  Returns:
381  - resp: server response if successful
382  - nr: article number
383  - id: message id
384  - list: the lines of the article's body or an empty list
385  if file was used"""
386 
387  return self.artcmd('BODY ' + id, file)
def date (   self)
Process the DATE command. Arguments:
None
Returns:
resp: server response if successful
date: Date suitable for newnews/newgroups commands etc.
time: Time suitable for newnews/newgroups commands etc.

Definition at line 481 of file nntplib.py.

References NNTP.shortcmd().

482  def date (self):
483  """Process the DATE command. Arguments:
484  None
485  Returns:
486  resp: server response if successful
487  date: Date suitable for newnews/newgroups commands etc.
488  time: Time suitable for newnews/newgroups commands etc."""
489 
490  resp = self.shortcmd("DATE")
491  if resp[:3] != '111':
492  raise NNTPReplyError(resp)
493  elem = resp.split()
494  if len(elem) != 2:
495  raise NNTPDataError(resp)
496  date = elem[1][2:8]
497  time = elem[1][-6:]
498  if len(date) != 6 or len(time) != 6:
499  raise NNTPDataError(resp)
500  return resp, date, time
501 
def getline (   self)
Internal: return one line from the server, stripping CRLF.
Raise EOFError if the connection is closed.

Definition at line 189 of file nntplib.py.

190  def getline(self):
191  """Internal: return one line from the server, stripping CRLF.
192  Raise EOFError if the connection is closed."""
193  line = self.file.readline()
194  if self.debugging > 1:
195  print '*get*', `line`
196  if not line: raise EOFError
197  if line[-2:] == CRLF: line = line[:-2]
198  elif line[-1:] in CRLF: line = line[:-1]
199  return line
def getlongresp (   self,
  file = None 
)
Internal: get a response plus following text from the server.
Raise various errors if the response indicates an error.

Definition at line 214 of file nntplib.py.

215  def getlongresp(self, file=None):
216  """Internal: get a response plus following text from the server.
217  Raise various errors if the response indicates an error."""
218 
219  openedFile = None
220  try:
221  # If a string was passed then open a file with that name
222  if isinstance(file, types.StringType):
223  openedFile = file = open(file, "w")
224 
225  resp = self.getresp()
226  if resp[:3] not in LONGRESP:
227  raise NNTPReplyError(resp)
228  list = []
229  while 1:
230  line = self.getline()
231  if line == '.':
232  break
233  if line[:2] == '..':
234  line = line[1:]
235  if file:
236  file.write(line + "\n")
237  else:
238  list.append(line)
239  finally:
240  # If this method created the file, then it must close it
241  if openedFile:
242  openedFile.close()
243 
244  return resp, list
def getresp (   self)
Internal: get a response from the server.
Raise various errors if the response indicates an error.

Definition at line 200 of file nntplib.py.

201  def getresp(self):
202  """Internal: get a response from the server.
203  Raise various errors if the response indicates an error."""
204  resp = self.getline()
205  if self.debugging: print '*resp*', `resp`
206  c = resp[:1]
207  if c == '4':
208  raise NNTPTemporaryError(resp)
209  if c == '5':
210  raise NNTPPermanentError(resp)
211  if c not in '123':
212  raise NNTPProtocolError(resp)
213  return resp
def getwelcome (   self)
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.

Definition at line 160 of file nntplib.py.

References NNTP.__init__().

161  def getwelcome(self):
162  """Get the welcome message from the server
163  (this is read and squirreled away by __init__()).
164  If the response code is 200, posting is allowed;
165  if it 201, posting is not allowed."""
166 
167  if self.debugging: print '*welcome*', `self.welcome`
168  return self.welcome
def group (   self,
  name 
)
Process a GROUP command.  Argument:
- group: the group name
Returns:
- resp: server response if successful
- count: number of articles (string)
- first: first article number (string)
- last: last article number (string)
- name: the group name

Definition at line 288 of file nntplib.py.

289  def group(self, name):
290  """Process a GROUP command. Argument:
291  - group: the group name
292  Returns:
293  - resp: server response if successful
294  - count: number of articles (string)
295  - first: first article number (string)
296  - last: last article number (string)
297  - name: the group name"""
298 
299  resp = self.shortcmd('GROUP ' + name)
300  if resp[:3] != '211':
301  raise NNTPReplyError(resp)
302  words = resp.split()
303  count = first = last = 0
304  n = len(words)
305  if n > 1:
306  count = words[1]
307  if n > 2:
308  first = words[2]
309  if n > 3:
310  last = words[3]
311  if n > 4:
312  name = words[4].lower()
313  return resp, count, first, last, name
def head (   self,
  id 
)
Process a HEAD command.  Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr: article number
- id: message id
- list: the lines of the article's header

Definition at line 364 of file nntplib.py.

References NNTP.artcmd().

365  def head(self, id):
366  """Process a HEAD command. Argument:
367  - id: article number or message id
368  Returns:
369  - resp: server response if successful
370  - nr: article number
371  - id: message id
372  - list: the lines of the article's header"""
373 
374  return self.artcmd('HEAD ' + id)
def help (   self)
Process a HELP command.  Returns:
- resp: server response if successful
- list: list of strings

Definition at line 314 of file nntplib.py.

315  def help(self):
316  """Process a HELP command. Returns:
317  - resp: server response if successful
318  - list: list of strings"""
319 
320  return self.longcmd('HELP')
def ihave (   self,
  id,
  f 
)
Process an IHAVE command.  Arguments:
- id: message-id of the article
- f:  file containing the article
Returns:
- resp: server response if successful
Note that if the server refuses the article an exception is raised.

Definition at line 524 of file nntplib.py.

References NNTP.getresp(), FTP.getresp(), FTP.putline(), NNTP.putline(), and NNTP.shortcmd().

525  def ihave(self, id, f):
526  """Process an IHAVE command. Arguments:
527  - id: message-id of the article
528  - f: file containing the article
529  Returns:
530  - resp: server response if successful
531  Note that if the server refuses the article an exception is raised."""
532 
533  resp = self.shortcmd('IHAVE ' + id)
534  # Raises error_??? if the server already has it
535  if resp[0] != '3':
536  raise NNTPReplyError(resp)
537  while 1:
538  line = f.readline()
539  if not line:
540  break
541  if line[-1] == '\n':
542  line = line[:-1]
543  if line[:1] == '.':
544  line = '.' + line
545  self.putline(line)
546  self.putline('.')
547  return self.getresp()
def last (   self)
Process a LAST command.  No arguments.  Return as for STAT.

Definition at line 354 of file nntplib.py.

355  def last(self):
356  """Process a LAST command. No arguments. Return as for STAT."""
357  return self.statcmd('LAST')
def list (   self)
Process a LIST command.  Return:
- resp: server response if successful
- list: list of (group, last, first, flag) (strings)

Definition at line 277 of file nntplib.py.

278  def list(self):
279  """Process a LIST command. Return:
280  - resp: server response if successful
281  - list: list of (group, last, first, flag) (strings)"""
282 
283  resp, list = self.longcmd('LIST')
284  for i in range(len(list)):
285  # Parse lines into "group last first flag"
286  list[i] = tuple(list[i].split())
287  return resp, list
def longcmd (   self,
  line,
  file = None 
)
Internal: send a command and get the response plus following text.

Definition at line 250 of file nntplib.py.

251  def longcmd(self, line, file=None):
252  """Internal: send a command and get the response plus following text."""
253  self.putcmd(line)
254  return self.getlongresp(file)
def newgroups (   self,
  date,
  time 
)
Process a NEWGROUPS command.  Arguments:
- date: string 'yymmdd' indicating the date
- time: string 'hhmmss' indicating the time
Return:
- resp: server response if successful
- list: list of newsgroup names

Definition at line 255 of file nntplib.py.

256  def newgroups(self, date, time):
257  """Process a NEWGROUPS command. Arguments:
258  - date: string 'yymmdd' indicating the date
259  - time: string 'hhmmss' indicating the time
260  Return:
261  - resp: server response if successful
262  - list: list of newsgroup names"""
263 
264  return self.longcmd('NEWGROUPS ' + date + ' ' + time)
def newnews (   self,
  group,
  date,
  time 
)
Process a NEWNEWS command.  Arguments:
- group: group name or '*'
- date: string 'yymmdd' indicating the date
- time: string 'hhmmss' indicating the time
Return:
- resp: server response if successful
- list: list of article ids

Definition at line 265 of file nntplib.py.

266  def newnews(self, group, date, time):
267  """Process a NEWNEWS command. Arguments:
268  - group: group name or '*'
269  - date: string 'yymmdd' indicating the date
270  - time: string 'hhmmss' indicating the time
271  Return:
272  - resp: server response if successful
273  - list: list of article ids"""
274 
275  cmd = 'NEWNEWS ' + group + ' ' + date + ' ' + time
276  return self.longcmd(cmd)
def next (   self)
Process a NEXT command.  No arguments.  Return as for STAT.

Definition at line 350 of file nntplib.py.

351  def next(self):
352  """Process a NEXT command. No arguments. Return as for STAT."""
353  return self.statcmd('NEXT')
def post (   self,
  f 
)
Process a POST command.  Arguments:
- f: file containing the article
Returns:
- resp: server response if successful

Definition at line 502 of file nntplib.py.

References NNTP.getresp(), FTP.getresp(), FTP.putline(), NNTP.putline(), and NNTP.shortcmd().

503  def post(self, f):
504  """Process a POST command. Arguments:
505  - f: file containing the article
506  Returns:
507  - resp: server response if successful"""
508 
509  resp = self.shortcmd('POST')
510  # Raises error_??? if posting is not allowed
511  if resp[0] != '3':
512  raise NNTPReplyError(resp)
513  while 1:
514  line = f.readline()
515  if not line:
516  break
517  if line[-1] == '\n':
518  line = line[:-1]
519  if line[:1] == '.':
520  line = '.' + line
521  self.putline(line)
522  self.putline('.')
523  return self.getresp()
def putcmd (   self,
  line 
)
Internal: send one command to the server (through putline()).

Definition at line 184 of file nntplib.py.

References NNTP.putline().

185  def putcmd(self, line):
186  """Internal: send one command to the server (through putline())."""
187  if self.debugging: print '*cmd*', `line`
188  self.putline(line)
def putline (   self,
  line 
)
Internal: send one line to the server, appending CRLF.

Definition at line 178 of file nntplib.py.

179  def putline(self, line):
180  """Internal: send one line to the server, appending CRLF."""
181  line = line + CRLF
182  if self.debugging > 1: print '*put*', `line`
183  self.sock.sendall(line)
def quit (   self)
Process a QUIT command and close the socket.  Returns:
- resp: server response if successful

Definition at line 548 of file nntplib.py.

References Chunk.file, FTP.file, NNTP.file, Hook.file, DumbWriter.file, MiniFieldStorage.file, Breakpoint.file, FieldStorage.file, HTTP.file, NNTP.shortcmd(), FTP.sock, NNTP.sock, HTTPConnection.sock, and HTTPSConnection.sock.

549  def quit(self):
550  """Process a QUIT command and close the socket. Returns:
551  - resp: server response if successful"""
552 
553  resp = self.shortcmd('QUIT')
554  self.file.close()
555  self.sock.close()
556  del self.file, self.sock
557  return resp
558 
def set_debuglevel (   self,
  level 
)
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

Definition at line 169 of file nntplib.py.

170  def set_debuglevel(self, level):
171  """Set the debugging level. Argument 'level' means:
172  0: no debugging output (default)
173  1: print commands and responses but not body text etc.
174  2: also print raw lines read and sent before stripping CR/LF"""
175 
self.debugging = level
def shortcmd (   self,
  line 
)
Internal: send a command and get the response.

Definition at line 245 of file nntplib.py.

246  def shortcmd(self, line):
247  """Internal: send a command and get the response."""
248  self.putcmd(line)
249  return self.getresp()
def slave (   self)
Process a SLAVE command.  Returns:
- resp: server response if successful

Definition at line 399 of file nntplib.py.

References NNTP.shortcmd().

400  def slave(self):
401  """Process a SLAVE command. Returns:
402  - resp: server response if successful"""
403 
404  return self.shortcmd('SLAVE')
def stat (   self,
  id 
)
Process a STAT command.  Argument:
- id: article number or message id
Returns:
- resp: server response if successful
- nr:   the article number
- id:   the article id

Definition at line 340 of file nntplib.py.

341  def stat(self, id):
342  """Process a STAT command. Argument:
343  - id: article number or message id
344  Returns:
345  - resp: server response if successful
346  - nr: the article number
347  - id: the article id"""
348 
349  return self.statcmd('STAT ' + id)
def statcmd (   self,
  line 
)
Internal: process a STAT, NEXT or LAST command.

Definition at line 335 of file nntplib.py.

336  def statcmd(self, line):
337  """Internal: process a STAT, NEXT or LAST command."""
338  resp = self.shortcmd(line)
339  return self.statparse(resp)
def statparse (   self,
  resp 
)
Internal: parse the response of a STAT, NEXT or LAST command.

Definition at line 321 of file nntplib.py.

322  def statparse(self, resp):
323  """Internal: parse the response of a STAT, NEXT or LAST command."""
324  if resp[:2] != '22':
325  raise NNTPReplyError(resp)
326  words = resp.split()
327  nr = 0
328  id = ''
329  n = len(words)
330  if n > 1:
331  nr = words[1]
332  if n > 2:
333  id = words[2]
334  return resp, nr, id
def xgtitle (   self,
  group 
)
Process an XGTITLE command (optional server extension) Arguments:
- group: group name wildcard (i.e. news.*)
Returns:
- resp: server response if successful
- list: list of (name,title) strings

Definition at line 448 of file nntplib.py.

References NNTP.longcmd().

449  def xgtitle(self, group):
450  """Process an XGTITLE command (optional server extension) Arguments:
451  - group: group name wildcard (i.e. news.*)
452  Returns:
453  - resp: server response if successful
454  - list: list of (name,title) strings"""
455 
456  line_pat = re.compile("^([^ \t]+)[ \t]+(.*)$")
457  resp, raw_lines = self.longcmd('XGTITLE ' + group)
458  lines = []
459  for raw_line in raw_lines:
460  match = line_pat.search(raw_line.strip())
461  if match:
462  lines.append(match.group(1, 2))
463  return resp, lines
def xhdr (   self,
  hdr,
  str 
)
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:
- resp: server response if successful
- list: list of (nr, value) strings

Definition at line 405 of file nntplib.py.

References NNTP.longcmd().

406  def xhdr(self, hdr, str):
407  """Process an XHDR command (optional server extension). Arguments:
408  - hdr: the header type (e.g. 'subject')
409  - str: an article nr, a message id, or a range nr1-nr2
410  Returns:
411  - resp: server response if successful
412  - list: list of (nr, value) strings"""
413 
414  pat = re.compile('^([0-9]+) ?(.*)\n?')
415  resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str)
416  for i in range(len(lines)):
417  line = lines[i]
418  m = pat.match(line)
419  if m:
420  lines[i] = m.group(1, 2)
421  return resp, lines
def xover (   self,
  start,
  end 
)
Process an XOVER command (optional server extension) Arguments:
- start: start of range
- end: end of range
Returns:
- resp: server response if successful
- list: list of (art-nr, subject, poster, date,
         id, references, size, lines)

Definition at line 422 of file nntplib.py.

References NNTP.longcmd(), and dospath.split().

423  def xover(self,start,end):
424  """Process an XOVER command (optional server extension) Arguments:
425  - start: start of range
426  - end: end of range
427  Returns:
428  - resp: server response if successful
429  - list: list of (art-nr, subject, poster, date,
430  id, references, size, lines)"""
431 
432  resp, lines = self.longcmd('XOVER ' + start + '-' + end)
433  xover_lines = []
434  for line in lines:
435  elem = line.split("\t")
436  try:
437  xover_lines.append((elem[0],
438  elem[1],
439  elem[2],
440  elem[3],
441  elem[4],
442  elem[5].split(),
443  elem[6],
444  elem[7]))
445  except IndexError:
446  raise NNTPDataError(line)
447  return resp,xover_lines
def xpath (   self,
  id 
)
Process an XPATH command (optional server extension) Arguments:
- id: Message id of article
Returns:
resp: server response if successful
path: directory path to article

Definition at line 464 of file nntplib.py.

References NNTP.shortcmd().

465  def xpath(self,id):
466  """Process an XPATH command (optional server extension) Arguments:
467  - id: Message id of article
468  Returns:
469  resp: server response if successful
470  path: directory path to article"""
471 
472  resp = self.shortcmd("XPATH " + id)
473  if resp[:3] != '223':
474  raise NNTPReplyError(resp)
475  try:
476  [resp_num, path] = resp.split()
477  except ValueError:
478  raise NNTPReplyError(resp)
479  else:
480  return resp, path

Field Documentation

debug = set_debuglevel
static

Definition at line 176 of file nntplib.py.

debugging

Definition at line 116 of file nntplib.py.

file

Definition at line 115 of file nntplib.py.

host

Definition at line 111 of file nntplib.py.

port

Definition at line 112 of file nntplib.py.

sock

Definition at line 113 of file nntplib.py.

welcome

Definition at line 117 of file nntplib.py.


The documentation for this class was generated from the following file: