4

so I have a problem trying to run this python code as administrator so I am not able to access and write on host file. Can anyone help me? I have looked through many of other questions but non of them seem to work.

Host File Directory: C:\Windows\System32\Drivers\etc\hosts

(Such as) Request UAC elevation from within a Python script?

Some of these answers actually work on prompting to get administrator access, but it still doesn't give permission to my program. The only way I figured out is to run python shell as administrator first and then run the code or run the command prompt as administrator and open python file with command prompt.

WEBSITE

https://boostlog.io/@faisalnad/create-a-website-blocker-with-python-5afe86ff47018500491f4898

This program is made for blocking website.

import time 
from datetime import datetime as dt 

# change hosts path according to your OS 
hosts_path = r”C:\Windows\System32\Drivers\etc\hosts”
# localhost's IP 
redirect = "127.0.0.1"

# websites That you want to block 
website_list = ["www.facebook.com","facebook.com", 
      "dub119.mail.live.com","www.dub119.mail.live.com", 
      "www.gmail.com","gmail.com"] 

while True: 

    # time of your work 
    if dt(dt.now().year, dt.now().month, dt.now().day,8)  < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day,16): 
        print("Working hours...") 
        with open(hosts_path, 'r+') as file: 
            content = file.read() 
            for website in website_list: 
                if website in content: 
                    pass
                else: 
                    # mapping hostnames to your localhost IP address 
                    file.write(redirect + " " + website + "\n") 
    else: 
        with open(hosts_path, 'r+') as file: 
            content=file.readlines() 
            file.seek(0) 
            for line in content: 
                if not any(website in line for website in website_list): 
                    file.write(line) 

            # removing hostnmes from host file 
            file.truncate() 

        print("Fun hours...") 
    time.sleep(5) 

This is the error:

Working hours...
Traceback (most recent call last):
  File "C:\Users\Key\Desktop\random project.py", line 19, in <module>
    with open(hosts_path, 'r+') as file:
PermissionError: [Errno 13] Permission denied: 'C:\\Windows\\System32\\Drivers\\etc\\hosts'

FILE DIRECTORY enter image description here

Key
  • 340
  • 2
  • 4
  • 10
  • Check if AV is running, it may be locking the `hosts` files, also make sure the script is running privileged. Note: `hosts_path` seems hard-coded and `c` isn't always the default OS install drive. Remember to issue a `ipconfig /flushdns`after changing the hosts file. The project seems interesting! Blocking students from accessing social medial on study time?! great ! – Pedro Lobito Dec 07 '18 at 06:02

1 Answers1

0

You can add write permission for the user under which your program runs following this link to add permission to the host file

Antonino
  • 3,178
  • 3
  • 24
  • 39
Amit Nanaware
  • 3,203
  • 1
  • 6
  • 19