Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import threading
  2. import queue
  3. import time
  4. import re
  5. import pprint
  6. import ftplib
  7. import socket
  8.  
  9.  
  10. list_unvalid = []
  11. list_valid = []
  12.  
  13. mylock = threading.RLock()
  14.  
  15. class StatusControl():
  16.  
  17. def __init__(self, data):
  18. self.data = data
  19. self.data_count = len(data)
  20. self.total_processed = 0
  21. self.percent_diff = 5
  22. self.last_percent = 0
  23. self.total_valid = 0
  24.  
  25. def addTotal(self):
  26. self.total_processed += 1
  27.  
  28. def getDataLen(self):
  29. return self.data_count
  30.  
  31. def getCurrPercent(self):
  32. return self.last_percent
  33.  
  34. def checkPercentDiff(self):
  35. current_percent = int(( self.total_processed / self.data_count ) * 100)
  36. if current_percent - self.last_percent >= self.percent_diff:
  37. self.last_percent = current_percent
  38. return True
  39. return False
  40.  
  41. def addValid(self):
  42. self.total_valid += 1
  43.  
  44. def worker_function(input_queue, output_queue, status_control):
  45. global list_unvalid, list_valid
  46.  
  47. while True:
  48. try:
  49. data = input_queue.get_nowait()
  50. except queue.Empty:
  51. break
  52. #print("checking " + str(data))
  53. try:
  54.  
  55. #line = 'username:password@my.domain.com/dir/aaa/aaa domain.com'
  56. data = data.replace('ftp://', '')
  57. login_data = re.findall('(.+?):(.+?)@(.+?)(/.[^s]+)s*(.*)', data, flags=re.IGNORECASE)
  58. print(login_data)
  59. login = login_data[0][0]
  60. password = login_data[0][1]
  61. host = login_data[0][2]
  62. directory = login_data[0][3]
  63. try:
  64. url = login_data[0][4]
  65. except IndexError:
  66. url = 'http://' + host
  67. nhost = socket.gethostbyname(host)
  68. ftp_connection = ftplib.FTP(nhost, login, password, timeout=35)
  69. #ftp_connection.login()
  70. ftp_connection.cwd(directory)
  71. dirs = ftp_connection.nlst()
  72. print(dirs)
  73. ftp_connection.close()
  74. status_control.addTotal()
  75. mylock.acquire()
  76. diff = status_control.checkPercentDiff()
  77. if diff == True:
  78. print(status_control.getCurrPercent() + '%')
  79. mylock.release()
  80. output_queue.put_nowait(data)
  81.  
  82. except Exception as e:
  83. print(str(e))
  84.  
  85. input_queue.task_done()
  86.  
  87.  
  88. def main():
  89. start_t = time.time()
  90.  
  91. f = open('ftp.txt', 'r')
  92. lines = f.readlines()
  93. lines = [line.strip() for line in lines]
  94.  
  95.  
  96. in_queue = queue.Queue()
  97. out_queue = queue.Queue()
  98.  
  99. for line in lines:
  100. if(line.strip() == ''):
  101. continue
  102. data = line
  103. in_queue.put_nowait(data)
  104.  
  105. status_control = StatusControl(lines)
  106.  
  107. worker_threads = 100
  108. threads = []
  109. for thr in range(worker_threads):
  110. thread = threading.Thread(target=worker_function, args=(in_queue, out_queue, status_control))
  111. thread.daemon = True
  112. threads.append(thread)
  113. thread.start()
  114.  
  115. for thread in threads:
  116. thread.join()
  117.  
  118. out_list = []
  119.  
  120.  
  121. while True:
  122. try:
  123. info = out_queue.get_nowait()
  124. out_list.append(info)
  125. except queue.Empty:
  126. break
  127. print(out_list)
  128. total_time = time.time() - start_t
  129. print('Total time ' + str(total_time) + ' seconds')
  130.  
  131. if __name__ == '__main__':
  132. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement