Advertisement
chingchonghackerma

Untitled

Feb 1st, 2022
1,627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. import datetime
  2. import time
  3. import elevate
  4. import os
  5.  
  6.  
  7. elevate.elevate()
  8.  
  9. host_path = r"C:\Windows\System32\drivers\etc\hosts" #r đằng trước string để coi như cái j viết trong string máy sẽ dùng như thế. Search raw string python
  10.  
  11. redirect = "127.0.0.1"
  12.  
  13. website_list = [] #các website để block
  14.  
  15. exe_list = ["msedge.exe", "notepad.exe"]
  16.  
  17. def CheckTime(time_start, time_end): #check xem thời gian hiện tại có trong thời gian gian không
  18.     time_start_hour = int(time_start[:2])
  19.     time_start_min = int(time_start[-2:])
  20.     datetime_time_start = datetime.datetime(datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day, time_start_hour, time_start_min)
  21.    
  22.     time_end_hour = int(time_end[:2])
  23.     time_end_min = int(time_end[-2:])
  24.     datetime_time_end = datetime.datetime(datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day, time_end_hour, time_end_min)
  25.  
  26.     if datetime_time_start < datetime.datetime.now() < datetime_time_end:
  27.         return True
  28.    
  29.     return False
  30.  
  31. def BlockWebsite(time_start, time_end): #block từ time_start -> time_end (2 cái này là string) vd: "18:00"
  32.  
  33.     if CheckTime(time_start, time_end):
  34.         print("websites still blocked")
  35.        
  36.         #code block websites (viết vào file hosts)
  37.         with open(host_path, 'r+') as file:
  38.             file_content = file.read()
  39.             for website in website_list:
  40.                 if website in file_content:
  41.                     pass
  42.                 else:
  43.                     file.write(redirect + " " + website + '\n')
  44.     else:
  45.         with open(host_path, "r+") as file:
  46.             file_content = file.readlines()
  47.             file.seek(0)
  48.             for line in file_content:
  49.                 if not any(website in line for website in website_list):
  50.                     file.write(line)
  51.             file.truncate()
  52.         #print('websites not blocked')
  53.  
  54. def BlockExe(time_start, time_end): #block từ time_start -> time_end (2 cái này là string) vd: "18:00"
  55.     if CheckTime(time_start, time_end):
  56.         print("exe still blocked")
  57.  
  58.         for exe in exe_list:
  59.             cmd_string = "taskkill /f /im  " + exe
  60.             os.system(cmd_string) # os.system là để chạy command trên cmd
  61.  
  62. time_start_global = "00:00"
  63. time_end_global = "05:40"
  64.  
  65.  
  66.  
  67.  
  68. if __name__ == "__main__":
  69.     while True:
  70.         BlockExe(time_start_global, time_end_global)
  71.         BlockWebsite(time_start_global, time_end_global)
  72.         time.sleep(1)
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement