ftplib: socket.error during/after LIST // ssl._ssl

2019-04-13 13:14发布

I try to connect to a FTPS server using a client certificate.
I tried on 2 different servers (over which I've no control but that should be quite similar).

The connection is established and the PWD command succeeds.
On one server the LIST command succeed but on the second one, it produces the right result (the list of files) but generates an error after (apparently during the SSL shutdown).
On the server side, they told me: "Your LIST command is passing correctly but afterward you loose the connection during a new SSL re-negociation."
Any idea why?

Thanks in advance.

.

The common commands to establish the connection:

# Python 2.7.3rc2 (default, Apr 22 2012, 22:30:17)

>>> import ssl
>>> from ftplib import FTP_TLS

>>> ftps = FTP_TLS(keyfile="/path/to/***.key", certfile="/path/to/***.crt")
>>> ftps.set_debuglevel(2)
>>> ftps.ssl_version = ssl.PROTOCOL_TLSv1
>>> ftps.connect("***", 7806)
*get* '220 Welcome to Synchrony Gateway FTP server\r\n'
*resp* '220 Welcome to Synchrony Gateway FTP server'
'220 Welcome to Synchrony Gateway FTP server'

>>> ftps.auth()
*cmd* 'AUTH TLS'
*put* 'AUTH TLS\r\n'
*get* '234 AUTH command OK, waiting handshake\r\n'
*resp* '234 AUTH command OK, waiting handshake'
'234 AUTH command OK, waiting handshake'

>>> ftps.login("***", "***")
*cmd* 'USER ***'
*put* 'USER ***\r\n'
*get* '331 Send password please\r\n'
*resp* '331 Send password please'
*cmd* 'PASS ************'
*put* 'PASS ************\r\n'
*get* '230 User logged in, proceed\r\n'
*resp* '230 User logged in, proceed'
'230 User logged in, proceed'

>>> ftps.prot_p()
*cmd* 'PBSZ 0'
*put* 'PBSZ 0\r\n'
*get* '200 Command okay\r\n'
*resp* '200 Command okay'
*cmd* 'PROT P'
*put* 'PROT P\r\n'
*get* '200 Command okay\r\n'
*resp* '200 Command okay'
'200 Command okay'

>>> ftps.set_pasv(True)
>>> ftps.pwd()
*cmd* 'PWD'
*put* 'PWD\r\n'
*get* '257 "/" is current directory.\r\n'
*resp* '257 "/" is current directory.'
'/'

.

The LIST command that fails:

>>> ftps.retrlines("LIST")
*cmd* 'TYPE A'
*put* 'TYPE A\r\n'
*get* '200 Command okay\r\n'
*resp* '200 Command okay'
*cmd* 'PASV'
*put* 'PASV\r\n'
*get* '227 Entering passive mode (212,63,***,**,30,131).\r\n'
*resp* '227 Entering passive mode (212,63,***,**,30,131).'
*cmd* 'LIST'
*put* 'LIST\r\n'
*get* '125 List started\r\n'
*resp* '125 List started'
total 3
dr-xr-xr-x   0 --NA--   --NA--            0 Apr 23 16:46 .
d---------   0 --NA--   --NA--            0 Jun  4 15:02 ..
dr-xr-xr-x   0 --NA--   --NA--            0 Apr 23 16:46 **
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/ftplib.py", line 721, in retrlines
    conn.unwrap()
  File "/usr/lib/python2.7/ssl.py", line 284, in unwrap
    s = self._sslobj.shutdown()
socket.error: [Errno 0] Error

>>> ftps.pwd()
*cmd* 'PWD'
*put* 'PWD\r\n'
*get* '226 List completed\r\n'
*resp* '226 List completed'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/ftplib.py", line 575, in pwd
    return parse257(resp)
  File "/usr/lib/python2.7/ftplib.py", line 839, in parse257
    raise error_reply, resp
ftplib.error_reply: 226 List completed

>>> ftps.quit()
*cmd* 'QUIT'
*put* 'QUIT\r\n'
*get* '257 "/" is current directory.\r\n'
*resp* '257 "/" is current directory.'
'257 "/" is current directory.'

1条回答
相关推荐>>
2楼-- · 2019-04-13 13:55

We are also facing the same problem. inside SSL.PY we are geting error 0 exception

def unwrap(self):
    if self._sslobj:
        print "SSL Object Present"
        s = self._sslobj.shutdown()
        print "SSL Object Shoutdown"
        print "print s"
        print s
        self._sslobj = None
        return s
    else:
        raise ValueError("No SSL wrapper around " + str(self))

s = self._sslobj.shutdown() on this line its throwing exception. do you have nay update on this?

I modified the ftplib.py retrline method with below code

    if isinstance(conn, ssl.SSLSocket):
     conn.close()

its working with out any error. I don't know impact of this changes in other functionality

查看更多
登录 后发表回答