Guest User

Untitled

a guest
Dec 5th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.62 KB | None | 0 0
  1. # Team SwagLords
  2. # Christian Williams
  3. # Kendrick Hillian
  4. # CSC 311
  5. # 09/09/2016
  6. # FTP Client
  7.  
  8.  
  9. import socket, getpass, zlib, gzip, os, glob
  10.  
  11.  
  12. class FTPClient:
  13. # Class variables
  14. directory = 'C:\\Downloads\\'
  15. server = socket.gethostname()
  16. port = 8075
  17. binary_bool = True
  18. ascii_bool = False
  19. compression_bool = False
  20. encryption_bool = False
  21. debug_bool = False
  22. get_bool = False
  23.  
  24. # Multiple files being sent
  25. multi_file_bool = False
  26. file_list = []
  27.  
  28.  
  29. def __init__(self):
  30. self.login()
  31. self.main()
  32.  
  33.  
  34. def login(self):
  35. while True:
  36. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  37. sock.connect((self.server,self.port))
  38. print 'Username: anon'
  39. user_input = str(getpass.getpass('Enter the password:\n>> '))
  40. while user_input == '':
  41. user_input = str(getpass.getpass('Enter the password:\n>> '))
  42. sock.sendall('pass ' + user_input.lower())
  43. response = sock.recv(1024)
  44. # Correct password
  45. if response == 'connected':
  46. print 'Connected.'
  47. sock.close()
  48. break
  49. # Incorrect input reset state of the client
  50. elif response == 'inv_c':
  51. self.binary_bool = True
  52. self.ascii_bool = False
  53. self.compression_bool = False
  54. self.encryption_bool = False
  55. self.multi_file_bool = False
  56. self.debug_bool = False
  57. self.get = False
  58. print 'Error. Re-enter the password.'
  59. sock.close()
  60.  
  61. def main(self):
  62. while True:
  63. # Multiple files are being sent
  64. if self.multi_file_bool:
  65. if self.get_bool:
  66. for file_name in self.file_list:
  67. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  68. s.connect((self.server, self.port))
  69. s.sendall('get ' + file_name)
  70. self.receive_file(s, file_name)
  71. s.close()
  72. self.multi_file_bool = False
  73. self.get_bool = False
  74. else:
  75. for file_name in self.file_list:
  76. #print self.file_list
  77. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  78. s.connect((self.server, self.port))
  79. if os.path.isfile(self.directory + str(file_name)):
  80.  
  81. s.sendall('put '+ str(file_name))
  82. response = s.recv(1024)
  83. if response == 'received':
  84. self.put(s, self.directory + str(file_name))
  85. s.close()
  86. ## response = s.recv(1024)
  87. ## if response == 'received':
  88. ## print file_name + ' placed on the server.'
  89. elif os.path.isfile(str(file_name)):
  90. #Send command to server
  91. print file_name
  92. s.sendall('put '+ str(file_name))
  93.  
  94. if response == 'received':
  95. self.put(s, self.directory + str(file_name))
  96. s.close()
  97. ## response = s.recv(1024)
  98. ## if response == 'received':
  99. ## print file_name + ' placed on the server.'
  100. ## else:
  101. ## print 'Error file not found.'
  102. s.close()
  103. self.multi_file_bool = False
  104. else:
  105. user_input = ''
  106. # Create Socket
  107. s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  108. s1.connect((self.server,self.port))
  109.  
  110. # Prompt user for command
  111. user_input = str(raw_input('Enter a command:\n>> ')).strip()
  112. input_array = user_input.lower().split()
  113.  
  114.  
  115. if len(input_array) == 1:
  116. if input_array[0] == 'ls':
  117. #Send command to server
  118. s1.sendall(user_input.lower())
  119. data = s1.recv(1024)
  120. print data
  121. elif input_array[0] == 'binary':
  122. #Send command to server
  123. s1.sendall(user_input.lower())
  124. self.ascii_bool = False
  125. self.binary_bool = True
  126. data = s1.recv(1024)
  127. if data == 'binary':
  128. print 'Binary mode activated.'
  129. elif input_array[0] == 'ascii':
  130. #Send command to server
  131. s1.sendall(user_input.lower())
  132. self.binary_bool = False
  133. self.ascii_bool = True
  134. data = s1.recv(1024)
  135. if data == 'ascii':
  136. print 'Ascii mode activated.'
  137. elif input_array[0] == 'compression':
  138. #Send command to server
  139. s1.sendall(user_input.lower())
  140. self.compression_bool = True
  141. data = s1.recv(1024)
  142. if data == 'compression':
  143. print 'Compression = On'
  144. elif input_array[0] == 'encryption':
  145. #Send command to server
  146. s1.sendall(user_input.lower())
  147. self.encryption_bool = True
  148. data = s1.recv(1024)
  149. if data == 'encryption':
  150. print 'Encryption = On'
  151. elif input_array[0] == 'normal':
  152. #Send command to server
  153. s1.sendall(user_input.lower())
  154. self.compression_bool = False
  155. self.encryption_bool = False
  156. data = s1.recv(1024)
  157. if data == 'normal':
  158. print 'Compression = Off and Encryption = Off'
  159. elif input_array[0] == 'pwd':
  160. #Send command to server
  161. s1.sendall(user_input.lower())
  162. output = s1.recv(1024)
  163. print output
  164. elif input_array[0] == 'quit':
  165. #Send command to server
  166. s1.sendall(user_input.lower())
  167. break
  168. elif input_array[0] == 'quitserver':
  169. #Send command to server
  170. s1.sendall(user_input.lower())
  171. break
  172.  
  173. elif len(input_array) == 2:
  174. if input_array[0] == 'mget':
  175. s1.sendall(user_input.lower())
  176. self.file_list = s1.recv(1024).split('\n')
  177. self.multi_file_bool = True
  178. self.get_bool = True
  179. elif input_array[0] == 'get':
  180. #Send command to server
  181. s1.sendall(user_input.lower())
  182. self.receive_file(s1, input_array[1])
  183. elif input_array[0] == 'ls':
  184. #Send command to server
  185. s1.sendall(user_input.lower())
  186. data = s1.recv(1024)
  187. print data
  188. elif input_array[0] == 'cd':
  189. #Send command to server
  190. s1.sendall(user_input.lower())
  191. response = s1.recv(1024)
  192. if response == 'inv_c':
  193. print 'Directory doesn\'t exist.'
  194. else:
  195. print 'Directory changed to: ' + response
  196. elif input_array[0] == 'mput':
  197. self.multi_file_bool = True
  198. self.file_list = self.list_files(self.directory + input_array[1]).split('\n')
  199.  
  200. elif input_array[0] == 'put':
  201. if os.path.isfile(self.directory + input_array[1]):
  202. #Send command to server
  203. s1.sendall(user_input.lower())
  204.  
  205. self.put(self.directory + input_array[1])
  206. s1.close()
  207. #s1.shutdown(socket.SHUT_WR)
  208. ## response = s1.recv(1024)
  209. ## if response == 'received':
  210. ## print input_array[1] + ' placed on the server.'
  211. elif os.path.isfile(input_array[1]):
  212. #Send command to server
  213. s1.sendall(user_input.lower())
  214. self.put(input_array[1])
  215. s1.close()
  216. #s1.shutdown(socket.SHUT_WR)
  217. ## response = s1.recv(1024)
  218. ## if response == 'received':
  219. ## print input_array[1] + ' placed on the server.'
  220. else:
  221. print 'Error file not found.'
  222. elif len(input_array) > 2:
  223. if input_array[0] == 'mget':
  224. self.file_list = input_array[1:]
  225. self.multi_file_bool = True
  226. self.get_bool = True
  227. elif input_array[0] == 'mput':
  228. self.file_list = input_array[1:]
  229. self.multi_file_bool = True
  230.  
  231.  
  232. s1.close()
  233. s1.close()
  234. print 'Connection closed.'
  235.  
  236. def list_files(self, dir_path):
  237. output = ''
  238.  
  239. file_list = glob.glob(dir_path)
  240. for file_name in file_list:
  241. #print file_list
  242. if os.path.isfile(file_name):
  243. path_list = file_name.split('\\')
  244. output += path_list[len(path_list) - 1].strip() + '\n'
  245. return output[:-1]
  246.  
  247.  
  248.  
  249. def receive_file(self, conn, file_name):
  250. data = conn.recv(1024)
  251. if data == 'inv_f':
  252. print 'File not found.'
  253. else:
  254. if self.binary_bool:
  255. if self.compression_bool:
  256. if not self.encryption_bool:
  257. #
  258. if file_name[0:2].lower() == 'c:\\':
  259. file_out = open(file_name + '.gz', 'wb')
  260. else:
  261. file_out = open(self.directory + file_name + '.gz', 'wb')
  262. while True:
  263. if data == '':
  264. file_out.close()
  265. if file_name[0:2].lower() == 'c:\\':
  266. number = os.path.getsize(file_name + '.gz')
  267. else:
  268. number = os.path.getsize(self.directory + file_name + '.gz')
  269. print number
  270. if file_name[0:2].lower() == 'c:\\':
  271. file_out = gzip.open(file_name + '.gz', 'rb')
  272. else:
  273. file_out = gzip.open(self.directory + file_name + '.gz', 'rb')
  274. output = file_out.read()
  275. file_out.close()
  276. print 'File received.'
  277. if file_name[0:2].lower() == 'c:\\':
  278. file_1 = open(file_name, 'wb')
  279. else:
  280. file_1 = open(self.directory + file_name, 'wb')
  281. file_1.write(output)
  282. file_1.close()
  283. if file_name[0:2].lower() == 'c:\\':
  284. number = os.path.getsize(file_name )
  285. else:
  286. number = os.path.getsize(self.directory + file_name)
  287. if file_name[0:2].lower() == 'c:\\':
  288. os.remove(file_name + '.gz')
  289. else:
  290. os.remove(self.directory + file_name + '.gz')
  291. break
  292. else:
  293. file_out.write(data)
  294. data = conn.recv(1024)
  295. else:
  296. # No Encryption
  297. if not self.encryption_bool:
  298. if file_name[0].lower() == 'c:\\':
  299. file_out = open(file_name, 'wb')
  300. else:
  301. file_out = open(self.directory + file_name, 'wb')
  302. while True:
  303. if data == '':
  304. file_out.close()
  305. print 'File received.'
  306. break
  307. else:
  308. file_out.write(data)
  309. data = conn.recv(1024)
  310.  
  311. elif self.ascii_bool:
  312. if self.compression_bool:
  313. if not self.encryption_bool:
  314. if file_name[0].lower() == 'c:\\':
  315. file_out = open(file_name + '.gz', 'w')
  316. else:
  317. file_out = open(self.directory + file_name + '.gz', 'w')
  318. while True:
  319. if data == '':
  320. file_out.close()
  321. if file_name[0].lower() == 'c:\\':
  322. number = os.path.getsize(file_name + '.gz')
  323. else:
  324. number = os.path.getsize(self.directory + file_name + '.gz')
  325. print number
  326. if file_name[0].lower() == 'c:\\':
  327. file_out = gzip.open(file_name + '.gz', 'r')
  328. else:
  329. file_out = gzip.open(self.directory + file_name + '.gz', 'r')
  330. output = file_out.read()
  331. file_out.close()
  332. print 'File received.'
  333. if file_name[0].lower() == 'c:\\':
  334. file_1 = open(file_name, 'w')
  335. else:
  336. file_1 = open(self.directory + file_name, 'w')
  337. file_1.write(output)
  338. file_1.close()
  339. if file_name[0].lower() == 'c:\\':
  340. number = os.path.getsize(file_name )
  341. else:
  342. number = os.path.getsize(self.directory + file_name)
  343. if file_name[0].lower() == 'c:\\':
  344. os.remove(file_name + '.gz')
  345. else:
  346. os.remove(self.directory + file_name + '.gz')
  347. break
  348. else:
  349. file_out.write(data)
  350. data = conn.recv(1024)
  351.  
  352. else:
  353. # No compression.
  354. if not self.encryption_bool:
  355. if file_name[0].lower() == 'c:\\':
  356. file_out = open(file_name, 'w')
  357. else:
  358. file_out = open(self.directory + file_name, 'w')
  359. while True:
  360. if data == '':
  361. file_out.close()
  362. print 'File received.'
  363. break
  364. else:
  365. file_out.write(data)
  366. data = conn.recv(1024)
  367.  
  368.  
  369. def put(self, conn, file_name):
  370.  
  371. if os.path.isfile(self.directory + file_name):
  372. try:
  373. file_in = open(self.directory + file_name, 'rb')
  374. packet = file_in.read(1024)
  375. while True:
  376. if packet == '':
  377. break
  378. conn.sendall(packet)
  379. packet = file_in.read(1024)
  380. print 'File sent.'
  381. file_in.close()
  382. except IOError:
  383. print 'File not found.'
  384. elif os.path.isfile(file_name):
  385. try:
  386. file_in = open(file_name, 'rb')
  387. packet = file_in.read(1024)
  388. while True:
  389. if packet == '':
  390. break
  391. conn.sendall(packet)
  392. packet = file_in.read(1024)
  393. print 'File sent.'
  394. file_in.close()
  395. except IOError:
  396. print 'File not found.'
  397. else:
  398. print 'File not found.'
  399.  
  400.  
  401. if __name__ == '__main__':
  402. client = FTPClient()
  403. del client
Add Comment
Please, Sign In to add comment