通过FTP_TLS将文件上传到FTP服务器获取错误ssl

时间:2017-12-04 04:24:20

标签: python ssl ftp

我使用filezilla服务器来创建ftp服务器。 ssl选项在端口21和990上打开。我尝试使用filezilla客户端上传和下载文件仍然运行良好。我尝试使用ftplib将文件上传到FTP服务器。这是我的代码:

*get* '227 Entering Passive Mode (192,168,1,102,19,170)\r\n'
*resp* '227 Entering Passive Mode (192,168,1,102,19,170)'
*cmd* 'STOR test.py'
*put* 'STOR test.py\r\n'
*get* '150 Opening data channel for file upload to server of "/test.py"\r\n'
*resp* '150 Opening data channel for file upload to server of "/test.py"'
Traceback (most recent call last):
  File "ImplicitFTP.py", line 49, in <module>
    ftps.storbinary("STOR test.py", file)
  File "C:\Python27\lib\ftplib.py", line 760, in storbinary
    conn = self.transfercmd(cmd, rest)
  File "C:\Python27\lib\ftplib.py", line 376, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Python27\lib\ftplib.py", line 713, in ntransfercmd
    server_hostname=self.host)
  File "C:\Python27\lib\ssl.py", line 363, in wrap_socket
    _context=self)
  File "C:\Python27\lib\ssl.py", line 611, in __init__
    self.do_handshake()
  File "C:\Python27\lib\ssl.py", line 840, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:661)

得到错误:

var data=[{
  animal: "cat",
  dateString: "2017-01-03"
},{
  animal: "dog",
  dateString: "2017-02-05"
},{
  animal: "cat",
  dateString: "2017-03-04"
}]

var ans = data.reduce(function(accumulator, element) {
var elementInAccumulator = accumulator[element.animal]
if(elementInAccumulator) {
  if(Array.isArray(elementInAccumulator.values)) {
    elementInAccumulator.values.push(element.dateString)
  }
  else {
  var arr=[]
  
  arr.push(elementInAccumulator.dateString)
  arr.push(element.dateString)
  elementInAccumulator.values= arr
  delete(elementInAccumulator.dateString)
  }
}
else {
  accumulator[element.animal] = element
}
return accumulator
}, {})
console.log(Object.keys(ans).map(function(element) {
  return ans[element]
}))

请帮帮我。感谢

1 个答案:

答案 0 :(得分:1)

问题可能是FTP服务器要求新数据通道中的TLS会话与控制通道相同。这在Python 3.7中尚未修复。为ftplib.FTP_TLS子类,如https://stackoverflow.com/a/43301750此处找到的解决方案,我对此做了一个小修正:

import ftplib


class ReusedSslSocket(SSLSocket):
    def unwrap(self):
        pass


class MyFTP_TLS(ftplib.FTP_TLS):
    """Explicit FTPS, with shared TLS session"""
    def ntransfercmd(self, cmd, rest=None):
        conn, size = ftplib.FTP.ntransfercmd(self, cmd, rest)
        if self._prot_p:
            conn = self.context.wrap_socket(conn,
                                            server_hostname=self.host,
                                            session=self.sock.session)  # reuses TLS session            
            conn.__class__ = ReusedSslSocket  # we should not close reused ssl socket when file transfers finish
        return conn, size

像这样使用它:

ftps = MyFTP_TLS()