<s> """<STR_LIT>""" <EOL> import re , socket <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class error_proto ( Exception ) : pass <EOL> POP3_PORT = <NUM_LIT> <EOL> POP3_SSL_PORT = <NUM_LIT> <EOL> CR = '<STR_LIT:\r>' <EOL> LF = '<STR_LIT:\n>' <EOL> CRLF = CR + LF <EOL> class POP3 : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port = POP3_PORT , <EOL> timeout = socket . _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . sock = socket . create_connection ( ( host , port ) , timeout ) <EOL> self . file = self . sock . makefile ( '<STR_LIT:rb>' ) <EOL> self . _debugging = <NUM_LIT:0> <EOL> self . welcome = self . _getresp ( ) <EOL> def _putline ( self , line ) : <EOL> if self . _debugging > <NUM_LIT:1> : print '<STR_LIT>' , repr ( line ) <EOL> self . sock . sendall ( '<STR_LIT>' % ( line , CRLF ) ) <EOL> def _putcmd ( self , line ) : <EOL> if self . _debugging : print '<STR_LIT>' , repr ( line ) <EOL> self . _putline ( line ) <EOL> def _getline ( self ) : <EOL> line = self . file . readline ( ) <EOL> if self . _debugging > <NUM_LIT:1> : print '<STR_LIT>' , repr ( line ) <EOL> if not line : raise error_proto ( '<STR_LIT>' ) <EOL> octets = len ( line ) <EOL> if line [ - <NUM_LIT:2> : ] == CRLF : <EOL> return line [ : - <NUM_LIT:2> ] , octets <EOL> if line [ <NUM_LIT:0> ] == CR : <EOL> return line [ <NUM_LIT:1> : - <NUM_LIT:1> ] , octets <EOL> return line [ : - <NUM_LIT:1> ] , octets <EOL> def _getresp ( self ) : <EOL> resp , o = self . _getline ( ) <EOL> if self . _debugging > <NUM_LIT:1> : print '<STR_LIT>' , repr ( resp ) <EOL> c = resp [ : <NUM_LIT:1> ] <EOL> if c != '<STR_LIT:+>' : <EOL> raise error_proto ( resp ) <EOL> return resp <EOL> def _getlongresp ( self ) : <EOL> resp = self . _getresp ( ) <EOL> list = [ ] ; octets = <NUM_LIT:0> <EOL> line , o = self . _getline ( ) <EOL> while line != '<STR_LIT:.>' : <EOL> if line [ : <NUM_LIT:2> ] == '<STR_LIT:..>' : <EOL> o = o - <NUM_LIT:1> <EOL> line = line [ <NUM_LIT:1> : ] <EOL> octets = octets + o <EOL> list . append ( line ) <EOL> line , o = self . _getline ( ) <EOL> return resp , list , octets <EOL> def _shortcmd ( self , line ) : <EOL> self . _putcmd ( line ) <EOL> return self . _getresp ( ) <EOL> def _longcmd ( self , line ) : <EOL> self . _putcmd ( line ) <EOL> return self . _getlongresp ( ) <EOL> def getwelcome ( self ) : <EOL> return self . welcome <EOL> def set_debuglevel ( self , level ) : <EOL> self . _debugging = level <EOL> def user ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return self . _shortcmd ( '<STR_LIT>' % user ) <EOL> def pass_ ( self , pswd ) : <EOL> """<STR_LIT>""" <EOL> return self . _shortcmd ( '<STR_LIT>' % pswd ) <EOL> def stat ( self ) : <EOL> """<STR_LIT>""" <EOL> retval = self . _shortcmd ( '<STR_LIT>' ) <EOL> rets = retval . split ( ) <EOL> if self . _debugging : print '<STR_LIT>' , repr ( rets ) <EOL> numMessages = int ( rets [ <NUM_LIT:1> ] ) <EOL> sizeMessages = int ( rets [ <NUM_LIT:2> ] ) <EOL> return ( numMessages , sizeMessages ) <EOL> def list ( self , which = None ) : <EOL> """<STR_LIT>""" <EOL> if which is not None : <EOL> return self . _shortcmd ( '<STR_LIT>' % which ) <EOL> return self . _longcmd ( '<STR_LIT>' ) <EOL> def retr ( self , which ) : <EOL> """<STR_LIT>""" <EOL> return self . _longcmd ( '<STR_LIT>' % which ) <EOL> def dele ( self , which ) : <EOL> """<STR_LIT>""" <EOL> return self . _shortcmd ( '<STR_LIT>' % which ) <EOL> def noop ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _shortcmd ( '<STR_LIT>' ) <EOL> def rset ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _shortcmd ( '<STR_LIT>' ) <EOL> def quit ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> resp = self . _shortcmd ( '<STR_LIT>' ) <EOL> except error_proto , val : <EOL> resp = val <EOL> self . file . close ( ) <EOL> self . sock . close ( ) <EOL> del self . file , self . sock <EOL> return resp <EOL> def rpop ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return self . _shortcmd ( '<STR_LIT>' % user ) <EOL> timestamp = re . compile ( r'<STR_LIT>' ) <EOL> def apop ( self , user , secret ) : <EOL> """<STR_LIT>""" <EOL> m = self . timestamp . match ( self . welcome ) <EOL> if not m : <EOL> raise error_proto ( '<STR_LIT>' ) <EOL> import hashlib <EOL> digest = hashlib . md5 ( m . group ( <NUM_LIT:1> ) + secret ) . digest ( ) <EOL> digest = '<STR_LIT>' . join ( map ( lambda x : '<STR_LIT>' % ord ( x ) , digest ) ) <EOL> return self . _shortcmd ( '<STR_LIT>' % ( user , digest ) ) <EOL> def top ( self , which , howmuch ) : <EOL> """<STR_LIT>""" <EOL> return self . _longcmd ( '<STR_LIT>' % ( which , howmuch ) ) <EOL> def uidl ( self , which = None ) : <EOL> """<STR_LIT>""" <EOL> if which is not None : <EOL> return self . _shortcmd ( '<STR_LIT>' % which ) <EOL> return self . _longcmd ( '<STR_LIT>' ) <EOL> try : <EOL> import ssl <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> class POP3_SSL ( POP3 ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port = POP3_SSL_PORT , keyfile = None , certfile = None ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . keyfile = keyfile <EOL> self . certfile = certfile <EOL> self . buffer = "<STR_LIT>" <EOL> msg = "<STR_LIT>" <EOL> self . sock = None <EOL> for res in socket . getaddrinfo ( self . host , self . port , <NUM_LIT:0> , socket . SOCK_STREAM ) : <EOL> af , socktype , proto , canonname , sa = res <EOL> try : <EOL> self . sock = socket . socket ( af , socktype , proto ) <EOL> self . sock . connect ( sa ) <EOL> except socket . error , msg : <EOL> if self . sock : <EOL> self . sock . close ( ) <EOL> self . sock = None <EOL> continue <EOL> break <EOL> if not self . sock : <EOL> raise socket . error , msg <EOL> self . file = self . sock . makefile ( '<STR_LIT:rb>' ) <EOL> self . sslobj = ssl . wrap_socket ( self . sock , self . keyfile , self . certfile ) <EOL> self . _debugging = <NUM_LIT:0> <EOL> self . welcome = self . _getresp ( ) <EOL> def _fillBuffer ( self ) : <EOL> localbuf = self . sslobj . read ( ) <EOL> if len ( localbuf ) == <NUM_LIT:0> : <EOL> raise error_proto ( '<STR_LIT>' ) <EOL> self . buffer += localbuf <EOL> def _getline ( self ) : <EOL> line = "<STR_LIT>" <EOL> renewline = re . compile ( r'<STR_LIT>' ) <EOL> match = renewline . match ( self . buffer ) <EOL> while not match : <EOL> self . _fillBuffer ( ) <EOL> match = renewline . match ( self . buffer ) <EOL> line = match . group ( <NUM_LIT:0> ) <EOL> self . buffer = renewline . sub ( '<STR_LIT>' , self . buffer , <NUM_LIT:1> ) <EOL> if self . _debugging > <NUM_LIT:1> : print '<STR_LIT>' , repr ( line ) <EOL> octets = len ( line ) <EOL> if line [ - <NUM_LIT:2> : ] == CRLF : <EOL> return line [ : - <NUM_LIT:2> ] , octets <EOL> if line [ <NUM_LIT:0> ] == CR : <EOL> return line [ <NUM_LIT:1> : - <NUM_LIT:1> ] , octets <EOL> return line [ : - <NUM_LIT:1> ] , octets <EOL> def _putline ( self , line ) : <EOL> if self . _debugging > <NUM_LIT:1> : print '<STR_LIT>' , repr ( line ) <EOL> line += CRLF <EOL> bytes = len ( line ) <EOL> while bytes > <NUM_LIT:0> : <EOL> sent = self . sslobj . write ( line ) <EOL> if sent == bytes : <EOL> break <EOL> line = line [ sent : ] <EOL> bytes = bytes - sent <EOL> def quit ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> resp = self . _shortcmd ( '<STR_LIT>' ) <EOL> except error_proto , val : <EOL> resp = val <EOL> self . sock . close ( ) <EOL> del self . sslobj , self . sock <EOL> return resp <EOL> __all__ . append ( "<STR_LIT>" ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> a = POP3 ( sys . argv [ <NUM_LIT:1> ] ) <EOL> print a . getwelcome ( ) <EOL> a . user ( sys . argv [ <NUM_LIT:2> ] ) <EOL> a . pass_ ( sys . argv [ <NUM_LIT:3> ] ) <EOL> a . list ( ) <EOL> ( numMsgs , totalSize ) = a . stat ( ) <EOL> for i in range ( <NUM_LIT:1> , numMsgs + <NUM_LIT:1> ) : <EOL> ( header , msg , octets ) = a . retr ( i ) <EOL> print "<STR_LIT>" % i <EOL> for line in msg : <EOL> print '<STR_LIT:U+0020>' + line <EOL> print '<STR_LIT>' <EOL> a . quit ( ) </s>
