Test program. Usage: ftplib [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ... Options: -d increase debugging level -r[file] set alternate ~/.netrc file Commands: -l[dir] list directory -d[dir] change the current directory -p toggl
()
| 899 | |
| 900 | |
| 901 | def test(): |
| 902 | '''Test program. |
| 903 | Usage: ftplib [-d] [-r[file]] host [-l[dir]] [-d[dir]] [-p] [file] ... |
| 904 | |
| 905 | Options: |
| 906 | -d increase debugging level |
| 907 | -r[file] set alternate ~/.netrc file |
| 908 | |
| 909 | Commands: |
| 910 | -l[dir] list directory |
| 911 | -d[dir] change the current directory |
| 912 | -p toggle passive and active mode |
| 913 | file retrieve the file and write it to stdout |
| 914 | ''' |
| 915 | |
| 916 | if len(sys.argv) < 2: |
| 917 | print(test.__doc__) |
| 918 | sys.exit(0) |
| 919 | |
| 920 | import netrc |
| 921 | |
| 922 | debugging = 0 |
| 923 | rcfile = None |
| 924 | while sys.argv[1] == '-d': |
| 925 | debugging = debugging+1 |
| 926 | del sys.argv[1] |
| 927 | if sys.argv[1][:2] == '-r': |
| 928 | # get name of alternate ~/.netrc file: |
| 929 | rcfile = sys.argv[1][2:] |
| 930 | del sys.argv[1] |
| 931 | host = sys.argv[1] |
| 932 | ftp = FTP(host) |
| 933 | ftp.set_debuglevel(debugging) |
| 934 | userid = passwd = acct = '' |
| 935 | try: |
| 936 | netrcobj = netrc.netrc(rcfile) |
| 937 | except OSError: |
| 938 | if rcfile is not None: |
| 939 | print("Could not open account file -- using anonymous login.", |
| 940 | file=sys.stderr) |
| 941 | else: |
| 942 | try: |
| 943 | userid, acct, passwd = netrcobj.authenticators(host) |
| 944 | except (KeyError, TypeError): |
| 945 | # no account for host |
| 946 | print("No account -- using anonymous login.", file=sys.stderr) |
| 947 | ftp.login(userid, passwd, acct) |
| 948 | for file in sys.argv[2:]: |
| 949 | if file[:2] == '-l': |
| 950 | ftp.dir(file[2:]) |
| 951 | elif file[:2] == '-d': |
| 952 | cmd = 'CWD' |
| 953 | if file[2:]: cmd = cmd + ' ' + file[2:] |
| 954 | ftp.sendcmd(cmd) |
| 955 | elif file == '-p': |
| 956 | ftp.set_pasv(not ftp.passiveserver) |
| 957 | else: |
| 958 | ftp.retrbinary('RETR ' + file, \ |
no test coverage detected
searching dependent graphs…