From 8cb19eba4193cef13fb58d4ce427ce3ad7e1295f Mon Sep 17 00:00:00 2001 From: Felix Ruess Date: Fri, 6 Mar 2015 21:39:22 +0100 Subject: [PATCH] [bebop] telnet timeout and some pep8 cleanup --- sw/tools/parrot/bebop.py | 21 +++++++++++---------- sw/tools/parrot/parrot_utils.py | 4 ++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/sw/tools/parrot/bebop.py b/sw/tools/parrot/bebop.py index dee5c14f5b..c10f364eec 100755 --- a/sw/tools/parrot/bebop.py +++ b/sw/tools/parrot/bebop.py @@ -98,6 +98,7 @@ tn, ftp = parrot_utils.connect(args.host) # Check the Bebop status if args.command == 'status': + print("Connected to Bebop at " + args.host) bebop_status() # Reboot the drone @@ -107,12 +108,12 @@ elif args.command == 'reboot': # Kill a program elif args.command == 'kill': - parrot_utils.execute_command(tn,'killall -9 ' + args.program) + parrot_utils.execute_command(tn, 'killall -9 ' + args.program) print('Program "' + args.program + '" is now killed') # Start a program elif args.command == 'start': - parrot_utils.execute_command(tn,args.start + ' &') + parrot_utils.execute_command(tn, args.start + ' &') print('Program "' + args.start + '" is now started') @@ -120,7 +121,7 @@ elif args.command == 'insmod': modfile = parrot_utils.split_into_path_and_file(args.file) print('Uploading \'' + modfile[1]) parrot_utils.uploadfile(ftp, modfile[1], file(args.file, "rb")) - print(parrot_utils.execute_command(tn,"insmod /data/ftp/" + modfile[1])) + print(parrot_utils.execute_command(tn, "insmod /data/ftp/" + modfile[1])) elif args.command == 'upload_file_and_run': # Split filename and path @@ -141,7 +142,7 @@ elif args.command == 'upload_file': # Split filename and path f = parrot_utils.split_into_path_and_file(args.file) - parrot_utils.execute_command(tn,"mkdir -p /data/ftp/" + args.folder) + parrot_utils.execute_command(tn, "mkdir -p /data/ftp/" + args.folder) print('Uploading \'' + f[1] + "\' from " + f[0] + " to /data/video/" + args.folder) parrot_utils.uploadfile(ftp, args.folder + "/" + f[1], file(args.file, "rb")) print("#pragma message: Upload of " + f[1] + " to Bebop Succes!") @@ -151,9 +152,9 @@ elif args.command == 'download_file': f = parrot_utils.split_into_path_and_file(args.file) # Open file and download try: - file = open(args.file, 'wb') + fd = open(args.file, 'wb') print('Downloading \'' + f[1] + "\' from " + args.folder + " to " + f[0]) - ftp.retrbinary("RETR " + args.folder + "/" + f[1], file.write) + ftp.retrbinary("RETR " + args.folder + "/" + f[1], fd.write) print("#pragma message: Download of " + f[1] + " from Bebop Succes!") except IOError: print("#pragma message: Fail to open file " + args.file) @@ -161,7 +162,7 @@ elif args.command == 'download_file': os.remove(args.file) print("#pragma message: Download of " + f[1] + " from Bebop Failed!") else: - file.close() + fd.close() elif args.command == 'download_dir': # Split filename and path @@ -175,16 +176,16 @@ elif args.command == 'download_dir': file_source = args.folder + '/' + file_name[1] file_dest = args.dest + '/' + file_name[1] try: - file = open(file_dest, 'wb') + fd = open(file_dest, 'wb') print('Downloading \'' + f + "\' to " + file_dest) - ftp.retrbinary("RETR " + file_source, file.write) + ftp.retrbinary("RETR " + file_source, fd.write) except IOError: print("#pragma message: Fail to open file " + file_dest) except: os.remove(file_dest) print("#pragma message: Download of " + f + " from Bebop Failed!") else: - file.close() + fd.close() print("#pragma message: End download of folder " + args.folder + " from Bebop") elif args.command == 'rm_dir': diff --git a/sw/tools/parrot/parrot_utils.py b/sw/tools/parrot/parrot_utils.py index e62a147b7e..8fe50744f9 100644 --- a/sw/tools/parrot/parrot_utils.py +++ b/sw/tools/parrot/parrot_utils.py @@ -90,11 +90,11 @@ def uploadfile(ftp, filename, content): # Connect with telnet and ftp, wait until login def connect(host): try: - tn = telnetlib.Telnet(host) + tn = telnetlib.Telnet(host, timeout=3) ftp = FTP(host) ftp.login() tn.read_until('# ') - return (tn, ftp) + return tn, ftp except: print('Could not connect to Parrot UAV (host: ' + host + ')') exit(2)