2
2
3
3
"""Example script for SFTP write"""
4
4
5
- from __future__ import print_function
6
-
7
5
import argparse
8
6
import socket
9
7
import os
10
- import pwd
11
- import sys
12
8
from datetime import datetime
13
9
14
10
from ssh2 .session import Session
17
13
LIBSSH2_SFTP_S_IROTH
18
14
19
15
20
- USERNAME = pwd . getpwuid ( os .geteuid ()). pw_name
16
+ USERNAME = os .getlogin ()
21
17
22
18
parser = argparse .ArgumentParser ()
23
19
32
28
33
29
34
30
def main ():
31
+ buf_size = 1024 * 1024
35
32
args = parser .parse_args ()
36
33
sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
37
34
sock .connect ((args .host , args .port ))
@@ -40,18 +37,23 @@ def main():
40
37
s .agent_auth (args .user )
41
38
sftp = s .sftp_init ()
42
39
mode = LIBSSH2_SFTP_S_IRUSR | \
43
- LIBSSH2_SFTP_S_IWUSR | \
44
- LIBSSH2_SFTP_S_IRGRP | \
45
- LIBSSH2_SFTP_S_IROTH
40
+ LIBSSH2_SFTP_S_IWUSR | \
41
+ LIBSSH2_SFTP_S_IRGRP | \
42
+ LIBSSH2_SFTP_S_IROTH
46
43
f_flags = LIBSSH2_FXF_CREAT | LIBSSH2_FXF_WRITE
44
+ fileinfo = os .stat (args .source )
47
45
print ("Starting copy of local file %s to remote %s:%s" % (
48
46
args .source , args .host , args .destination ))
49
47
now = datetime .now ()
50
- with open (args .source , 'rb' ) as local_fh , \
48
+ with open (args .source , 'rb' , buf_size ) as local_fh , \
51
49
sftp .open (args .destination , f_flags , mode ) as remote_fh :
52
- for data in local_fh :
50
+ data = local_fh .read (buf_size )
51
+ while data :
53
52
remote_fh .write (data )
54
- print ("Finished writing remote file in %s" % (datetime .now () - now ,))
53
+ data = local_fh .read (buf_size )
54
+ taken = datetime .now () - now
55
+ rate = (fileinfo .st_size / 1024000.0 ) / taken .total_seconds ()
56
+ print (f"Finished writing remote file in { taken } , transfer rate { rate } MB/s" )
55
57
56
58
57
59
if __name__ == "__main__" :
0 commit comments