3 Upload a file to Smoothie over the network
6 from __future__
import print_function
12 # Define command line argument interface
13 parser
= argparse
.ArgumentParser(description
='Upload a file to Smoothie over network.')
14 parser
.add_argument('file', type=argparse
.FileType('r'),
15 help='filename to be uploaded')
16 parser
.add_argument('ipaddr',
17 help='Smoothie IP address')
18 parser
.add_argument('-v','--verbose',action
='store_true',
19 help='Show data being uploaded')
20 parser
.add_argument('-o','--output',
21 help='Set output filename')
22 parser
.add_argument('-q','--quiet',action
='store_true',
23 help='suppress all output to terminal')
24 parser
.add_argument('-s','--space',action
='store_true',
25 help='Leave whitespaces in output filename')
27 args
= parser
.parse_args()
30 verbose
= args
.verbose
33 output
= args
.file.name
35 output
= re
.sub("\s", "_", output
)
37 filesize
= os
.path
.getsize(args
.file.name
)
39 if not args
.quiet
: print("Uploading " + args
.file.name
+ " to " + args
.ipaddr
+ " as " + output
+ " size: " + str(filesize
) )
41 # make connection to sftp server
42 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
44 s
.connect((args
.ipaddr
, 115))
49 if not ln
.startswith("+") :
50 print("Failed to connect with sftp: " + ln
)
53 if verbose
: print("RSP: " + ln
.strip())
55 # Issue initial store command
56 tn
.write("STOR OLD /sd/" + output
+ "\n")
60 if not ln
.startswith("+") :
61 print("Failed to create file: " + ln
)
64 if verbose
: print("RSP: " + ln
.strip())
67 tn
.write("SIZE " + str(filesize
) + "\n")
71 if not ln
.startswith("+") :
72 print("Failed: " + ln
)
75 if verbose
: print("RSP: " + ln
.strip())
82 print("SND: " + line
.strip())
85 print(str(cnt
) + "/" + str(filesize
) + "\r", end
='')
91 if not ln
.startswith("+") :
92 print("Failed to save file: " + ln
)
95 if verbose
: print("RSP: " + ln
.strip())
104 if not args
.quiet
: print("Upload complete")