Wrapped up the ~/.netrc support. This is basically just the changes Guido &
I discussed to the original version way-back-when.
This commit is contained in:
parent
dd527332b7
commit
475d51d7b2
|
@ -543,10 +543,12 @@ def ftpcp(source, sourcename, target, targetname = '', type = 'I'):
|
||||||
target.voidresp()
|
target.voidresp()
|
||||||
|
|
||||||
|
|
||||||
NoFileSpecified = "netrc.NoFileSpecified"
|
|
||||||
|
|
||||||
|
|
||||||
class Netrc:
|
class Netrc:
|
||||||
|
"""Class to parse & provide access to 'netrc' format files.
|
||||||
|
|
||||||
|
See the netrc(4) man page for information on the file format.
|
||||||
|
|
||||||
|
"""
|
||||||
__defuser = None
|
__defuser = None
|
||||||
__defpasswd = None
|
__defpasswd = None
|
||||||
__defacct = None
|
__defacct = None
|
||||||
|
@ -556,7 +558,7 @@ class Netrc:
|
||||||
if os.environ.has_key("HOME"):
|
if os.environ.has_key("HOME"):
|
||||||
filename = os.path.join(os.environ["HOME"], ".netrc")
|
filename = os.path.join(os.environ["HOME"], ".netrc")
|
||||||
else:
|
else:
|
||||||
raise NoFileSpecified, "specify file to load or set $HOME"
|
raise IOError, "specify file to load or set $HOME"
|
||||||
self.__hosts = {}
|
self.__hosts = {}
|
||||||
self.__macros = {}
|
self.__macros = {}
|
||||||
fp = open(filename, "r")
|
fp = open(filename, "r")
|
||||||
|
@ -625,7 +627,7 @@ class Netrc:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
host = string.lower(host)
|
host = string.lower(host)
|
||||||
user, passwd, acct = None, None, None
|
user = passwd = acct = None
|
||||||
if self.__hosts.has_key(host):
|
if self.__hosts.has_key(host):
|
||||||
user, passwd, acct = self.__hosts[host]
|
user, passwd, acct = self.__hosts[host]
|
||||||
user = user or self.__defuser
|
user = user or self.__defuser
|
||||||
|
@ -641,19 +643,38 @@ class Netrc:
|
||||||
"""Return a sequence of lines which define a named macro."""
|
"""Return a sequence of lines which define a named macro."""
|
||||||
return self.__macros[macro]
|
return self.__macros[macro]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
'''Test program.
|
'''Test program.
|
||||||
Usage: ftp [-d] host [-l[dir]] [-d[dir]] [-p] [file] ...'''
|
Usage: ftp [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ...'''
|
||||||
|
|
||||||
debugging = 0
|
debugging = 0
|
||||||
|
rcfile = None
|
||||||
while sys.argv[1] == '-d':
|
while sys.argv[1] == '-d':
|
||||||
debugging = debugging+1
|
debugging = debugging+1
|
||||||
del sys.argv[1]
|
del sys.argv[1]
|
||||||
|
if sys.argv[1][:2] == '-r':
|
||||||
|
# get name of alternate ~/.netrc file:
|
||||||
|
rcfile = sys.argv[1][2:]
|
||||||
|
del sys.argv[1]
|
||||||
host = sys.argv[1]
|
host = sys.argv[1]
|
||||||
ftp = FTP(host)
|
ftp = FTP(host)
|
||||||
ftp.set_debuglevel(debugging)
|
ftp.set_debuglevel(debugging)
|
||||||
ftp.login()
|
userid = passwd = acct = ''
|
||||||
|
try:
|
||||||
|
netrc = Netrc(rcfile)
|
||||||
|
except IOError:
|
||||||
|
if rcfile is not None:
|
||||||
|
sys.stderr.write("Could not open account file"
|
||||||
|
" -- using anonymous login.")
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
userid, passwd, acct = netrc.get_account(host)
|
||||||
|
except KeyError:
|
||||||
|
# no account for host
|
||||||
|
sys.stderr.write("No account -- using anonymous login.")
|
||||||
|
ftp.login(userid, passwd, acct)
|
||||||
for file in sys.argv[2:]:
|
for file in sys.argv[2:]:
|
||||||
if file[:2] == '-l':
|
if file[:2] == '-l':
|
||||||
ftp.dir(file[2:])
|
ftp.dir(file[2:])
|
||||||
|
|
Loading…
Reference in New Issue