Guest User

Untitled

a guest
May 18th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. import ftplib
  2. from typing import List
  3. from os.path import join as pathjoin
  4.  
  5. host = "192.168.1.13"
  6. port = 21
  7. key_length_num_of_bytes = 4
  8. BYTE_ORDER = 'big'
  9.  
  10. # ftp.retrlines("LIST")
  11. # ftp.quit()
  12.  
  13. class MACGenerator(object):
  14.  
  15. def __init__(self) -> None:
  16. super().__init__()
  17.  
  18.  
  19. # noinspection PyPep8Naming
  20. @staticmethod
  21. def compute_MAC(f):
  22. return "A" * 128
  23.  
  24.  
  25. class FileSystemHandler(object):
  26.  
  27. def __init__(self, cwd) -> None:
  28. """
  29.  
  30. :param cwd: Current working dir
  31. """
  32. super().__init__()
  33. self.cwd = cwd
  34.  
  35.  
  36. # def is_file_exists(self, f):
  37.  
  38.  
  39. class FTPCLient(object):
  40.  
  41. def __init__(self, host, port) -> None:
  42. super().__init__()
  43.  
  44. self.port = port
  45. self.host = host
  46.  
  47. self.mac_generator = MACGenerator()
  48. self.connection_handler = ConnectionHandler(host, port)
  49. self.input_handler = UserInputHandler()
  50. self.cmd_handler = FTPcmd()
  51.  
  52.  
  53. def run(self):
  54.  
  55. self.connection_handler.login()
  56.  
  57. while not self.connection_handler.is_finished():
  58. user_in = self.input_handler.get_user_input()
  59.  
  60. if user_in is not None:
  61. processed_in = self.cmd_handler.process_input(user_in)
  62. else:
  63. print("NO GOOD")
  64. continue
  65.  
  66. self.connection_handler.send_cmd(processed_in)
  67. response = self.connection_handler.server_response()
  68. print(response)
  69.  
  70.  
  71. class FTPcmd(object):
  72. command_dict = {
  73. 'delete': 'DELE',
  74. 'upload': 'STOR',
  75. 'ls': 'LIST',
  76. 'download': 'RETR',
  77. }
  78.  
  79.  
  80. def __init__(self) -> None:
  81. super().__init__()
  82.  
  83.  
  84. def cmd_to_ftp_command(self, command):
  85.  
  86. returnable = FTPcmd.command_dict.get(command)
  87. if returnable is not None:
  88. return returnable
  89. else:
  90. print("NO GOOD ftp command")
  91. return None
  92.  
  93.  
  94. def process_input(self, user_in: List[str]):
  95. ftp_cmd = self.cmd_to_ftp_command(user_in[0])
  96. args = user_in[1:]
  97.  
  98. if ftp_cmd is not None and self.validate_args(ftp_cmd, args):
  99. return " ".join([ftp_cmd] + args)
  100.  
  101. def validate_args(self, ftp_cmd, args):
  102. return True
  103.  
  104.  
  105. class ConnectionHandler(object):
  106.  
  107. def __init__(self, host, port) -> None:
  108. super().__init__()
  109. self.port = port
  110. self.host = host
  111.  
  112. self.ftp = ftplib.FTP()
  113. self.ftp.connect(host, port)
  114.  
  115. self.recent_response = []
  116.  
  117.  
  118. def is_finished(self):
  119. return False
  120.  
  121.  
  122. def login(self):
  123. user = input('Username:')
  124. pw = input('Password:')
  125.  
  126. self.do_login(user, pw)
  127.  
  128.  
  129. def do_login(self, user, pw):
  130. resp = self.ftp.login(user, pw)
  131. print("Response: ", resp)
  132.  
  133.  
  134. def send_cmd(self, processed_in):
  135. if processed_in == 'ls':
  136. return_code = self.ftp.retrlines(processed_in, callback=self.response_handle)
  137. print("Returned: ", return_code)
  138. elif processed_in.startswith('STOR'):
  139. with open('test_hex.xml', 'rb') as f:
  140. self.ftp.storbinary('STOR test_hex.xml', f)
  141.  
  142. def response_handle(self, res):
  143. self.recent_response.append( res)
  144.  
  145. def server_response(self):
  146. return self.recent_response
  147.  
  148.  
  149.  
  150. class UserInputHandler(object):
  151.  
  152. def __init__(self) -> None:
  153. super().__init__()
  154.  
  155.  
  156. def get_user_input(self):
  157. user_input = input(">>>")
  158. return self.handle_user_input(user_input)
  159.  
  160.  
  161. def handle_user_input(self, user_input):
  162. if not self.valid_user_input(user_input):
  163. self.input_error_print(user_input)
  164. return None
  165.  
  166. return self.basic_process_input(user_input).split()
  167.  
  168.  
  169. def valid_user_input(self, user_input):
  170. return True
  171.  
  172.  
  173. def input_error_print(self, user_input):
  174. print("NOT SO GOOD")
  175.  
  176.  
  177. def basic_process_input(self, user_input):
  178. return user_input
  179.  
  180.  
  181. if __name__ == '__main__':
  182. client = FTPCLient(host, port)
  183. client.run()
Add Comment
Please, Sign In to add comment