我正在使用 pysftp 库的get_r函数(https://pysftp.readthedocs.io/en/release_0.2.9/pysftp.html#pysftp.Connection.get_r)从 sftp 服务器获取目录结构的本地副本。
对于远程目录的内容已更改并且我只想获取自上次运行脚本以来更改的文件的情况,这是正确的方法吗?
该脚本应该能够递归地同步远程目录并镜像远程目录的状态 - fe 使用参数控制是否应该删除本地过时的文件(远程服务器上不再存在的文件),以及对应该获取现有文件和新文件。
用法示例:
from sftp_sync import sync_dir
sync_dir('/remote/path/', '/local/path/')
Run Code Online (Sandbox Code Playgroud) 我想使用 Python 2.7 使用 SFTP 递归地将包含文件和子文件夹的整个目录结构从 Linux 服务器复制到本地计算机(Windows 和 Linux)。
我可以在同一台机器上使用 WinSCP ping 服务器并下载文件。
我尝试了以下代码,在 Linux 上运行良好,但在 Windows 上不起作用。
我试过\,/,os.join,全部给了我同样的错误,检查权限也是如此。
import os
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None # disable host key checking.
sftp=pysftp.Connection('xxxx.xxx.com', username='xxx', password='xxx', cnopts=cnopts)
sftp.get_r('/abc/def/ghi/klm/mno', 'C:\pqr', preserve_mtime=False)
Run Code Online (Sandbox Code Playgroud)
import os
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None # disable host key checking.
sftp=pysftp.Connection('xxxx.xxx.com', username='xxx', password='xxx', cnopts=cnopts)
sftp.get_r('/abc/def/ghi/klm/mno', 'C:\pqr', preserve_mtime=False)
Run Code Online (Sandbox Code Playgroud) 该代码不起作用:
def sftp_connection(self):
import pysftp
connection = pysftp.Connection(self.host, username=self.system_name,
private_key=os.path.join(HOME, '.ssh', 'id_rsa'))
# in the next lines I try to use AutoAddPolicy
client = connection.sftp_client()
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)
return connection
Run Code Online (Sandbox Code Playgroud)
这是例外:
def sftp_connection(self):
import pysftp
connection = pysftp.Connection(self.host, username=self.system_name,
private_key=os.path.join(HOME, '.ssh', 'id_rsa'))
# in the next lines I try to use AutoAddPolicy
client = connection.sftp_client()
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)
return connection
Run Code Online (Sandbox Code Playgroud)
在尝试设置之前,我得到了例外host_key_policy。
我找不到通过pysftp访问客户端实例的其他方法。
有没有办法AutoAddPolicy在我得到异常之前进行设置?
还有一个相关的问题。我的问题是关于如何应用旧问题中提供的几种解决方案之一。
我需要连接到SFTP,下载最新文件,然后更改文件名并再次加载到相同的SFTP文件夹并删除“原始名称”文件。我已经使用用户名和密码通过FTP完成了此操作,但是在这种情况下,SFTP具有一个密钥文件(.ppk)。如何将密钥文件设置为密码?
谢谢!
import pysftp
srv = pysftp.Connection(host="your_FTP_server", username="your_username",
password="your_password")
# Get the directory and file listing
data = srv.listdir()
# Closes the connection
srv.close()
# Prints out the directories and files, line by line
for i in data:
print i
Run Code Online (Sandbox Code Playgroud) 此代码引发异常。如何在不将其存储在文件中的情况下验证 SSH 指纹?我相信下面的代码是为公钥设计的。但是带有 SFTP 服务器的客户端验证了指纹并且没有给我公钥。
import os
import shutil
import pysftp
import paramiko
connection_info = {
'server': "example.com",
'user': "user",
'passwd': "password",
'target_dir': "out/prod",
'hostkey': "ssh-rsa 2048 d8:4e:f1:f1:f1:f1:f1:f1:21:31:41:14:13:12:11:aa",
}
def move_files_from_server_to_local(server, localpath):
target_dir = server['target_dir']
keydata = "d8:4e:f1:f1:f1:f1:f1:f1:21:31:41:14:13:12:11:aa"
key = paramiko.RSAKey(data=decodebytes(keydata))
options = pysftp.CnOpts()
options.hostkeys.add('example.com', 'ssh-rsa', key)
with pysftp.Connection(
server['server'],
username=server['user'],
password=server['passwd'],
cnopts=options) as conn:
conn.get_d(target_dir, localpath)
delete_files_from_dir(conn, target_dir)
move_files_from_server_to_local(connection_info, "/")
Run Code Online (Sandbox Code Playgroud)
该代码基于使用 pysftp 验证主机密钥。
我的代码首先将行写入 CSV 中io.StringIO():
fileBuffer = io.StringIO()
# write header
header_writer = csv.DictWriter(fileBuffer, fieldnames=columnNames)
header_writer.writeheader()
# write lines
writer = csv.writer(fileBuffer, delimiter=',')
for line in data:
line_dec = line.decode('ISO-8859-1')
# print([line_dec])
writer.writerow([line_dec])
Run Code Online (Sandbox Code Playgroud)
以下代码还打印所有预期行:
$print(fileBuffer.getvalue()) # -> prints all expected rows
Run Code Online (Sandbox Code Playgroud)
我还可以使用 pysftp 成功连接到 SFTP 服务器,甚至在使用 pysftp 时,代码也成功返回所有预期行:
with pysftp.Connection(host, username=user, password=pw, cnopts=cnopts) as sftp:
print('sucessfully connected to {} via Port 22'.format(host))
print(fileBuffer.getvalue()) # -> prints all expected rows
sftp.putfo(fileBuffer, file2BeSavedAs) # -> no rows put on FTP Server
Run Code Online (Sandbox Code Playgroud)
实际问题来了: …
我正在使用Python 2.7 pysftp程序包连接到SFTP服务器。
import pysftp
DOWNLOAD = {
"USERNAME": "username",
"PASSWORD": "password"
}
FTP_SITE = 'sftp.mysite.com'
srv = pysftp.Connection(host=FTP_SITE, username=DOWNLOAD['USERNAME'],
password=DOWNLOAD['PASSWORD']
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我得到了错误日志:
import pysftp
DOWNLOAD = {
"USERNAME": "username",
"PASSWORD": "password"
}
FTP_SITE = 'sftp.mysite.com'
srv = pysftp.Connection(host=FTP_SITE, username=DOWNLOAD['USERNAME'],
password=DOWNLOAD['PASSWORD']
Run Code Online (Sandbox Code Playgroud)
我目前正在通过执行以下操作来关闭对主机密钥的检查:
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
srv = pysftp.Connection(host=FTP_SITE, username=DOWNLOAD['USERNAME'],
password=DOWNLOAD['PASSWORD'], cnopts=cnopts)
Run Code Online (Sandbox Code Playgroud)
我想保留主机密钥的安全性功能。谁能提供有关如何生成主机密钥的链接,或者在此处提供一小段代码示例?我没找到太多。
我正在使用 pysftp 连接到服务器并向其上传文件。
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
self.sftp = pysftp.Connection(host=self.serverConnectionAuth['host'], port=self.serverConnectionAuth['port'],
username=self.serverConnectionAuth['username'], password=self.serverConnectionAuth['password'],
cnopts=cnopts)
self.sftp.put(localpath=self.filepath+filename, remotepath=filename)
Run Code Online (Sandbox Code Playgroud)
有时它没有错误,但有时它会正确放置文件,但会引发以下异常。该文件由服务器上运行的另一个程序读取和处理,因此我可以看到该文件在那里并且没有损坏
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
self.sftp = pysftp.Connection(host=self.serverConnectionAuth['host'], port=self.serverConnectionAuth['port'],
username=self.serverConnectionAuth['username'], password=self.serverConnectionAuth['password'],
cnopts=cnopts)
self.sftp.put(localpath=self.filepath+filename, remotepath=filename)
Run Code Online (Sandbox Code Playgroud)
我怎样才能防止异常?
我想读取安全 SFTP 文件夹上的一些 CSV/Excel 文件,在这些文件中进行一些更改(每个文件中的固定更改,如删除第 2 列),将它们上传到 Postgre 数据库,并将它们上传到不同的 SFTP 路径Python
最好的方法是什么?
我已经使用 pysftp 库连接到 SFTP 并正在读取 Excel:
import pysftp
import pandas as pd
myHostname = "*****"
myUsername = "****"
myPassword = "***8"
cnopts =pysftp.CnOpts()
cnopts.hostkeys = None
sftp=pysftp.Connection(host=myHostname, username=myUsername,
password=myPassword,cnopts=cnopts)
print ("Connection succesfully stablished ... ")
sftp.chdir('test/test')
#sftp.pwd
a=[]
for i in sftp.listdir_attr():
with sftp.open(i.filename) as f:
df=pd.read_csv(f)
Run Code Online (Sandbox Code Playgroud)
我应该如何继续上传到数据库并使对 CSV 的这些更改永久生效?
我在 etl 工作(第一次),我需要从客户端的 SFTP 中提取一些文件。我的问题是,文件数量是可变的,所以我需要检查,如果文件中存在,并得到它,该文件格式,如“_文件YYYY-MM-DD -number- ñ ”,其中YYYY-MM-DD是当前的日期和n是文件的编号,因此如果有 7 个文件,我必须查找:
直到现在我发现我可以做这样的事情
cnopts = pysftp.CnOpts()
with pysftp.Connection(host=host, port=port, username=username, password=password, cnopts=cnopts) as sftp:
files = sftp.listdir(directory)
Run Code Online (Sandbox Code Playgroud)
我如何在那里的文件中找到?
我想使用 pysftp 0.2.8 将多个文件从 Windows 目录上传到 SFTP 服务器。我已经阅读了文档,它建议使用put_dorput_r但两者都给我以下错误:
OSError:无效路径:
sftp_local_path = r'C:\Users\Swiss\some\path'
sftp_remote_path = '/FTP/LPS Data/ATC/RAND/20191019_RAND/XML'
with pysftp.Connection("xxx.xxx.xxx.xxx", username=myUsername, password=myPassword) as sftp:
with sftp.cd(sftp_remote_path):
sftp.put_r(sftp_local_path, sftp_remote_path)
for i in sftp.listdir():
lstatout=str(sftp.lstat(i)).split()[0]
if 'd' in lstatout: print (i, 'is a directory')
sftp.close()
Run Code Online (Sandbox Code Playgroud)
我希望能够将本地目录中的所有文件或选定文件复制到 SFTP 服务器。