From 023fec6f465caa4acfe6ab34effec0de25944c2c Mon Sep 17 00:00:00 2001 From: turboclear Date: Tue, 14 Mar 2017 21:25:44 -0400 Subject: [PATCH] servefiles.py - interactive mode, explicit interpreter This adds an interactive mode if no arguments are provided, prompting the user to enter the IP, file, etc., The 'help' display is instead shown if the standard 'help' flags are used (ex: --h, -h, --help, etc.,)t This also allows the user to specify the path to the interpreter. The original version assumed only the script name was used as an argument, and the default interpreter for the environment was being used, so it would fail on "C:\python27\python.exe servefiles.py" --- servefiles/servefiles.py | 63 +++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/servefiles/servefiles.py b/servefiles/servefiles.py index 549ef3a..f0ddbfc 100644 --- a/servefiles/servefiles.py +++ b/servefiles/servefiles.py @@ -20,26 +20,69 @@ except ImportError: from urllib.parse import urljoin, quote from urllib.request import pathname2url -if len(sys.argv) < 3 or len(sys.argv) > 5: +interactive = False + +if len(sys.argv) <= 2: + # If there aren't enough variables, use interactive mode + if len(sys.argv) == 2: + if sys.argv[1].lower() in ('--help', '-help', 'help', 'h', '-h', '--h'): + print('Usage: ' + sys.argv[0] + ' [host ip] [host port]') + sys.exit(1) + + interactive = True + +elif len(sys.argv) < 3 or len(sys.argv) > 6: print('Usage: ' + sys.argv[0] + ' [host ip] [host port]') sys.exit(1) accepted_extension = ('.cia', '.tik', '.cetk') -target_ip = sys.argv[1] -target_path = sys.argv[2] hostPort = 8080 # Default value +if interactive: + target_ip = raw_input("The IP of your 3DS: ") + target_path = raw_input("The file you want to send (.cia, .tik, or .cetk): ") + + hostIp = raw_input("Host IP (or press Enter to have the script detect host IP):") + if hostIp == '': + print('Detecting host IP...') + hostIp = [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] + else: + hostPort = raw_input("Host port (or press Enter to keep default, 8080):") + if hostPort == '': + hostPort = 8080 # Default + + +else: + # (if the script is being run using a full python path; ex: "path/to/python script_name.py foo foo..") + if sys.argv[1] == os.path.basename(__file__): + target_ip = sys.argv[2] + target_path = sys.argv[3] + + if len(sys.argv) >= 5: + hostIp = sys.argv[4] + if len(sys.argv) == 6: + hostPort = int(sys.argv[5]) + else: + print('Detecting host IP...') + hostIp = [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] + + # (if the script is being run using just the script name and default executable for python scripts; ex: "script_name.py foo foo..") + else: + target_ip = sys.argv[1] + target_path = sys.argv[2] + + if len(sys.argv) >= 4: + hostIp = sys.argv[3] + if len(sys.argv) == 5: + hostPort = int(sys.argv[4]) + else: + print('Detecting host IP...') + hostIp = [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] + if not os.path.exists(target_path): print(target_path + ': No such file or directory.') sys.exit(1) -if len(sys.argv) >= 4: - hostIp = sys.argv[3] - if len(sys.argv) == 5: - hostPort = int(sys.argv[4]) -else: - print('Detecting host IP...') - hostIp = [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1] print('Preparing data...') baseUrl = hostIp + ':' + str(hostPort) + '/'