Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. class tyFTP(ftplib.FTP_TLS):
  2. def __init__(self, host='', user='', passwd='', acct='', keyfile=None,
  3. certfile=None, timeout=60):
  4. ftplib.FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)
  5. def connect(self, host='', port=0, timeout=-999):
  6. '''Connect to host. Arguments are:
  7. - host: hostname to connect to (string, default previous host)
  8. - port: port to connect to (integer, default previous port)
  9. '''
  10. if host != '':
  11. self.host = host
  12. if port > 0:
  13. self.port = port
  14. if timeout != -999:
  15. self.timeout = timeout
  16. try:
  17. self.sock = socket.create_connection((self.host, self.port), self.timeout)
  18. self.af = self.sock.family
  19. #add this line!!!
  20. self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile,ssl_version=ssl.PROTOCOL_TLSv1)
  21. #add end
  22. self.file = self.sock.makefile('rb')
  23. self.welcome = self.getresp()
  24. except Exception as e:
  25. print(e)
  26. return self.welcome
  27.  
  28. from ftplib import FTP_TLS
  29. import socket
  30. import ssl
  31.  
  32. class tyFTP(FTP_TLS):
  33. def __init__(self, host='', user='', passwd='', acct='', keyfile=None, certfile=None, timeout=60):
  34. FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)
  35. def connect(self, host='', port=0, timeout=-999):
  36. if host != '':
  37. self.host = host
  38. if port > 0:
  39. self.port = port
  40. if timeout != -999:
  41. self.timeout = timeout
  42.  
  43. try:
  44. self.sock = socket.create_connection((self.host, self.port), self.timeout)
  45. self.af = self.sock.family
  46. self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile, ssl_version=ssl.PROTOCOL_TLSv1)
  47. self.file = self.sock.makefile('rb')
  48. self.welcome = self.getresp()
  49. except Exception as e:
  50. print e
  51. return self.welcome
  52.  
  53. from ImplicityTLS import tyFTP
  54. server = tyFTP()
  55. server.connect(host="xxxxx", port=990)
  56. server.login(user="yyyy", passwd="fffff")
  57. server.prot_p()
  58.  
  59. from ftplib import FTP_TLS, FTP
  60. import socket
  61. import ssl
  62.  
  63. class IMPLICIT_FTP_TLS(FTP_TLS):
  64. def __init__(self, host='', user='', passwd='', acct='', keyfile=None,
  65. certfile=None, timeout=60):
  66. FTP_TLS.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)
  67.  
  68. def connect(self, host='', port=0, timeout=-999):
  69. '''Connect to host. Arguments are:
  70. - host: hostname to connect to (string, default previous host)
  71. - port: port to connect to (integer, default previous port)
  72. '''
  73. if host != '':
  74. self.host = host
  75. if port > 0:
  76. self.port = port
  77. if timeout != -999:
  78. self.timeout = timeout
  79. try:
  80. self.sock = socket.create_connection((self.host, self.port), self.timeout)
  81. self.af = self.sock.family
  82. self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
  83. self.file = self.sock.makefile('rb')
  84. self.welcome = self.getresp()
  85. except Exception as e:
  86. print (e)
  87. return self.welcome
  88.  
  89. def ntransfercmd(self, cmd, rest=None):
  90. conn, size = FTP.ntransfercmd(self, cmd, rest)
  91. if self._prot_p:
  92. conn = ssl.wrap_socket(conn, self.keyfile, self.certfile)
  93. return conn, size
  94.  
  95. >>> ftps = IMPLICIT_FTP_TLS()
  96. >>> ftps.connect(host='your.ftp.host', port=990)
  97. >>> ftps.login(user="your_user", passwd="your_passwd")
  98. >>> ftps.prot_p()
  99. >>> ftps.retrlines('LIST')
  100.  
  101. import ftplib, os, sys
  102. import socket
  103. import ssl
  104. FTPS_OBJ = ftplib.FTP_TLS
  105.  
  106.  
  107.  
  108. def conn_i_ftps(FTP_Site, Login_Name, Login_Password):
  109. print "Starting IMPLICIT ftp_tls..."
  110. ftps = tyFTP()
  111. print ftps.connect(host=FTP_Site, port=990, timeout=120)
  112. ftps.prot_p()
  113. ftps.login(user=Login_Name, passwd=Login_Password)
  114. print "Logged In"
  115. ftps.retrlines('LIST')
  116. # return ftps
  117.  
  118.  
  119. class tyFTP(FTPS_OBJ):
  120. def __init__(self, host='', user='', passwd='', acct='', keyfile=None, certfile=None, timeout=60):
  121. FTPS_OBJ.__init__(self, host, user, passwd, acct, keyfile, certfile, timeout)
  122.  
  123. def connect(self, host='', port=0, timeout=-999):
  124. if host != '':
  125. self.host = host
  126. if port > 0:
  127. self.port = port
  128. if timeout != -999:
  129. self.timeout = timeout
  130.  
  131. try:
  132. self.sock = socket.create_connection((self.host, self.port), self.timeout)
  133. self.af = self.sock.family
  134. self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
  135. self.file = self.sock.makefile('rb')
  136. self.welcome = self.getresp()
  137. except Exception as e:
  138. print e
  139. return self.welcome
  140.  
  141. def makepasv(self):
  142. print port #<---Show passively assigned port
  143. print host #<---Show the non-routable, passively assigned IP
  144. host, port = FTPS_OBJ.makepasv(self)
  145. host = socket.gethostbyname(self.host) #<---- This changes the host back to the original IP that was used for the connection
  146. print 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  147. print host #<----Showing the original IP
  148. return host, port
  149.  
  150. FTP_Site = "ftp.someserver.com"
  151. Login_Name = "some_name"
  152. Login_Password = "some_passwd"
  153.  
  154. conn_i_ftps(FTP_Site, Login_Name, Login_Password)
  155.  
  156. if host.split(".")[0] in (10, 192, 172):
  157. host = socket.gethostbyname(self.host)
  158. .
  159. .
  160. .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement