首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用pysftp连接FTP服务器

使用pysftp连接FTP服务器
EN

Stack Overflow用户
提问于 2020-03-08 19:48:57
回答 2查看 1.4K关注 0票数 2

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

代码语言:javascript
运行
复制
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”得到的错误:

代码语言:javascript
运行
复制
[ 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
代码语言:javascript
运行
复制
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日志:

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

发布于 2020-03-10 00:41:26

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

pysftp是SFTP库。

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

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

票数 1
EN

Stack Overflow用户

发布于 2021-07-06 17:43:35

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

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

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

代码语言:javascript
运行
复制
pysftp.Connection(host, username=myusername, password=mypassword, port=21098, cnopts=mycnopts)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60587160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档