将FTP服务器与pysftp连接

2024-09-28 20:18:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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
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.egg\pysftp\__init__.py", line 140, in __init__
    File "pysftp-0.2.9-py3.7.egg\py`enter code here`sftp\__init__.py", line 176, in _start_transport
    File "Python37-32\lib\site-packages\paramiko\transport.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

Tags: toinpyself服务器hostforinit
2条回答

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

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

例如,namescape将端口21098用于SFTP连接。为了说明这一点,您可以在对pysft.Connection的调用中包含一个port参数:

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

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

pysftp是SFTP库

FTP和SFTP是完全不同的协议

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

相关问题 更多 >