使用pysftp连接FTP服务器



我有以下代码要从4个不同的FTP服务器下载。它在我的本地机器上工作,但在服务器上";ftp4";未连接。奇怪的是,我可以连接到";ftp4";使用FileZilla在服务器上没有任何问题。这是代码:

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.eggpysftp__init__.py:61: UserWarning: Failed to load HostKeys from .sshknown_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
Traceback (most recent call last):
File "main.py", line 183, in <module>
downloader.run()
File "main.py", line 107, in run
with pysftp.Connection(host[0], username=host[1], password=host[2], cnopts=self.cnopts) as sftp:
File "pysftp-0.2.9-py3.7.eggpysftp__init__.py", line 140, in __init__
File "pysftp-0.2.9-py3.7.eggpy`enter code here`sftp__init__.py", line 176, in _start_transport
File "Python37-32libsite-packagesparamikotransport.py", line 416, >in __init__
"Unable to connect to {}: {}".format(hostname, reason)
paramiko.ssh_exception.SSHException: Unable to connect to 10.10.10.04: 
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

服务器上的FTP日志:

Status: Connecting to 10.10.10.04:21...  
Status: Connection established, waiting for welcome message...  
Status: Initializing TLS...  
Status: Verifying certificate...  
Status: TLS connection established.  
Status: Logged in  
Status: Retrieving directory listing...  
Status: Directory listing of "/" successful

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

pysftp是SFTP库。

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

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

如果您试图连接到错误的端口,也可能发生此错误。

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

例如,Namecheap使用端口21098进行SFTP连接。为此,您可以在对pysft的调用中包含port参数。连接:

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

最新更新