python script website blocker is not working even though the script compiles correctly
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
add a comment |
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
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: nohttps://...
.
– Klaus D.
Nov 25 '18 at 5:10
add a comment |
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
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
python hosts
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: nohttps://...
.
– Klaus D.
Nov 25 '18 at 5:10
add a comment |
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: nohttps://...
.
– 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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 25 '18 at 5:09
Uku LoskitUku Loskit
30.7k86981
30.7k86981
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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