<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> try : <EOL> import SOCKS ; socket = SOCKS ; del SOCKS <EOL> from socket import getfqdn ; socket . getfqdn = getfqdn ; del getfqdn <EOL> except ImportError : <EOL> import socket <EOL> from socket import _GLOBAL_DEFAULT_TIMEOUT <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> MSG_OOB = <NUM_LIT> <EOL> FTP_PORT = <NUM_LIT> <EOL> class Error ( Exception ) : pass <EOL> class error_reply ( Error ) : pass <EOL> class error_temp ( Error ) : pass <EOL> class error_perm ( Error ) : pass <EOL> class error_proto ( Error ) : pass <EOL> all_errors = ( Error , IOError , EOFError ) <EOL> CRLF = '<STR_LIT:\r\n>' <EOL> class FTP : <EOL> '''<STR_LIT>''' <EOL> debugging = <NUM_LIT:0> <EOL> host = '<STR_LIT>' <EOL> port = FTP_PORT <EOL> sock = None <EOL> file = None <EOL> welcome = None <EOL> passiveserver = <NUM_LIT:1> <EOL> def __init__ ( self , host = '<STR_LIT>' , user = '<STR_LIT>' , passwd = '<STR_LIT>' , acct = '<STR_LIT>' , <EOL> timeout = _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> self . timeout = timeout <EOL> if host : <EOL> self . connect ( host ) <EOL> if user : <EOL> self . login ( user , passwd , acct ) <EOL> def connect ( self , host = '<STR_LIT>' , port = <NUM_LIT:0> , timeout = - <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> if host != '<STR_LIT>' : <EOL> self . host = host <EOL> if port > <NUM_LIT:0> : <EOL> self . port = port <EOL> if timeout != - <NUM_LIT> : <EOL> self . timeout = timeout <EOL> self . sock = socket . create_connection ( ( self . host , self . port ) , self . timeout ) <EOL> self . af = self . sock . family <EOL> self . file = self . sock . makefile ( '<STR_LIT:rb>' ) <EOL> self . welcome = self . getresp ( ) <EOL> return self . welcome <EOL> def getwelcome ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . debugging : <EOL> print '<STR_LIT>' , self . sanitize ( 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 set_pasv ( self , val ) : <EOL> '''<STR_LIT>''' <EOL> self . passiveserver = val <EOL> def sanitize ( self , s ) : <EOL> if s [ : <NUM_LIT:5> ] == '<STR_LIT>' or s [ : <NUM_LIT:5> ] == '<STR_LIT>' : <EOL> i = len ( s ) <EOL> while i > <NUM_LIT:5> and s [ i - <NUM_LIT:1> ] in '<STR_LIT:\r\n>' : <EOL> i = i - <NUM_LIT:1> <EOL> s = s [ : <NUM_LIT:5> ] + '<STR_LIT:*>' * ( i - <NUM_LIT:5> ) + s [ i : ] <EOL> return repr ( s ) <EOL> def putline ( self , line ) : <EOL> line = line + CRLF <EOL> if self . debugging > <NUM_LIT:1> : print '<STR_LIT>' , self . sanitize ( line ) <EOL> self . sock . sendall ( line ) <EOL> def putcmd ( self , line ) : <EOL> if self . debugging : print '<STR_LIT>' , self . sanitize ( line ) <EOL> self . putline ( line ) <EOL> def getline ( self ) : <EOL> line = self . file . readline ( ) <EOL> if self . debugging > <NUM_LIT:1> : <EOL> print '<STR_LIT>' , self . sanitize ( 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 getmultiline ( self ) : <EOL> line = self . getline ( ) <EOL> if line [ <NUM_LIT:3> : <NUM_LIT:4> ] == '<STR_LIT:->' : <EOL> code = line [ : <NUM_LIT:3> ] <EOL> while <NUM_LIT:1> : <EOL> nextline = self . getline ( ) <EOL> line = line + ( '<STR_LIT:\n>' + nextline ) <EOL> if nextline [ : <NUM_LIT:3> ] == code and nextline [ <NUM_LIT:3> : <NUM_LIT:4> ] != '<STR_LIT:->' : <EOL> break <EOL> return line <EOL> def getresp ( self ) : <EOL> resp = self . getmultiline ( ) <EOL> if self . debugging : print '<STR_LIT>' , self . sanitize ( resp ) <EOL> self . lastresp = resp [ : <NUM_LIT:3> ] <EOL> c = resp [ : <NUM_LIT:1> ] <EOL> if c in ( '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' ) : <EOL> return resp <EOL> if c == '<STR_LIT:4>' : <EOL> raise error_temp , resp <EOL> if c == '<STR_LIT:5>' : <EOL> raise error_perm , resp <EOL> raise error_proto , resp <EOL> def voidresp ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . getresp ( ) <EOL> if resp [ : <NUM_LIT:1> ] != '<STR_LIT:2>' : <EOL> raise error_reply , resp <EOL> return resp <EOL> def abort ( self ) : <EOL> '''<STR_LIT>''' <EOL> line = '<STR_LIT>' + CRLF <EOL> if self . debugging > <NUM_LIT:1> : print '<STR_LIT>' , self . sanitize ( line ) <EOL> self . sock . sendall ( line , MSG_OOB ) <EOL> resp = self . getmultiline ( ) <EOL> if resp [ : <NUM_LIT:3> ] not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise error_proto , resp <EOL> def sendcmd ( self , cmd ) : <EOL> '''<STR_LIT>''' <EOL> self . putcmd ( cmd ) <EOL> return self . getresp ( ) <EOL> def voidcmd ( self , cmd ) : <EOL> """<STR_LIT>""" <EOL> self . putcmd ( cmd ) <EOL> return self . voidresp ( ) <EOL> def sendport ( self , host , port ) : <EOL> '''<STR_LIT>''' <EOL> hbytes = host . split ( '<STR_LIT:.>' ) <EOL> pbytes = [ repr ( port // <NUM_LIT> ) , repr ( port % <NUM_LIT> ) ] <EOL> bytes = hbytes + pbytes <EOL> cmd = '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( bytes ) <EOL> return self . voidcmd ( cmd ) <EOL> def sendeprt ( self , host , port ) : <EOL> '''<STR_LIT>''' <EOL> af = <NUM_LIT:0> <EOL> if self . af == socket . AF_INET : <EOL> af = <NUM_LIT:1> <EOL> if self . af == socket . AF_INET6 : <EOL> af = <NUM_LIT:2> <EOL> if af == <NUM_LIT:0> : <EOL> raise error_proto , '<STR_LIT>' <EOL> fields = [ '<STR_LIT>' , repr ( af ) , host , repr ( port ) , '<STR_LIT>' ] <EOL> cmd = '<STR_LIT>' + '<STR_LIT:|>' . join ( fields ) <EOL> return self . voidcmd ( cmd ) <EOL> def makeport ( self ) : <EOL> '''<STR_LIT>''' <EOL> err = None <EOL> sock = None <EOL> for res in socket . getaddrinfo ( None , <NUM_LIT:0> , self . af , socket . SOCK_STREAM , <NUM_LIT:0> , socket . AI_PASSIVE ) : <EOL> af , socktype , proto , canonname , sa = res <EOL> try : <EOL> sock = socket . socket ( af , socktype , proto ) <EOL> sock . bind ( sa ) <EOL> except socket . error , err : <EOL> if sock : <EOL> sock . close ( ) <EOL> sock = None <EOL> continue <EOL> break <EOL> if sock is None : <EOL> if err is not None : <EOL> raise err <EOL> else : <EOL> raise socket . error ( "<STR_LIT>" ) <EOL> sock . listen ( <NUM_LIT:1> ) <EOL> port = sock . getsockname ( ) [ <NUM_LIT:1> ] <EOL> host = self . sock . getsockname ( ) [ <NUM_LIT:0> ] <EOL> if self . af == socket . AF_INET : <EOL> resp = self . sendport ( host , port ) <EOL> else : <EOL> resp = self . sendeprt ( host , port ) <EOL> if self . timeout is not _GLOBAL_DEFAULT_TIMEOUT : <EOL> sock . settimeout ( self . timeout ) <EOL> return sock <EOL> def makepasv ( self ) : <EOL> if self . af == socket . AF_INET : <EOL> host , port = parse227 ( self . sendcmd ( '<STR_LIT>' ) ) <EOL> else : <EOL> host , port = parse229 ( self . sendcmd ( '<STR_LIT>' ) , self . sock . getpeername ( ) ) <EOL> return host , port <EOL> def ntransfercmd ( self , cmd , rest = None ) : <EOL> """<STR_LIT>""" <EOL> size = None <EOL> if self . passiveserver : <EOL> host , port = self . makepasv ( ) <EOL> conn = socket . create_connection ( ( host , port ) , self . timeout ) <EOL> try : <EOL> if rest is not None : <EOL> self . sendcmd ( "<STR_LIT>" % rest ) <EOL> resp = self . sendcmd ( cmd ) <EOL> if resp [ <NUM_LIT:0> ] == '<STR_LIT:2>' : <EOL> resp = self . getresp ( ) <EOL> if resp [ <NUM_LIT:0> ] != '<STR_LIT:1>' : <EOL> raise error_reply , resp <EOL> except : <EOL> conn . close ( ) <EOL> raise <EOL> else : <EOL> sock = self . makeport ( ) <EOL> try : <EOL> if rest is not None : <EOL> self . sendcmd ( "<STR_LIT>" % rest ) <EOL> resp = self . sendcmd ( cmd ) <EOL> if resp [ <NUM_LIT:0> ] == '<STR_LIT:2>' : <EOL> resp = self . getresp ( ) <EOL> if resp [ <NUM_LIT:0> ] != '<STR_LIT:1>' : <EOL> raise error_reply , resp <EOL> conn , sockaddr = sock . accept ( ) <EOL> if self . timeout is not _GLOBAL_DEFAULT_TIMEOUT : <EOL> conn . settimeout ( self . timeout ) <EOL> finally : <EOL> sock . close ( ) <EOL> if resp [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> size = parse150 ( resp ) <EOL> return conn , size <EOL> def transfercmd ( self , cmd , rest = None ) : <EOL> """<STR_LIT>""" <EOL> return self . ntransfercmd ( cmd , rest ) [ <NUM_LIT:0> ] <EOL> def login ( self , user = '<STR_LIT>' , passwd = '<STR_LIT>' , acct = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> if not user : user = '<STR_LIT>' <EOL> if not passwd : passwd = '<STR_LIT>' <EOL> if not acct : acct = '<STR_LIT>' <EOL> if user == '<STR_LIT>' and passwd in ( '<STR_LIT>' , '<STR_LIT:->' ) : <EOL> passwd = passwd + '<STR_LIT>' <EOL> resp = self . sendcmd ( '<STR_LIT>' + user ) <EOL> if resp [ <NUM_LIT:0> ] == '<STR_LIT:3>' : resp = self . sendcmd ( '<STR_LIT>' + passwd ) <EOL> if resp [ <NUM_LIT:0> ] == '<STR_LIT:3>' : resp = self . sendcmd ( '<STR_LIT>' + acct ) <EOL> if resp [ <NUM_LIT:0> ] != '<STR_LIT:2>' : <EOL> raise error_reply , resp <EOL> return resp <EOL> def retrbinary ( self , cmd , callback , blocksize = <NUM_LIT> , rest = None ) : <EOL> """<STR_LIT>""" <EOL> self . voidcmd ( '<STR_LIT>' ) <EOL> conn = self . transfercmd ( cmd , rest ) <EOL> while <NUM_LIT:1> : <EOL> data = conn . recv ( blocksize ) <EOL> if not data : <EOL> break <EOL> callback ( data ) <EOL> conn . close ( ) <EOL> return self . voidresp ( ) <EOL> def retrlines ( self , cmd , callback = None ) : <EOL> """<STR_LIT>""" <EOL> if callback is None : callback = print_line <EOL> resp = self . sendcmd ( '<STR_LIT>' ) <EOL> conn = self . transfercmd ( cmd ) <EOL> fp = conn . makefile ( '<STR_LIT:rb>' ) <EOL> while <NUM_LIT:1> : <EOL> line = fp . readline ( ) <EOL> if self . debugging > <NUM_LIT:2> : print '<STR_LIT>' , repr ( line ) <EOL> if not line : <EOL> break <EOL> if line [ - <NUM_LIT:2> : ] == CRLF : <EOL> line = line [ : - <NUM_LIT:2> ] <EOL> elif line [ - <NUM_LIT:1> : ] == '<STR_LIT:\n>' : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> callback ( line ) <EOL> fp . close ( ) <EOL> conn . close ( ) <EOL> return self . voidresp ( ) <EOL> def storbinary ( self , cmd , fp , blocksize = <NUM_LIT> , callback = None , rest = None ) : <EOL> """<STR_LIT>""" <EOL> self . voidcmd ( '<STR_LIT>' ) <EOL> conn = self . transfercmd ( cmd , rest ) <EOL> while <NUM_LIT:1> : <EOL> buf = fp . read ( blocksize ) <EOL> if not buf : break <EOL> conn . sendall ( buf ) <EOL> if callback : callback ( buf ) <EOL> conn . close ( ) <EOL> return self . voidresp ( ) <EOL> def storlines ( self , cmd , fp , callback = None ) : <EOL> """<STR_LIT>""" <EOL> self . voidcmd ( '<STR_LIT>' ) <EOL> conn = self . transfercmd ( cmd ) <EOL> while <NUM_LIT:1> : <EOL> buf = fp . readline ( ) <EOL> if not buf : break <EOL> if buf [ - <NUM_LIT:2> : ] != CRLF : <EOL> if buf [ - <NUM_LIT:1> ] in CRLF : buf = buf [ : - <NUM_LIT:1> ] <EOL> buf = buf + CRLF <EOL> conn . sendall ( buf ) <EOL> if callback : callback ( buf ) <EOL> conn . close ( ) <EOL> return self . voidresp ( ) <EOL> def acct ( self , password ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' + password <EOL> return self . voidcmd ( cmd ) <EOL> def nlst ( self , * args ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' <EOL> for arg in args : <EOL> cmd = cmd + ( '<STR_LIT:U+0020>' + arg ) <EOL> files = [ ] <EOL> self . retrlines ( cmd , files . append ) <EOL> return files <EOL> def dir ( self , * args ) : <EOL> '''<STR_LIT>''' <EOL> cmd = '<STR_LIT>' <EOL> func = None <EOL> if args [ - <NUM_LIT:1> : ] and type ( args [ - <NUM_LIT:1> ] ) != type ( '<STR_LIT>' ) : <EOL> args , func = args [ : - <NUM_LIT:1> ] , args [ - <NUM_LIT:1> ] <EOL> for arg in args : <EOL> if arg : <EOL> cmd = cmd + ( '<STR_LIT:U+0020>' + arg ) <EOL> self . retrlines ( cmd , func ) <EOL> def rename ( self , fromname , toname ) : <EOL> '''<STR_LIT>''' <EOL> resp = self . sendcmd ( '<STR_LIT>' + fromname ) <EOL> if resp [ <NUM_LIT:0> ] != '<STR_LIT:3>' : <EOL> raise error_reply , resp <EOL> return self . voidcmd ( '<STR_LIT>' + toname ) <EOL> def delete ( self , filename ) : <EOL> '''<STR_LIT>''' <EOL> resp = self . sendcmd ( '<STR_LIT>' + filename ) <EOL> if resp [ : <NUM_LIT:3> ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return resp <EOL> else : <EOL> raise error_reply , resp <EOL> def cwd ( self , dirname ) : <EOL> '''<STR_LIT>''' <EOL> if dirname == '<STR_LIT:..>' : <EOL> try : <EOL> return self . voidcmd ( '<STR_LIT>' ) <EOL> except error_perm , msg : <EOL> if msg . args [ <NUM_LIT:0> ] [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise <EOL> elif dirname == '<STR_LIT>' : <EOL> dirname = '<STR_LIT:.>' <EOL> cmd = '<STR_LIT>' + dirname <EOL> return self . voidcmd ( cmd ) <EOL> def size ( self , filename ) : <EOL> '''<STR_LIT>''' <EOL> resp = self . sendcmd ( '<STR_LIT>' + filename ) <EOL> if resp [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> s = resp [ <NUM_LIT:3> : ] . strip ( ) <EOL> try : <EOL> return int ( s ) <EOL> except ( OverflowError , ValueError ) : <EOL> return long ( s ) <EOL> def mkd ( self , dirname ) : <EOL> '''<STR_LIT>''' <EOL> resp = self . sendcmd ( '<STR_LIT>' + dirname ) <EOL> return parse257 ( resp ) <EOL> def rmd ( self , dirname ) : <EOL> '''<STR_LIT>''' <EOL> return self . voidcmd ( '<STR_LIT>' + dirname ) <EOL> def pwd ( self ) : <EOL> '''<STR_LIT>''' <EOL> resp = self . sendcmd ( '<STR_LIT>' ) <EOL> return parse257 ( resp ) <EOL> def quit ( self ) : <EOL> '''<STR_LIT>''' <EOL> resp = self . voidcmd ( '<STR_LIT>' ) <EOL> self . close ( ) <EOL> return resp <EOL> def close ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . file is not None : <EOL> self . file . close ( ) <EOL> if self . sock is not None : <EOL> self . sock . close ( ) <EOL> self . file = self . sock = None <EOL> try : <EOL> import ssl <EOL> ssl . PROTOCOL_TLSv1 <EOL> except ( ImportError , AttributeError ) : <EOL> pass <EOL> else : <EOL> class FTP_TLS ( FTP ) : <EOL> '''<STR_LIT>''' <EOL> ssl_version = ssl . PROTOCOL_TLSv1 <EOL> def __init__ ( self , host = '<STR_LIT>' , user = '<STR_LIT>' , passwd = '<STR_LIT>' , acct = '<STR_LIT>' , keyfile = None , <EOL> certfile = None , timeout = _GLOBAL_DEFAULT_TIMEOUT ) : <EOL> self . keyfile = keyfile <EOL> self . certfile = certfile <EOL> self . _prot_p = False <EOL> FTP . __init__ ( self , host , user , passwd , acct , timeout ) <EOL> def login ( self , user = '<STR_LIT>' , passwd = '<STR_LIT>' , acct = '<STR_LIT>' , secure = True ) : <EOL> if secure and not isinstance ( self . sock , ssl . SSLSocket ) : <EOL> self . auth ( ) <EOL> return FTP . login ( self , user , passwd , acct ) <EOL> def auth ( self ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( self . sock , ssl . SSLSocket ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . ssl_version == ssl . PROTOCOL_TLSv1 : <EOL> resp = self . voidcmd ( '<STR_LIT>' ) <EOL> else : <EOL> resp = self . voidcmd ( '<STR_LIT>' ) <EOL> self . sock = ssl . wrap_socket ( self . sock , self . keyfile , self . certfile , <EOL> ssl_version = self . ssl_version ) <EOL> self . file = self . sock . makefile ( mode = '<STR_LIT:rb>' ) <EOL> return resp <EOL> def prot_p ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . voidcmd ( '<STR_LIT>' ) <EOL> resp = self . voidcmd ( '<STR_LIT>' ) <EOL> self . _prot_p = True <EOL> return resp <EOL> def prot_c ( self ) : <EOL> '''<STR_LIT>''' <EOL> resp = self . voidcmd ( '<STR_LIT>' ) <EOL> self . _prot_p = False <EOL> return resp <EOL> def ntransfercmd ( self , cmd , rest = None ) : <EOL> conn , size = FTP . ntransfercmd ( self , cmd , rest ) <EOL> if self . _prot_p : <EOL> conn = ssl . wrap_socket ( conn , self . keyfile , self . certfile , <EOL> ssl_version = self . ssl_version ) <EOL> return conn , size <EOL> def retrbinary ( self , cmd , callback , blocksize = <NUM_LIT> , rest = None ) : <EOL> self . voidcmd ( '<STR_LIT>' ) <EOL> conn = self . transfercmd ( cmd , rest ) <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> data = conn . recv ( blocksize ) <EOL> if not data : <EOL> break <EOL> callback ( data ) <EOL> if isinstance ( conn , ssl . SSLSocket ) : <EOL> conn . unwrap ( ) <EOL> finally : <EOL> conn . close ( ) <EOL> return self . voidresp ( ) <EOL> def retrlines ( self , cmd , callback = None ) : <EOL> if callback is None : callback = print_line <EOL> resp = self . sendcmd ( '<STR_LIT>' ) <EOL> conn = self . transfercmd ( cmd ) <EOL> fp = conn . makefile ( '<STR_LIT:rb>' ) <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> line = fp . readline ( ) <EOL> if self . debugging > <NUM_LIT:2> : print '<STR_LIT>' , repr ( line ) <EOL> if not line : <EOL> break <EOL> if line [ - <NUM_LIT:2> : ] == CRLF : <EOL> line = line [ : - <NUM_LIT:2> ] <EOL> elif line [ - <NUM_LIT:1> : ] == '<STR_LIT:\n>' : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> callback ( line ) <EOL> if isinstance ( conn , ssl . SSLSocket ) : <EOL> conn . unwrap ( ) <EOL> finally : <EOL> fp . close ( ) <EOL> conn . close ( ) <EOL> return self . voidresp ( ) <EOL> def storbinary ( self , cmd , fp , blocksize = <NUM_LIT> , callback = None , rest = None ) : <EOL> self . voidcmd ( '<STR_LIT>' ) <EOL> conn = self . transfercmd ( cmd , rest ) <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> buf = fp . read ( blocksize ) <EOL> if not buf : break <EOL> conn . sendall ( buf ) <EOL> if callback : callback ( buf ) <EOL> if isinstance ( conn , ssl . SSLSocket ) : <EOL> conn . unwrap ( ) <EOL> finally : <EOL> conn . close ( ) <EOL> return self . voidresp ( ) <EOL> def storlines ( self , cmd , fp , callback = None ) : <EOL> self . voidcmd ( '<STR_LIT>' ) <EOL> conn = self . transfercmd ( cmd ) <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> buf = fp . readline ( ) <EOL> if not buf : break <EOL> if buf [ - <NUM_LIT:2> : ] != CRLF : <EOL> if buf [ - <NUM_LIT:1> ] in CRLF : buf = buf [ : - <NUM_LIT:1> ] <EOL> buf = buf + CRLF <EOL> conn . sendall ( buf ) <EOL> if callback : callback ( buf ) <EOL> if isinstance ( conn , ssl . SSLSocket ) : <EOL> conn . unwrap ( ) <EOL> finally : <EOL> conn . close ( ) <EOL> return self . voidresp ( ) <EOL> __all__ . append ( '<STR_LIT>' ) <EOL> all_errors = ( Error , IOError , EOFError , ssl . SSLError ) <EOL> _150_re = None <EOL> def parse150 ( resp ) : <EOL> '''<STR_LIT>''' <EOL> if resp [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise error_reply , resp <EOL> global _150_re <EOL> if _150_re is None : <EOL> import re <EOL> _150_re = re . compile ( "<STR_LIT>" , re . IGNORECASE ) <EOL> m = _150_re . match ( resp ) <EOL> if not m : <EOL> return None <EOL> s = m . group ( <NUM_LIT:1> ) <EOL> try : <EOL> return int ( s ) <EOL> except ( OverflowError , ValueError ) : <EOL> return long ( s ) <EOL> _227_re = None <EOL> def parse227 ( resp ) : <EOL> '''<STR_LIT>''' <EOL> if resp [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise error_reply , resp <EOL> global _227_re <EOL> if _227_re is None : <EOL> import re <EOL> _227_re = re . compile ( r'<STR_LIT>' ) <EOL> m = _227_re . search ( resp ) <EOL> if not m : <EOL> raise error_proto , resp <EOL> numbers = m . groups ( ) <EOL> host = '<STR_LIT:.>' . join ( numbers [ : <NUM_LIT:4> ] ) <EOL> port = ( int ( numbers [ <NUM_LIT:4> ] ) << <NUM_LIT:8> ) + int ( numbers [ <NUM_LIT:5> ] ) <EOL> return host , port <EOL> def parse229 ( resp , peer ) : <EOL> '''<STR_LIT>''' <EOL> if resp [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise error_reply , resp <EOL> left = resp . find ( '<STR_LIT:(>' ) <EOL> if left < <NUM_LIT:0> : raise error_proto , resp <EOL> right = resp . find ( '<STR_LIT:)>' , left + <NUM_LIT:1> ) <EOL> if right < <NUM_LIT:0> : <EOL> raise error_proto , resp <EOL> if resp [ left + <NUM_LIT:1> ] != resp [ right - <NUM_LIT:1> ] : <EOL> raise error_proto , resp <EOL> parts = resp [ left + <NUM_LIT:1> : right ] . split ( resp [ left + <NUM_LIT:1> ] ) <EOL> if len ( parts ) != <NUM_LIT:5> : <EOL> raise error_proto , resp <EOL> host = peer [ <NUM_LIT:0> ] <EOL> port = int ( parts [ <NUM_LIT:3> ] ) <EOL> return host , port <EOL> def parse257 ( resp ) : <EOL> '''<STR_LIT>''' <EOL> if resp [ : <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise error_reply , resp <EOL> if resp [ <NUM_LIT:3> : <NUM_LIT:5> ] != '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> dirname = '<STR_LIT>' <EOL> i = <NUM_LIT:5> <EOL> n = len ( resp ) <EOL> while i < n : <EOL> c = resp [ i ] <EOL> i = i + <NUM_LIT:1> <EOL> if c == '<STR_LIT:">' : <EOL> if i >= n or resp [ i ] != '<STR_LIT:">' : <EOL> break <EOL> i = i + <NUM_LIT:1> <EOL> dirname = dirname + c <EOL> return dirname <EOL> def print_line ( line ) : <EOL> '''<STR_LIT>''' <EOL> print line <EOL> def ftpcp ( source , sourcename , target , targetname = '<STR_LIT>' , type = '<STR_LIT:I>' ) : <EOL> '''<STR_LIT>''' <EOL> if not targetname : targetname = sourcename <EOL> type = '<STR_LIT>' + type <EOL> source . voidcmd ( type ) <EOL> target . voidcmd ( type ) <EOL> sourcehost , sourceport = parse227 ( source . sendcmd ( '<STR_LIT>' ) ) <EOL> target . sendport ( sourcehost , sourceport ) <EOL> treply = target . sendcmd ( '<STR_LIT>' + targetname ) <EOL> if treply [ : <NUM_LIT:3> ] not in ( '<STR_LIT>' , '<STR_LIT>' ) : raise error_proto <EOL> sreply = source . sendcmd ( '<STR_LIT>' + sourcename ) <EOL> if sreply [ : <NUM_LIT:3> ] not in ( '<STR_LIT>' , '<STR_LIT>' ) : raise error_proto <EOL> source . voidresp ( ) <EOL> target . voidresp ( ) <EOL> class Netrc : <EOL> """<STR_LIT>""" <EOL> __defuser = None <EOL> __defpasswd = None <EOL> __defacct = None <EOL> def __init__ ( self , filename = None ) : <EOL> if filename is None : <EOL> if "<STR_LIT>" in os . environ : <EOL> filename = os . path . join ( os . environ [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> raise IOError , "<STR_LIT>" <EOL> self . __hosts = { } <EOL> self . __macros = { } <EOL> fp = open ( filename , "<STR_LIT:r>" ) <EOL> in_macro = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> line = fp . readline ( ) <EOL> if not line : break <EOL> if in_macro and line . strip ( ) : <EOL> macro_lines . append ( line ) <EOL> continue <EOL> elif in_macro : <EOL> self . __macros [ macro_name ] = tuple ( macro_lines ) <EOL> in_macro = <NUM_LIT:0> <EOL> words = line . split ( ) <EOL> host = user = passwd = acct = None <EOL> default = <NUM_LIT:0> <EOL> i = <NUM_LIT:0> <EOL> while i < len ( words ) : <EOL> w1 = words [ i ] <EOL> if i + <NUM_LIT:1> < len ( words ) : <EOL> w2 = words [ i + <NUM_LIT:1> ] <EOL> else : <EOL> w2 = None <EOL> if w1 == '<STR_LIT:default>' : <EOL> default = <NUM_LIT:1> <EOL> elif w1 == '<STR_LIT>' and w2 : <EOL> host = w2 . lower ( ) <EOL> i = i + <NUM_LIT:1> <EOL> elif w1 == '<STR_LIT>' and w2 : <EOL> user = w2 <EOL> i = i + <NUM_LIT:1> <EOL> elif w1 == '<STR_LIT:password>' and w2 : <EOL> passwd = w2 <EOL> i = i + <NUM_LIT:1> <EOL> elif w1 == '<STR_LIT>' and w2 : <EOL> acct = w2 <EOL> i = i + <NUM_LIT:1> <EOL> elif w1 == '<STR_LIT>' and w2 : <EOL> macro_name = w2 <EOL> macro_lines = [ ] <EOL> in_macro = <NUM_LIT:1> <EOL> break <EOL> i = i + <NUM_LIT:1> <EOL> if default : <EOL> self . __defuser = user or self . __defuser <EOL> self . __defpasswd = passwd or self . __defpasswd <EOL> self . __defacct = acct or self . __defacct <EOL> if host : <EOL> if host in self . __hosts : <EOL> ouser , opasswd , oacct = self . __hosts [ host ] <EOL> user = user or ouser <EOL> passwd = passwd or opasswd <EOL> acct = acct or oacct <EOL> self . __hosts [ host ] = user , passwd , acct <EOL> fp . close ( ) <EOL> def get_hosts ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __hosts . keys ( ) <EOL> def get_account ( self , host ) : <EOL> """<STR_LIT>""" <EOL> host = host . lower ( ) <EOL> user = passwd = acct = None <EOL> if host in self . __hosts : <EOL> user , passwd , acct = self . __hosts [ host ] <EOL> user = user or self . __defuser <EOL> passwd = passwd or self . __defpasswd <EOL> acct = acct or self . __defacct <EOL> return user , passwd , acct <EOL> def get_macros ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __macros . keys ( ) <EOL> def get_macro ( self , macro ) : <EOL> """<STR_LIT>""" <EOL> return self . __macros [ macro ] <EOL> def test ( ) : <EOL> '''<STR_LIT>''' <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print test . __doc__ <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> debugging = <NUM_LIT:0> <EOL> rcfile = None <EOL> while sys . argv [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> debugging = debugging + <NUM_LIT:1> <EOL> del sys . argv [ <NUM_LIT:1> ] <EOL> if sys . argv [ <NUM_LIT:1> ] [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> rcfile = sys . argv [ <NUM_LIT:1> ] [ <NUM_LIT:2> : ] <EOL> del sys . argv [ <NUM_LIT:1> ] <EOL> host = sys . argv [ <NUM_LIT:1> ] <EOL> ftp = FTP ( host ) <EOL> ftp . set_debuglevel ( debugging ) <EOL> userid = passwd = acct = '<STR_LIT>' <EOL> try : <EOL> netrc = Netrc ( rcfile ) <EOL> except IOError : <EOL> if rcfile is not None : <EOL> sys . stderr . write ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> try : <EOL> userid , passwd , acct = netrc . get_account ( host ) <EOL> except KeyError : <EOL> sys . stderr . write ( <EOL> "<STR_LIT>" ) <EOL> ftp . login ( userid , passwd , acct ) <EOL> for file in sys . argv [ <NUM_LIT:2> : ] : <EOL> if file [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> ftp . dir ( file [ <NUM_LIT:2> : ] ) <EOL> elif file [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> cmd = '<STR_LIT>' <EOL> if file [ <NUM_LIT:2> : ] : cmd = cmd + '<STR_LIT:U+0020>' + file [ <NUM_LIT:2> : ] <EOL> resp = ftp . sendcmd ( cmd ) <EOL> elif file == '<STR_LIT>' : <EOL> ftp . set_pasv ( not ftp . passiveserver ) <EOL> else : <EOL> ftp . retrbinary ( '<STR_LIT>' + file , sys . stdout . write , <NUM_LIT> ) <EOL> ftp . quit ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
