python script website blocker is not working even though the script compiles correctly












0















The code compiles fine and executes as its supposed to. But when I go to one of the websites in the list of website I created for websites that are supposed to be blocked, I am still able to go to that website. I can still go to www.facebook.com,even though its one of the five websites that my python script is supposed to block



import time
from datetime import datetime as dt

host_temp="hosts.txt"
host_path=r"C:WindowsSystem32driversetchosts"
redirect="127.0.0.1"


website_list= ["https://www.facebook.com/","www.facebook.com","facebook.com","dub119.mail.live.
com","www.dub119.mail.live.com"]

while True:
if dt(dt.now().year,dt.now().month,dt.now().day,0)<dt.now()< dt(dt.now().year,dt.now().month,dt.now().day,23):
print("Working hours...")
with open(host_path,'r+') as file:
content=file.read()
# print(content)
for website in website_list:
if website in content:
pass
else:
file.write(redirect+" "+website+"n")

else:
with open(host_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)
file.truncate()

print("Fun hours..")

time.sleep(4)


And I run this code as an Administrator so I know thats not the problem:enter image description here



"enter image description here










share|improve this question

























  • What is the contents of your hosts.txt file after running the script?

    – Flight Odyssey
    Nov 25 '18 at 3:42













  • I have a screen shot of the hosts files I just added to my original question

    – Joan Mitchell
    Nov 25 '18 at 4:00











  • You don't put URLs into that file, just hostnames. In other words: no https://....

    – Klaus D.
    Nov 25 '18 at 5:10
















0















The code compiles fine and executes as its supposed to. But when I go to one of the websites in the list of website I created for websites that are supposed to be blocked, I am still able to go to that website. I can still go to www.facebook.com,even though its one of the five websites that my python script is supposed to block



import time
from datetime import datetime as dt

host_temp="hosts.txt"
host_path=r"C:WindowsSystem32driversetchosts"
redirect="127.0.0.1"


website_list= ["https://www.facebook.com/","www.facebook.com","facebook.com","dub119.mail.live.
com","www.dub119.mail.live.com"]

while True:
if dt(dt.now().year,dt.now().month,dt.now().day,0)<dt.now()< dt(dt.now().year,dt.now().month,dt.now().day,23):
print("Working hours...")
with open(host_path,'r+') as file:
content=file.read()
# print(content)
for website in website_list:
if website in content:
pass
else:
file.write(redirect+" "+website+"n")

else:
with open(host_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)
file.truncate()

print("Fun hours..")

time.sleep(4)


And I run this code as an Administrator so I know thats not the problem:enter image description here



"enter image description here










share|improve this question

























  • What is the contents of your hosts.txt file after running the script?

    – Flight Odyssey
    Nov 25 '18 at 3:42













  • I have a screen shot of the hosts files I just added to my original question

    – Joan Mitchell
    Nov 25 '18 at 4:00











  • You don't put URLs into that file, just hostnames. In other words: no https://....

    – Klaus D.
    Nov 25 '18 at 5:10














0












0








0








The code compiles fine and executes as its supposed to. But when I go to one of the websites in the list of website I created for websites that are supposed to be blocked, I am still able to go to that website. I can still go to www.facebook.com,even though its one of the five websites that my python script is supposed to block



import time
from datetime import datetime as dt

host_temp="hosts.txt"
host_path=r"C:WindowsSystem32driversetchosts"
redirect="127.0.0.1"


website_list= ["https://www.facebook.com/","www.facebook.com","facebook.com","dub119.mail.live.
com","www.dub119.mail.live.com"]

while True:
if dt(dt.now().year,dt.now().month,dt.now().day,0)<dt.now()< dt(dt.now().year,dt.now().month,dt.now().day,23):
print("Working hours...")
with open(host_path,'r+') as file:
content=file.read()
# print(content)
for website in website_list:
if website in content:
pass
else:
file.write(redirect+" "+website+"n")

else:
with open(host_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)
file.truncate()

print("Fun hours..")

time.sleep(4)


And I run this code as an Administrator so I know thats not the problem:enter image description here



"enter image description here










share|improve this question
















The code compiles fine and executes as its supposed to. But when I go to one of the websites in the list of website I created for websites that are supposed to be blocked, I am still able to go to that website. I can still go to www.facebook.com,even though its one of the five websites that my python script is supposed to block



import time
from datetime import datetime as dt

host_temp="hosts.txt"
host_path=r"C:WindowsSystem32driversetchosts"
redirect="127.0.0.1"


website_list= ["https://www.facebook.com/","www.facebook.com","facebook.com","dub119.mail.live.
com","www.dub119.mail.live.com"]

while True:
if dt(dt.now().year,dt.now().month,dt.now().day,0)<dt.now()< dt(dt.now().year,dt.now().month,dt.now().day,23):
print("Working hours...")
with open(host_path,'r+') as file:
content=file.read()
# print(content)
for website in website_list:
if website in content:
pass
else:
file.write(redirect+" "+website+"n")

else:
with open(host_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)
file.truncate()

print("Fun hours..")

time.sleep(4)


And I run this code as an Administrator so I know thats not the problem:enter image description here



"enter image description here







python hosts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 3:59







Joan Mitchell

















asked Nov 25 '18 at 3:26









Joan MitchellJoan Mitchell

63




63













  • What is the contents of your hosts.txt file after running the script?

    – Flight Odyssey
    Nov 25 '18 at 3:42













  • I have a screen shot of the hosts files I just added to my original question

    – Joan Mitchell
    Nov 25 '18 at 4:00











  • You don't put URLs into that file, just hostnames. In other words: no https://....

    – Klaus D.
    Nov 25 '18 at 5:10



















  • What is the contents of your hosts.txt file after running the script?

    – Flight Odyssey
    Nov 25 '18 at 3:42













  • I have a screen shot of the hosts files I just added to my original question

    – Joan Mitchell
    Nov 25 '18 at 4:00











  • You don't put URLs into that file, just hostnames. In other words: no https://....

    – Klaus D.
    Nov 25 '18 at 5:10

















What is the contents of your hosts.txt file after running the script?

– Flight Odyssey
Nov 25 '18 at 3:42







What is the contents of your hosts.txt file after running the script?

– Flight Odyssey
Nov 25 '18 at 3:42















I have a screen shot of the hosts files I just added to my original question

– Joan Mitchell
Nov 25 '18 at 4:00





I have a screen shot of the hosts files I just added to my original question

– Joan Mitchell
Nov 25 '18 at 4:00













You don't put URLs into that file, just hostnames. In other words: no https://....

– Klaus D.
Nov 25 '18 at 5:10





You don't put URLs into that file, just hostnames. In other words: no https://....

– Klaus D.
Nov 25 '18 at 5:10












1 Answer
1






active

oldest

votes


















0














file.write(redirect+" "+website+"rn")



You need to use Windows line endings. Additionally note that the hosts file should include DNS entries, so facebook.com and www.facebook.com make sense, but URLs like https://.. don't.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464387%2fpython-script-website-blocker-is-not-working-even-though-the-script-compiles-cor%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    file.write(redirect+" "+website+"rn")



    You need to use Windows line endings. Additionally note that the hosts file should include DNS entries, so facebook.com and www.facebook.com make sense, but URLs like https://.. don't.






    share|improve this answer




























      0














      file.write(redirect+" "+website+"rn")



      You need to use Windows line endings. Additionally note that the hosts file should include DNS entries, so facebook.com and www.facebook.com make sense, but URLs like https://.. don't.






      share|improve this answer


























        0












        0








        0







        file.write(redirect+" "+website+"rn")



        You need to use Windows line endings. Additionally note that the hosts file should include DNS entries, so facebook.com and www.facebook.com make sense, but URLs like https://.. don't.






        share|improve this answer













        file.write(redirect+" "+website+"rn")



        You need to use Windows line endings. Additionally note that the hosts file should include DNS entries, so facebook.com and www.facebook.com make sense, but URLs like https://.. don't.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 5:09









        Uku LoskitUku Loskit

        30.7k86981




        30.7k86981
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464387%2fpython-script-website-blocker-is-not-working-even-though-the-script-compiles-cor%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen