【发布时间】:2019-05-27 06:11:56
【问题描述】:
我在这个站点上有一个代码,可以从服务器的远程目录下载文件。现在我想修改此代码,以便它比较远程目录上而不是本地目录上的文件和列表。它列出了远程目录和本地目录之间不常见的文件。 这可能吗? 请帮忙。提前致谢
import os
import pysftp
import socket
from stat import S_IMODE, S_ISDIR, S_ISREG
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
IP = "192.168.X.X"
myUsername = "user"
port = 22
myPassword = "password"
try:
with pysftp.Connection(host=IP, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
try:
r=str(socket.gethostbyaddr(IP))
print("connection successful with "+r)
def get_r_portable(sftp, remotedir, localdir, preserve_mtime=False):
for entry in sftp.listdir(remotedir):
remotepath = remotedir + "/" + entry
localpath = os.path.join(localdir, entry)
mode = sftp.stat(remotepath).st_mode
if S_ISDIR(mode):
try:
os.mkdir(localpath, mode=777)
except OSError:
pass
get_r_portable(sftp, remotepath, localpath, preserve_mtime)
elif S_ISREG(mode):
sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)
remote_path = input("enter the remote_path: ")
local_path = input("enter the local_path: ")
get_r_portable(sftp, remote_path, local_path, preserve_mtime=False)
except socket.herror:
print("Unknown host")
except:
print("connection failed")
结果应该是存在于远程目录而不是本地目录中的不常见文件。
【问题讨论】:
-
不,这不可能。请通过以下链接。 Check it here
-
@ashok knv 请看下面的答案
标签: python python-3.x sftp pysftp