4242import warnings
4343from socket import _GLOBAL_DEFAULT_TIMEOUT
4444
45- __all__ = ["FTP" , "Netrc" ]
45+ __all__ = ["FTP" ]
4646
4747# Magic number from <socket.h>
4848MSG_OOB = 0x1 # Process data out of band
@@ -920,115 +920,6 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
920920 target .voidresp ()
921921
922922
923- class Netrc :
924- """Class to parse & provide access to 'netrc' format files.
925-
926- See the netrc(4) man page for information on the file format.
927-
928- WARNING: This class is obsolete -- use module netrc instead.
929-
930- """
931- __defuser = None
932- __defpasswd = None
933- __defacct = None
934-
935- def __init__ (self , filename = None ):
936- warnings .warn ("This class is deprecated, use the netrc module instead" ,
937- DeprecationWarning , 2 )
938- if filename is None :
939- if "HOME" in os .environ :
940- filename = os .path .join (os .environ ["HOME" ],
941- ".netrc" )
942- else :
943- raise OSError ("specify file to load or set $HOME" )
944- self .__hosts = {}
945- self .__macros = {}
946- fp = open (filename , "r" )
947- in_macro = 0
948- while 1 :
949- line = fp .readline ()
950- if not line :
951- break
952- if in_macro and line .strip ():
953- macro_lines .append (line )
954- continue
955- elif in_macro :
956- self .__macros [macro_name ] = tuple (macro_lines )
957- in_macro = 0
958- words = line .split ()
959- host = user = passwd = acct = None
960- default = 0
961- i = 0
962- while i < len (words ):
963- w1 = words [i ]
964- if i + 1 < len (words ):
965- w2 = words [i + 1 ]
966- else :
967- w2 = None
968- if w1 == 'default' :
969- default = 1
970- elif w1 == 'machine' and w2 :
971- host = w2 .lower ()
972- i = i + 1
973- elif w1 == 'login' and w2 :
974- user = w2
975- i = i + 1
976- elif w1 == 'password' and w2 :
977- passwd = w2
978- i = i + 1
979- elif w1 == 'account' and w2 :
980- acct = w2
981- i = i + 1
982- elif w1 == 'macdef' and w2 :
983- macro_name = w2
984- macro_lines = []
985- in_macro = 1
986- break
987- i = i + 1
988- if default :
989- self .__defuser = user or self .__defuser
990- self .__defpasswd = passwd or self .__defpasswd
991- self .__defacct = acct or self .__defacct
992- if host :
993- if host in self .__hosts :
994- ouser , opasswd , oacct = \
995- self .__hosts [host ]
996- user = user or ouser
997- passwd = passwd or opasswd
998- acct = acct or oacct
999- self .__hosts [host ] = user , passwd , acct
1000- fp .close ()
1001-
1002- def get_hosts (self ):
1003- """Return a list of hosts mentioned in the .netrc file."""
1004- return self .__hosts .keys ()
1005-
1006- def get_account (self , host ):
1007- """Returns login information for the named host.
1008-
1009- The return value is a triple containing userid,
1010- password, and the accounting field.
1011-
1012- """
1013- host = host .lower ()
1014- user = passwd = acct = None
1015- if host in self .__hosts :
1016- user , passwd , acct = self .__hosts [host ]
1017- user = user or self .__defuser
1018- passwd = passwd or self .__defpasswd
1019- acct = acct or self .__defacct
1020- return user , passwd , acct
1021-
1022- def get_macros (self ):
1023- """Return a list of all defined macro names."""
1024- return self .__macros .keys ()
1025-
1026- def get_macro (self , macro ):
1027- """Return a sequence of lines which define a named macro."""
1028- return self .__macros [macro ]
1029-
1030-
1031-
1032923def test ():
1033924 '''Test program.
1034925 Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...
@@ -1042,6 +933,8 @@ def test():
1042933 print (test .__doc__ )
1043934 sys .exit (0 )
1044935
936+ import netrc
937+
1045938 debugging = 0
1046939 rcfile = None
1047940 while sys .argv [1 ] == '-d' :
@@ -1056,14 +949,14 @@ def test():
1056949 ftp .set_debuglevel (debugging )
1057950 userid = passwd = acct = ''
1058951 try :
1059- netrc = Netrc (rcfile )
952+ netrcobj = netrc . netrc (rcfile )
1060953 except OSError :
1061954 if rcfile is not None :
1062955 sys .stderr .write ("Could not open account file"
1063956 " -- using anonymous login." )
1064957 else :
1065958 try :
1066- userid , passwd , acct = netrc . get_account (host )
959+ userid , acct , passwd = netrcobj . authenticators (host )
1067960 except KeyError :
1068961 # no account for host
1069962 sys .stderr .write (
0 commit comments