python ftp sftp

ftp upload and download files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from ftplib import FTP
import time
import os
import shutil

port = 21

DEF (Host, Port, username, password) :
FTP = the FTP ()
ftp.set_debuglevel ( 2 )
ftp.connect (Host, Port) # connection
ftp.login (username, password) # log, if the empty string anonymous login instead of to return the FTP



DEF DownloadFile (FTP, remotePath, localPath) :
the bufsize of = 1024 # cache block size setting
FP = Open (localPath, 'WB' ) # write mode to open the file in the local
ftp.retrbinary ( 'the RETR' + remotePath, fp.write, bufsize) on # receiving server file and write local files
ftp.set_debuglevel ( 0 ) # turn off debugging
fp.close () # close the file


def uploadfile(ftp, remotepath, localpath):
bufsize = 1024
fp = open(localpath, 'rb')
ftp.storbinary('STOR ' + remotepath, fp, bufsize) # 上传文件
ftp.set_debuglevel(0)
fp.close()


if __name__ == '__main__':
ftp = ftpconnect(host, port, username, password)
print(ftp)

paramiko SFTP upload and download files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
      
      
       
       Big Box   Python the FTP SFTP AN class = "Line">
        Import paramiko
        Import os
       
       
       
       
       
       


= hostname 'XXX'
username = 'XXX'
password = 'XXX'
Port = 22 is # int
upload_local_dir = 'XXX'
download_local_dir = 'XXX'
remote_dir = 'Inbox /' # note directory need to manually add /
SFTP_DOWNLOAD_FLAG = False # used determining whether file is downloaded successfully


def sftp_callback(transfered, total):
global SFTP_DOWNLOAD_FLAG
if transfered == total:
SFTP_DOWNLOAD_FLAG = True

def sftpconnect(host, port, username, password):
try:
sf = paramiko.Transport((host, port))
sf.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(sf)
return sftp
except Exception as e:
print('connect exception: ', e)


def sftp_upload(host, port, username, password, local, remote):
sf = paramiko.Transport((host, port))
sf.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(sf)
try:
if os.path.isdir(local):
for f in os.listdir(local):
sftp.put(os.path.join(local, f), os.path.join(remote+f))
else:
sftp.put(local, remote)
except Exception as e:
print('upload exception: ', e)
finally:
if sf is not None:
sf.close()


DEF sftp_download (Host, Port, username, password, local, Remote) : Global SFTP_DOWNLOAD_FLAG SF = paramiko.Transport ((Host, Port)) sf.connect (username = username, password = password) SFTP paramiko.SFTPClient.from_transport = ( SF) the try : # do not use the remote directory os.path.isdir (local) to determine whether the folder unless it has been determined # because inbox / parent directory may not have access to, determine the directory always returns False for f in SFTP. listdir (Remote): sftpAttributes = sftp.lstat (os.path.join (Remote + f)) fileSize = sftpAttributes.st_size IF fileSize: # only file is not empty, the function will be called sftp_callback











sftp.get (os.path.join (Remote + f), os.path.join (local, f), sftp_callback) the else : # whether to download an empty file can not be successful with sftp_callback judge, so I do not download continue



IF SFTP_DOWNLOAD_FLAG: # successful download, remove the remote directory files
SFTP_DOWNLOAD_FLAG = False
sftp.remove (os.path.join (Remote + f)) the else : # If the upload problem, remove local files have been uploaded os.remove (os .path.join (local, f))



except Exception as e:
print('download exception: ', e)
finally:
if sf is not None:
sf.close()


IF the __name__ == '__main__' : # test connection SFTP = sftpconnect (Host, Port, username, password) Print (SFTP)



Guess you like

Origin www.cnblogs.com/lijianming180/p/12361315.html