OK I think it's an odd time to code and I'll be putting everything in one script.
So let's call this website blocker.py.
And yeah again
The idea is that I'll be accessing this host file which is this directory.
If you are on Windows you'll get it here. If you are on a Mac or Linux that would be directly at this
folder and then you have the host file.
So you get the idea and so let's keep that path in a variable.
Let's say hosts path and that would be equal to I had copied this earlier.
So I'm just pasting it here. So on Windows
these are backslashes on the Mac or Linux you'd have forward slash and then etc and then you have of course the hosts
name so the name of the file.
If you are on Windows you should be careful about something. These back slashes
sometimes you might have something like let's say N. A folder that starts with the N character.
What that will do is Python will read this as a special character.
And what this special character means is It means a break line.
So when you pass backslash and Python thinks you're passing a Python keyword not actually a string.
In that case what you could do is you could pass R prefix there which raw.
That means Remove this.
With this you'd tell Python that you're actually passing a raw string in there so you're not planning to pass any
brake lines in there like a backslash.
And I hope that is clear.
Or alternatively you can remove this and you can add two backslashes in each of these directories separators.
.
So that would be an alternative solution.
But let's keep it like this.
Good. And maybe two other variables let's say redirect equals to this string.
So the I.P where your browse visit will be redirected when you browse Facebook or other things that
you find distracting.
And then let's say website so you need to pass the websites that you want to block.
That could be a list.
So I'm calling this website list here.
Here I'll pass the www.Facebook.com.
And also you want to pass Facebook.com as well because sometimes these websites use 3 w there
for their domain but sometimes they don't.
So you want to pass both of these domains in the host file and then maybe my hotmail account.
So how do I know that this is the domain?
Well if I typed Hotmail.com and I can see that Hotmail.com is sending me to this domain in
the browser so I can see this domain in the browser.
This opens up my email displays.
So this is what you want to pass.
You want to see the final landing domain and maybe the other version.
And yeah, we should be good to go. We have a list with four elements.
That means the host file we will have four lines.
I currently have this Facebook's here. So I am gonna save the host again with all those.
And I'll make sure I don't have any Python scripts running here in the processes.
I don't have any.
And I was afraid that I had a running script, a Python script there.
So that's why I checked in the processes.
So how are we going to do this?
Let me put this here and this here.
Well our goal is to have this this file modified at let's say 8 a.m..
So at that time I want these four Web sites these four lines to be added in here together with this IP.
And then I want these four lines to be removed at let's say 4:00 p.m. and there are maybe two ways to do
this.
One way would be to write a simple Python script that all it does is it opens this file.
Let's say with open host path as file and then write these elements of the list in that file in that host file.
And that's all.
And then what you do is you may use this task scheduler on Windows or Cron on Linux or other
packages that do schedules tasks and you may set this Python scripts to run in certain times.
That is one solution.
But why would I use third party tools when I know Python.
I mean I know Python better than these tools and I'm not 100 percent sure that with these tools I'll
do exactly what I'm after.
But with Python that is a programming language if you set to doing something if you think about something
that's almost a hundred percent sure that you will do that because you have a lot of flexibility
with a programming language so it's good that if you know programming language then you should seek
for a native solution.
And yeah, that's what I'll do in this case. I will not just write a simple script that adds to these four
lines of code in this host file.
Instead I'll have this script running all the time and to do that I'll use a while loop.
While true and then everything goes inside this while loop. What while loop does
is it execute an action
very very fast so it depends on your processor speed.
But in my case for instance what this will do it will print out one every
Let me guess, one millisecond.
So it depends on your processor speed but maybe faster then one millisecond.
And let me open a command line and execute this script.
Shift right click here.
Or command window here. This terminal plus package of atom is having a problem of the moment.
It has a bug so I'll be using a simple command line.
Terminal plus is probably more convenient but this shouldn't be a problem. So Python
and the script name is website blocker. And yeah, one is being printed out as you see. Control C to interrupt the
process.
What I'm thinking about is to have some code here that checks the time every millisecond.
So that would be something like East time between 8 a.m. and 4 p.m..
If it, then keep these lines in this host file. If it's not then remove these lines from the host file.
And that action would be performed I am saying every millisecond.
But that's not a very smart thing to do.
Why would you want your program to run every millisecond.
That doesn't sound very memory efficient.
So what we could do here is a simple trick.
We import time and then here we say time sleep and let's say 5.
This time we execute this and one we will be printed every 5 seconds.
So that's it.
Which means if we write now the code that accesses these lines here in this host file, that code will run every five
seconds and not every millisecond.
So that's enough accuracy for us.
That means that if time is 4 pm and then it goes four and one second we will still have this line.
We will have this website blocked yet 4 pm plus one second so there will be a delay of 5 seconds which
is not a problem.
And you can also put this higher number so you can make it 5 minutes which should be 300 300 seconds
but let me keep it 5. 5 is good.
5 seconds.
I think this is starting to make more sense for you.
I would like to stop here. In the next lecture
we will go ahead and write the code that writes these lines in this host file.