<s> """<STR_LIT>""" <EOL> import re <EOL> import socket <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , ] <EOL> class NNTPError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> Exception . __init__ ( self , * args ) <EOL> try : <EOL> self . response = args [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> self . response = '<STR_LIT>' <EOL> class NNTPReplyError ( NNTPError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NNTPTemporaryError ( NNTPError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NNTPPermanentError ( NNTPError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NNTPProtocolError ( NNTPError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NNTPDataError ( NNTPError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> error_reply = NNTPReplyError <EOL> error_temp = NNTPTemporaryError <EOL> error_perm = NNTPPermanentError <EOL> error_proto = NNTPProtocolError <EOL> error_data = NNTPDataError <EOL> NNTP_PORT = <NUM_LIT> <EOL> LONGRESP = [ '<STR_LIT:100>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> CRLF = '<STR_LIT:\r\n>' <EOL> class NNTP : <EOL> def __init__ ( self , host , port = NNTP_PORT , user = None , password = None , <EOL> readermode = None , usenetrc = True ) : <EOL> """<STR_LIT>""" <EOL> self . host = host <EOL> self . port = port <EOL> self . sock = socket . create_connection ( ( host , port ) ) <EOL> self . file = self . sock . makefile ( '<STR_LIT:rb>' ) <EOL> self . debugging = <NUM_LIT:0> <EOL> self . welcome = self . getresp ( ) <EOL> readermode_afterauth = <NUM_LIT:0> <EOL> if readermode : <EOL> try : <EOL> self . welcome = self . shortcmd ( '<STR_LIT>' ) <EOL> except NNTPPermanentError : <EOL> pass <EOL> except NNTPTemporaryError , e : <EOL> if user and e . response [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> readermode_afterauth = <NUM_LIT:1> <EOL> else : <EOL> raise <EOL> try : <EOL> if usenetrc and not user : <EOL> import netrc <EOL> credentials = netrc . netrc ( ) <EOL> auth = credentials . authenticators ( host ) <EOL> if auth : <EOL> user = auth [ <NUM_LIT:0> ] <EOL> password = auth [ <NUM_LIT:2> ] <EOL> except IOError : <EOL> pass <EOL> if user : <EOL> resp = self . shortcmd ( '<STR_LIT>' + user ) <EOL> if resp [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> if not password : <EOL> raise NNTPReplyError ( resp ) <EOL> else : <EOL> resp = self . shortcmd ( <EOL> '<STR_LIT>' + password ) <EOL> if resp [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise NNTPPermanentError ( resp ) <EOL> if readermode_afterauth : <EOL> try : <EOL> self . welcome = self . shortcmd ( '<STR_LIT>' ) <EOL> except NNTPPermanentError : <EOL> pass <EOL> def getwelcome ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . debugging : print '<STR_LIT>' , repr ( self . welcome ) <EOL> return self . welcome <EOL> def set_debuglevel ( self , level ) : <EOL> """<STR_LIT>""" <EOL> self . debugging = level <EOL> debug = set_debuglevel <EOL> def putline ( self , line ) : <EOL> """<STR_LIT>""" <EOL> line = line + CRLF <EOL> if self . debugging > <NUM_LIT:1> : print '<STR_LIT>' , repr ( line ) <EOL> self . sock . sendall ( line ) <EOL> def putcmd ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if self . debugging : print '<STR_LIT>' , repr ( line ) <EOL> self . putline ( line ) <EOL> def getline ( self ) : <EOL> """<STR_LIT>""" <EOL> line = self . file . readline ( ) <EOL> if self . debugging > <NUM_LIT:1> : <EOL> print '<STR_LIT>' , repr ( line ) <EOL> if not line : raise EOFError <EOL> if line [ - <NUM_LIT:2> : ] == CRLF : line = line [ : - <NUM_LIT:2> ] <EOL> elif line [ - <NUM_LIT:1> : ] in CRLF : line = line [ : - <NUM_LIT:1> ] <EOL> return line <EOL> def getresp ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . getline ( ) <EOL> if self . debugging : print '<STR_LIT>' , repr ( resp ) <EOL> c = resp [ : <NUM_LIT:1> ] <EOL> if c == '<STR_LIT:4>' : <EOL> raise NNTPTemporaryError ( resp ) <EOL> if c == '<STR_LIT:5>' : <EOL> raise NNTPPermanentError ( resp ) <EOL> if c not in '<STR_LIT>' : <EOL> raise NNTPProtocolError ( resp ) <EOL> return resp <EOL> def getlongresp ( self , file = None ) : <EOL> """<STR_LIT>""" <EOL> openedFile = None <EOL> try : <EOL> if isinstance ( file , str ) : <EOL> openedFile = file = open ( file , "<STR_LIT:w>" ) <EOL> resp = self . getresp ( ) <EOL> if resp [ : <NUM_LIT:3> ] not in LONGRESP : <EOL> raise NNTPReplyError ( resp ) <EOL> list = [ ] <EOL> while <NUM_LIT:1> : <EOL> line = self . getline ( ) <EOL> if line == '<STR_LIT:.>' : <EOL> break <EOL> if line [ : <NUM_LIT:2> ] == '<STR_LIT:..>' : <EOL> line = line [ <NUM_LIT:1> : ] <EOL> if file : <EOL> file . write ( line + "<STR_LIT:\n>" ) <EOL> else : <EOL> list . append ( line ) <EOL> finally : <EOL> if openedFile : <EOL> openedFile . close ( ) <EOL> return resp , list <EOL> def shortcmd ( self , line ) : <EOL> """<STR_LIT>""" <EOL> self . putcmd ( line ) <EOL> return self . getresp ( ) <EOL> def longcmd ( self , line , file = None ) : <EOL> """<STR_LIT>""" <EOL> self . putcmd ( line ) <EOL> return self . getlongresp ( file ) <EOL> def newgroups ( self , date , time , file = None ) : <EOL> """<STR_LIT>""" <EOL> return self . longcmd ( '<STR_LIT>' + date + '<STR_LIT:U+0020>' + time , file ) <EOL> def newnews ( self , group , date , time , file = None ) : <EOL> """<STR_LIT>""" <EOL> cmd = '<STR_LIT>' + group + '<STR_LIT:U+0020>' + date + '<STR_LIT:U+0020>' + time <EOL> return self . longcmd ( cmd , file ) <EOL> def list ( self , file = None ) : <EOL> """<STR_LIT>""" <EOL> resp , list = self . longcmd ( '<STR_LIT>' , file ) <EOL> for i in range ( len ( list ) ) : <EOL> list [ i ] = tuple ( list [ i ] . split ( ) ) <EOL> return resp , list <EOL> def description ( self , group ) : <EOL> """<STR_LIT>""" <EOL> resp , lines = self . descriptions ( group ) <EOL> if len ( lines ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return lines [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> def descriptions ( self , group_pattern ) : <EOL> """<STR_LIT>""" <EOL> line_pat = re . compile ( "<STR_LIT>" ) <EOL> resp , raw_lines = self . longcmd ( '<STR_LIT>' + group_pattern ) <EOL> if resp [ : <NUM_LIT:3> ] != "<STR_LIT>" : <EOL> resp , raw_lines = self . longcmd ( '<STR_LIT>' + group_pattern ) <EOL> lines = [ ] <EOL> for raw_line in raw_lines : <EOL> match = line_pat . search ( raw_line . strip ( ) ) <EOL> if match : <EOL> lines . append ( match . group ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> return resp , lines <EOL> def group ( self , name ) : <EOL> """<STR_LIT>""" <EOL> resp = self . shortcmd ( '<STR_LIT>' + name ) <EOL> if resp [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise NNTPReplyError ( resp ) <EOL> words = resp . split ( ) <EOL> count = first = last = <NUM_LIT:0> <EOL> n = len ( words ) <EOL> if n > <NUM_LIT:1> : <EOL> count = words [ <NUM_LIT:1> ] <EOL> if n > <NUM_LIT:2> : <EOL> first = words [ <NUM_LIT:2> ] <EOL> if n > <NUM_LIT:3> : <EOL> last = words [ <NUM_LIT:3> ] <EOL> if n > <NUM_LIT:4> : <EOL> name = words [ <NUM_LIT:4> ] . lower ( ) <EOL> return resp , count , first , last , name <EOL> def help ( self , file = None ) : <EOL> """<STR_LIT>""" <EOL> return self . longcmd ( '<STR_LIT>' , file ) <EOL> def statparse ( self , resp ) : <EOL> """<STR_LIT>""" <EOL> if resp [ : <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise NNTPReplyError ( resp ) <EOL> words = resp . split ( ) <EOL> nr = <NUM_LIT:0> <EOL> id = '<STR_LIT>' <EOL> n = len ( words ) <EOL> if n > <NUM_LIT:1> : <EOL> nr = words [ <NUM_LIT:1> ] <EOL> if n > <NUM_LIT:2> : <EOL> id = words [ <NUM_LIT:2> ] <EOL> return resp , nr , id <EOL> def statcmd ( self , line ) : <EOL> """<STR_LIT>""" <EOL> resp = self . shortcmd ( line ) <EOL> return self . statparse ( resp ) <EOL> def stat ( self , id ) : <EOL> """<STR_LIT>""" <EOL> return self . statcmd ( '<STR_LIT>' + id ) <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . statcmd ( '<STR_LIT>' ) <EOL> def last ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . statcmd ( '<STR_LIT>' ) <EOL> def artcmd ( self , line , file = None ) : <EOL> """<STR_LIT>""" <EOL> resp , list = self . longcmd ( line , file ) <EOL> resp , nr , id = self . statparse ( resp ) <EOL> return resp , nr , id , list <EOL> def head ( self , id ) : <EOL> """<STR_LIT>""" <EOL> return self . artcmd ( '<STR_LIT>' + id ) <EOL> def body ( self , id , file = None ) : <EOL> """<STR_LIT>""" <EOL> return self . artcmd ( '<STR_LIT>' + id , file ) <EOL> def article ( self , id ) : <EOL> """<STR_LIT>""" <EOL> return self . artcmd ( '<STR_LIT>' + id ) <EOL> def slave ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . shortcmd ( '<STR_LIT>' ) <EOL> def xhdr ( self , hdr , str , file = None ) : <EOL> """<STR_LIT>""" <EOL> pat = re . compile ( '<STR_LIT>' ) <EOL> resp , lines = self . longcmd ( '<STR_LIT>' + hdr + '<STR_LIT:U+0020>' + str , file ) <EOL> for i in range ( len ( lines ) ) : <EOL> line = lines [ i ] <EOL> m = pat . match ( line ) <EOL> if m : <EOL> lines [ i ] = m . group ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> return resp , lines <EOL> def xover ( self , start , end , file = None ) : <EOL> """<STR_LIT>""" <EOL> resp , lines = self . longcmd ( '<STR_LIT>' + start + '<STR_LIT:->' + end , file ) <EOL> xover_lines = [ ] <EOL> for line in lines : <EOL> elem = line . split ( "<STR_LIT:\t>" ) <EOL> try : <EOL> xover_lines . append ( ( elem [ <NUM_LIT:0> ] , <EOL> elem [ <NUM_LIT:1> ] , <EOL> elem [ <NUM_LIT:2> ] , <EOL> elem [ <NUM_LIT:3> ] , <EOL> elem [ <NUM_LIT:4> ] , <EOL> elem [ <NUM_LIT:5> ] . split ( ) , <EOL> elem [ <NUM_LIT:6> ] , <EOL> elem [ <NUM_LIT:7> ] ) ) <EOL> except IndexError : <EOL> raise NNTPDataError ( line ) <EOL> return resp , xover_lines <EOL> def xgtitle ( self , group , file = None ) : <EOL> """<STR_LIT>""" <EOL> line_pat = re . compile ( "<STR_LIT>" ) <EOL> resp , raw_lines = self . longcmd ( '<STR_LIT>' + group , file ) <EOL> lines = [ ] <EOL> for raw_line in raw_lines : <EOL> match = line_pat . search ( raw_line . strip ( ) ) <EOL> if match : <EOL> lines . append ( match . group ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> return resp , lines <EOL> def xpath ( self , id ) : <EOL> """<STR_LIT>""" <EOL> resp = self . shortcmd ( "<STR_LIT>" + id ) <EOL> if resp [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise NNTPReplyError ( resp ) <EOL> try : <EOL> [ resp_num , path ] = resp . split ( ) <EOL> except ValueError : <EOL> raise NNTPReplyError ( resp ) <EOL> else : <EOL> return resp , path <EOL> def date ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . shortcmd ( "<STR_LIT>" ) <EOL> if resp [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise NNTPReplyError ( resp ) <EOL> elem = resp . split ( ) <EOL> if len ( elem ) != <NUM_LIT:2> : <EOL> raise NNTPDataError ( resp ) <EOL> date = elem [ <NUM_LIT:1> ] [ <NUM_LIT:2> : <NUM_LIT:8> ] <EOL> time = elem [ <NUM_LIT:1> ] [ - <NUM_LIT:6> : ] <EOL> if len ( date ) != <NUM_LIT:6> or len ( time ) != <NUM_LIT:6> : <EOL> raise NNTPDataError ( resp ) <EOL> return resp , date , time <EOL> def post ( self , f ) : <EOL> """<STR_LIT>""" <EOL> resp = self . shortcmd ( '<STR_LIT:POST>' ) <EOL> if resp [ <NUM_LIT:0> ] != '<STR_LIT:3>' : <EOL> raise NNTPReplyError ( resp ) <EOL> while <NUM_LIT:1> : <EOL> line = f . readline ( ) <EOL> if not line : <EOL> break <EOL> if line [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> if line [ : <NUM_LIT:1> ] == '<STR_LIT:.>' : <EOL> line = '<STR_LIT:.>' + line <EOL> self . putline ( line ) <EOL> self . putline ( '<STR_LIT:.>' ) <EOL> return self . getresp ( ) <EOL> def ihave ( self , id , f ) : <EOL> """<STR_LIT>""" <EOL> resp = self . shortcmd ( '<STR_LIT>' + id ) <EOL> if resp [ <NUM_LIT:0> ] != '<STR_LIT:3>' : <EOL> raise NNTPReplyError ( resp ) <EOL> while <NUM_LIT:1> : <EOL> line = f . readline ( ) <EOL> if not line : <EOL> break <EOL> if line [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> if line [ : <NUM_LIT:1> ] == '<STR_LIT:.>' : <EOL> line = '<STR_LIT:.>' + line <EOL> self . putline ( line ) <EOL> self . putline ( '<STR_LIT:.>' ) <EOL> return self . getresp ( ) <EOL> def quit ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . shortcmd ( '<STR_LIT>' ) <EOL> self . file . close ( ) <EOL> self . sock . close ( ) <EOL> del self . file , self . sock <EOL> return resp <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import os <EOL> newshost = '<STR_LIT>' and os . environ [ "<STR_LIT>" ] <EOL> if newshost . find ( '<STR_LIT:.>' ) == - <NUM_LIT:1> : <EOL> mode = '<STR_LIT>' <EOL> else : <EOL> mode = None <EOL> s = NNTP ( newshost , readermode = mode ) <EOL> resp , count , first , last , name = s . group ( '<STR_LIT>' ) <EOL> print resp <EOL> print '<STR_LIT>' , name , '<STR_LIT>' , count , '<STR_LIT>' , first , '<STR_LIT:to>' , last <EOL> resp , subs = s . xhdr ( '<STR_LIT>' , first + '<STR_LIT:->' + last ) <EOL> print resp <EOL> for item in subs : <EOL> print "<STR_LIT>" % item <EOL> resp = s . quit ( ) <EOL> print resp </s>
