使用pysftp连接FTP服务器

时间:2020-03-08 11:48:57

标签: python ftp sftp pysftp

我有从4个不同的ftp服务器下载的流代码,它在我的本地计算机上有效,但在服务器ftp4上未连接。我可以用Filezila连接到ftp 4而在服务器上没有任何问题是很奇怪的 这是代码:


HOST_LIST = [["10.10.10.04", "user", "pasword"]] #ftp4

self.cnopts = pysftp.CnOpts()
self.cnopts.hostkeys = None

 def get_r_portable(self, 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_ISREG(mode):
            if self.PATHFILTER.strip() != "":
                 if str(remotepath).lower().find(self.PATHFILTER.lower()) > -1:
                    sftp.get(remotepath, localpath, preserve_mtime=preserve_mtime)


for host in self.HOST_LIST:
            with pysftp.Connection(host[0], username=host[1], password=host[2], cnopts=self.cnopts) as sftp:
                try:
                    for dirs in self.FOL_LIST:
                        currdir = REMOTEFOLDER + dirs + ""
                        try:
                            self.get_r_portable(sftp, currdir, self.LOCALFOLDER, True)
                        except Exception as e:
                            self.logger.error("Exception in dir exploration" + str(e))
                except Exception as e:
                    print('error')

我对于ftp4的错误:

[ 2020-03-08 15:05:03,574 ] [  ][ WARNING ] Please note that this utility does not use hostkeys to verify the hosts. If this is insecure for your setup, then kindly update the code or submit a feature request.
\pysftp-0.2.9-py3.7.egg\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from \.ssh\known_hosts.  
You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
[ 2020-03-08 15:05:03,577 ] [ ][ INFO ] Attempting connection to 10.10.10.04

回溯(最近通话最近一次):

文件“ main.py”,第183行,位于 downloader.run() 运行中的文件“ main.py”,第107行 使用pysftp.Connection(host [0],username = host [1],password = host [2],> cnopts = self.cnopts)作为sftp: > init 中的文件“ pysftp-0.2.9-py3.7.egg \ pysftp__init __。py”,第140行 _start_transport中的文件“ pysftp-0.2.9-py3.7.egg \ py enter code here sftp__init __。py”,第176行 init 中的文件“ Python37-32 \ lib \ site-packages \ paramiko \ transport.py”,第416行 “无法连接到{}:{}”。格式(主机名,原因) paramiko.ssh_exception.SSHException:无法连接到10.10.10.04: [WinError 10060]连接尝试失败,因为已连接的一方在一段时间后未正确响应,或者由于连接的主机未响应而建立的连接失败

- FTP登录服务器:

- 状态:正在连接10.10.10.04:21 ...
状态:连接已建立,正在等待欢迎消息...
状态:正在初始化TLS ...
状态:正在验证证书...
状态:TLS连接已建立。
状态:已登录
状态:正在检索目录列表...
状态:目录列表中的“ /”成功

2 个答案:

答案 0 :(得分:0)

您正在连接到FileZilla中的FTP服务器。

pysftp是SFTP库。

FTP和SFTP是完全不同的协议。

要使用Python连接FTP,请使用ftplib。

答案 1 :(得分:0)

如果您尝试连接到错误的端口,也会发生此错误。

默认情况下,pysftp 使用端口 22(标准 SFTP 端口),但有些主机使用不同的端口。

例如,Namecheap 使用端口 21098 进行 SFTP 连接。考虑到这一点,您可以在对 pysft.Connection 的调用中包含一个 port 参数:

pysftp.Connection(host, username=myusername, password=mypassword, port=21098, cnopts=mycnopts)