在 pysftp 连接字符串中指定端口并不直接

Specifying the port in a pysftp connection string is not straight forward

这个有效:

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

with pysftp.Connection('ftpsite.com', username='xxx', password='xxx', cnopts=cnopts) as sftp:
    with sftp.cd('inbox'):             
    sftp.get('WinSCP.ini')  

但是现在我想直接测试ftp(端口21),所以我添加端口属性:

with pysftp.Connection('ftpsite.com', port=21 , username='xxx', password='xxx', cnopts=cnopts) as sftp:

现在我明白了:

异常:paramiko.ssh_exception.SSHException 消息:读取 SSH 协议横幅时出错

我很困惑...

SFTP 使用 SSH 所以它的端口是 22,而不是 21

FTP 使用端口 21

如错误所说,ssh异常。尝试 'port=22'

来源:

port 21     Yes, and SCTP   Assigned    Official    File Transfer Protocol (FTP) control (command)


port 22     Yes, and SCTP   Assigned    Official    Secure Shell (SSH), secure logins, file transfers (scp, sftp) and port forwarding 

wikipedia

pysftp 库仅使用 SFTP 协议进行通信,这与 'normal' FTP 协议不同。所以你看到的是当你的程序试图在 SFTP 中与一个 FTP 服务器对话,并且不理解它返回的响应时的错误。