Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. # Combines several solutions found on the internet
  2.  
  3. class ImplicitFTP_TLS(ftplib.FTP_TLS):
  4. """FTP_TLS subclass to support implicit FTPS."""
  5. """Constructor takes a boolean parameter ignore_PASV_host whether o ignore the hostname"""
  6. """in the PASV response, and use the hostname from the session instead"""
  7. def __init__(self, *args, **kwargs):
  8. self.ignore_PASV_host = kwargs.get('ignore_PASV_host') == True
  9. super().__init__(*args, {k: v for k, v in kwargs.items() if not k == 'ignore_PASV_host'})
  10. self._sock = None
  11.  
  12. @property
  13. def sock(self):
  14. """Return the socket."""
  15. return self._sock
  16.  
  17. @sock.setter
  18. def sock(self, value):
  19. """When modifying the socket, ensure that it is ssl wrapped."""
  20. if value is not None and not isinstance(value, ssl.SSLSocket):
  21. value = self.context.wrap_socket(value)
  22. self._sock = value
  23.  
  24. def ntransfercmd(self, cmd, rest=None):
  25. """Override the ntransfercmd method"""
  26. conn, size = ftplib.FTP.ntransfercmd(self, cmd, rest)
  27. conn = self.sock.context.wrap_socket(
  28. conn, server_hostname=self.host, session=self.sock.session
  29. )
  30. return conn, size
  31.  
  32. def makepasv(self):
  33. host, port = super().makepasv()
  34. return (self.host if self.ignore_PASV_host else host), port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement