| 1 | #!/usr/bin/env python |
| 2 | """\ |
| 3 | Upload a file to Smoothie over the network |
| 4 | """ |
| 5 | |
| 6 | from __future__ import print_function |
| 7 | import sys |
| 8 | import argparse |
| 9 | import socket |
| 10 | import os |
| 11 | # Define command line argument interface |
| 12 | parser = argparse.ArgumentParser(description='Upload a file to Smoothie over network.') |
| 13 | parser.add_argument('file', type=argparse.FileType('r'), |
| 14 | help='filename to be uploaded') |
| 15 | parser.add_argument('ipaddr', |
| 16 | help='Smoothie IP address') |
| 17 | parser.add_argument('-v','--verbose',action='store_true', |
| 18 | help='Show data being uploaded') |
| 19 | parser.add_argument('-o','--output', |
| 20 | help='Set output filename') |
| 21 | parser.add_argument('-q','--quiet',action='store_true', |
| 22 | help='suppress all output to terminal') |
| 23 | |
| 24 | args = parser.parse_args() |
| 25 | |
| 26 | f = args.file |
| 27 | verbose = args.verbose |
| 28 | output = args.output |
| 29 | if output == None : |
| 30 | output= args.file.name |
| 31 | |
| 32 | filesize= os.path.getsize(args.file.name) |
| 33 | |
| 34 | if not args.quiet : print("Uploading " + args.file.name + " to " + args.ipaddr + " as " + output + " size: " + str(filesize) ) |
| 35 | |
| 36 | # make connection to sftp server |
| 37 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 38 | s.settimeout(4.0) |
| 39 | s.connect((args.ipaddr, 115)) |
| 40 | tn= s.makefile() |
| 41 | |
| 42 | # read startup prompt |
| 43 | ln= tn.readline() |
| 44 | if not ln.startswith("+") : |
| 45 | print("Failed to connect with sftp: " + ln) |
| 46 | sys.exit(); |
| 47 | |
| 48 | if verbose: print("RSP: " + ln.strip()) |
| 49 | |
| 50 | # Issue initial store command |
| 51 | tn.write("STOR OLD /sd/" + output + "\n") |
| 52 | tn.flush() |
| 53 | |
| 54 | ln= tn.readline() |
| 55 | if not ln.startswith("+") : |
| 56 | print("Failed to create file: " + ln) |
| 57 | sys.exit(); |
| 58 | |
| 59 | if verbose: print("RSP: " + ln.strip()) |
| 60 | |
| 61 | # send size of file |
| 62 | tn.write("SIZE " + str(filesize) + "\n") |
| 63 | tn.flush() |
| 64 | |
| 65 | ln= tn.readline() |
| 66 | if not ln.startswith("+") : |
| 67 | print("Failed: " + ln) |
| 68 | sys.exit(); |
| 69 | |
| 70 | if verbose: print("RSP: " + ln.strip()) |
| 71 | |
| 72 | cnt= 0 |
| 73 | # now send file |
| 74 | for line in f: |
| 75 | tn.write(line) |
| 76 | if verbose : |
| 77 | print("SND: " + line.strip()) |
| 78 | elif not args.quiet : |
| 79 | cnt += len(line) |
| 80 | print(str(cnt) + "/" + str(filesize) + "\r", end='') |
| 81 | |
| 82 | |
| 83 | tn.flush() |
| 84 | |
| 85 | ln= tn.readline() |
| 86 | if not ln.startswith("+") : |
| 87 | print("Failed to save file: " + ln) |
| 88 | sys.exit(); |
| 89 | |
| 90 | if verbose: print("RSP: " + ln.strip()) |
| 91 | |
| 92 | # exit |
| 93 | tn.write("DONE\n") |
| 94 | tn.flush() |
| 95 | ln= tn.readline() |
| 96 | tn.close() |
| 97 | f.close() |
| 98 | |
| 99 | if not args.quiet : print("Upload complete") |
| 100 | |