Good, I have the script running at the moment and ass you can see the loop is doing its job.
So it's printing out the working hours in the command line here every five seconds.
And now it's 15 past 12.
So that is between 8:00 a.m. and 4:00 p.m..
Now I will go ahead and add some lines here.
So I'm thinking of keeping these statements here, this print statements but also adding more lines here
which corresponds to file handling methods which will access these hosts file and modify it depending
on the time.
Now something that you should be aware about is this host file can only be modified by the administrator
which is if I run the Python program I would have to open a command line to run the command line
as administrator.
So if you're on a Linux or Mac you would do the same by using sudo for instance.
In this case I opened this command line and by just right clicking in here so shift right click and open
command window here but that will not run the command line as an administrator.
Which underlies that with this simple console
I cannot access these host file.
So in other words you need to run your Python program with a command line or a terminal where you
have administrative rights and for the sake of building a program it's more wise to just copy this
host file and just paste it here where the script file is.
The reason I'm doing that is because here I'll not have to open a command line as administrator every
time I test my script.
So once I'm sure that things are working well then I just change the path.
So at the moment I will not work with this path. I will work with this.
Let's say hosts temp for temporary which is equal to the host file name because host
is just here in the same folder with web site blocker.py.
And besides making a working copy of this host file, you also want to make a backup copy of the host file just
in case you make some errors in your Python code and you delete the file or you just
delete some content from your existing host file.
And I'm just going to copy this and just put it here.
So paste it there in my D drive and yeah, I'll just close the path of the real host file for now until
I have a working script that modifies this host file successfully.
This one here.
Great.
Now.
I can open this actually here on the right. Close here.
Good.
I think this is a good layout for you to do see the code well. And what do we have to do now?
Oh well we are checking the current time against these two other date time values and we have two scenarios.
So in the first scenario we are between these two times.
And in that case where we are between these two times we want to make sure that we have four lines of
text here in a host file where each corresponds with each of these least items of course together with
redirecting IP in the front. So the same format as you see here but without the comment tag. And yeah,
we can go ahead and open this host file now with the open method and write the list items in the host file.
However we may also want to check first if these four lines already exist in the host file because
the host file can have two statues.
So the first status is the host file is without these four lines there.
The other status is the host file has this these four lines therefore you want to check for them.
And to check these lines
you first need to read the host file in Python.
So you want to apply read method to your file and then maybe add a loop that iterates through this list
and in each iteration you apply a conditional that says if a list item in content where content
Is a text you have from the host file.
So if that item is in the content then don't do anything otherwise add that item in the host file.
So let's go ahead and do that with open
hosts path. Sorry, that would be host temp. Host path is a path that we will be using later.
So host temp and we want to open that file using
Well if we use the read method so R, we could read the file content but we wouldn't be able to write within
this statement within the with statement.
And if you pass W you wouldn't be able to read. So the middle solution that is to use our R plus.
With that you would be able to read and write.
So as file.
And yeah, before writing anything we want to first check there.
So let's say content equals file.read and what content will contain is the entire text
of the host file.
And actually we could print that out just to see what we have this far.
So normally you don't want to print that out but in this case just for demonstration.
And it's also good to check where we are this far.
So python website blocker and so yeah, we got the text of host file printed out.
And before that a working hours was printed out and now every five seconds the working hours and the
content of the host file are being printed out.
So I'll stop this.
We're doing well.
Now that we have content we want to iterate through the website list. So for web site in website list.
For instance the first item will be this one.
So if Facebook dot com.
If website in content.
So if this is in this content, in this text and we are in the working hours in this part of the if conditional.
So if Facebook is there already we don't want to do anything because that will already block all our
Facebook website so we pass that which means that Python will go ahead and check for other lines.
So it will go here so it says else will not be executed in this case because if was executed.
So if that is not executed then Python goes to the next line and it sleeps for five seconds and then once
it sleeps for five seconds the loop starts all over again.
The condition is being evaluated again.
Working hours is printed out.
If we are between these two times then we open the file again in the second iteration of the while loop
and we read the text and then we iterate in our list and check if any of our list of items is
in the text of the host file.
And then we pass to the next lines if it is not there and if it is there we want to file.right.
You do something like website and yeah, we are almost there.
We can check what we got this far so lets you understand. So I executed this and now you see that
www.Facebook.com was added in the host file.
And then the next item of the list, Facebook.com
and then my Hotmail account and so on.
So the four items of the list which means the structure of our code is working well. We just need to work
a bit on the style on the text formatting.
So we are printing out the website
but we also want the redirect IP in there. Actually the first one should be redirect variable. This one.
And then according to the host file style
we should have a space there as you can see it say the IP address and the host name should be separated
by at least one space and that is what we will do.
So redirect what you can do here is plus one space within double quotes and then we have plus website
and to avoid the text being passed in one single line you want to pass here the backslash and special
character that will create brake lines between these text items.
Great.
Now I can check this.
Well nothing will happen because Facebook is already there.
So it is going to stop this and really this.
And yeah now it's working well.
And by the way if you have an antivirus in your system you probably have some warning there because
the host file is a bit sensitive.
So your antivirus will probably be notifying you that some program is modifying your host file.
If you want to you can just you can cancel this don't do any cleaning.
You don't have to clean it because you know that you're the one who is accessing that host file.
So cancel that and yeah it's working well
I believe. We are done with the first part or the conditional.
So now where we are in this time which is between these two hours.
But if you were let's say 14 here.
Stop this check again.
In this case we get printed out these phone hours but the text in the host file will be still there.
So what we want to do next is remove this text from the host file when this part of the conditional
is satisfied. I will do that
the next lecture, so see you there.