1
00:00:00,540 --> 00:00:06,800
Good, I have the script running at the moment and ass you can see the loop is doing its job.

2
00:00:06,810 --> 00:00:12,960
So it's printing out the working hours in the command line here every five seconds.

3
00:00:13,680 --> 00:00:18,530
And now it's 15 past 12.

4
00:00:18,840 --> 00:00:21,950
So that is between 8:00 a.m. and 4:00 p.m..

5
00:00:21,970 --> 00:00:24,480
Now I will go ahead and add some lines here.

6
00:00:24,570 --> 00:00:33,270
So I'm thinking of keeping these statements here, this print statements but also adding more lines here

7
00:00:33,270 --> 00:00:43,110
which corresponds to file handling methods which will access these hosts file and modify it depending

8
00:00:43,170 --> 00:00:45,270
on the time.

9
00:00:45,330 --> 00:00:52,680
Now something that you should be aware about is this host file can only be modified by the administrator

10
00:00:53,160 --> 00:01:01,830
which is if I run the Python program I would have to open a command line to run the command line

11
00:01:02,490 --> 00:01:04,990
as administrator.

12
00:01:05,010 --> 00:01:11,000
So if you're on a Linux or Mac you would do the same by using sudo for instance.

13
00:01:11,040 --> 00:01:17,600
In this case I opened this command line and by just right clicking in here so shift right click and open

14
00:01:17,610 --> 00:01:22,450
command window here but that will not run the command line as an administrator.

15
00:01:22,660 --> 00:01:26,730
Which underlies that with this simple console

16
00:01:26,760 --> 00:01:30,140
I cannot access these host file.

17
00:01:30,510 --> 00:01:37,560
So in other words you need to run your Python program with a command line or a terminal where you

18
00:01:37,560 --> 00:01:45,270
have administrative rights and for the sake of building a program it's more wise to just copy this

19
00:01:45,360 --> 00:01:52,370
host file and just paste it here where the script file is.

20
00:01:52,770 --> 00:01:58,950
The reason I'm doing that is because here I'll not have to open a command line as administrator every

21
00:01:58,950 --> 00:02:00,940
time I test my script.

22
00:02:01,140 --> 00:02:06,720
So once I'm sure that things are working well then I just change the path.

23
00:02:06,720 --> 00:02:11,270
So at the moment I will not work with this path. I will work with this.

24
00:02:11,460 --> 00:02:21,390
Let's say hosts temp for temporary which is equal to the host file name because host

25
00:02:21,480 --> 00:02:26,300
is just here in the same folder with web site blocker.py.

26
00:02:26,300 --> 00:02:33,450
And besides making a working copy of this host file, you also want to make a backup copy of the host file just

27
00:02:33,450 --> 00:02:39,420
in case you make some errors in your Python code and you delete the file or you just

28
00:02:39,750 --> 00:02:42,420
delete some content from your existing host file.

29
00:02:42,540 --> 00:02:48,210
And I'm just going to copy this and just put it here.

30
00:02:48,750 --> 00:02:59,400
So paste it there in my D drive and yeah, I'll just close the path of the real host file for now until

31
00:02:59,400 --> 00:03:03,700
I have a working script that modifies this host file successfully.

32
00:03:03,760 --> 00:03:05,410
This one here.

33
00:03:05,550 --> 00:03:06,330
Great.

34
00:03:06,360 --> 00:03:07,410
Now.

35
00:03:07,890 --> 00:03:17,170
I can open this actually here on the right. Close here.

36
00:03:19,120 --> 00:03:19,880
Good.

37
00:03:19,890 --> 00:03:25,260
I think this is a good layout for you to do see the code well. And what do we have to do now?

38
00:03:25,260 --> 00:03:25,860

39
00:03:26,150 --> 00:03:34,740
Oh well we are checking the current time against these two other date time values and we have two scenarios.

40
00:03:34,750 --> 00:03:34,900

41
00:03:35,040 --> 00:03:38,070
So in the first scenario we are between these two times.

42
00:03:38,070 --> 00:03:45,150
And in that case where we are between these two times we want to make sure that we have four lines of

43
00:03:45,180 --> 00:03:53,640
text here in a host file where each corresponds with each of these least items of course together with

44
00:03:53,940 --> 00:04:04,050
redirecting IP in the front. So the same format as you see here but without the comment tag. And yeah,

45
00:04:04,050 --> 00:04:11,820
we can go ahead and open this host file now with the open method and write the list items in the host file.

46
00:04:11,820 --> 00:04:12,150

47

00:04:12,180 --> 00:04:19,700
However we may also want to check first if these four lines already exist in the host file because

48
00:04:19,710 --> 00:04:21,750
the host file can have two statues.

49
00:04:21,880 --> 00:04:27,490
So the first status is the host file is without these four lines there.

50
00:04:27,720 --> 00:04:35,150
The other status is the host file has this these four lines therefore you want to check for them.

51
00:04:35,490 --> 00:04:37,100
And to check these lines

52
00:04:37,230 --> 00:04:40,710
you first need to read the host file in Python.

53
00:04:41,040 --> 00:04:49,000
So you want to apply read method to your file and then maybe add a loop that iterates through this list

54
00:04:49,350 --> 00:04:57,060
and in each iteration you apply a conditional that says if a list item in content where content 

55
00:04:57,060 --> 00:05:00,070
Is a text you have from the host file.

56
00:05:00,270 --> 00:05:08,260
So if that item is in the content then don't do anything otherwise add that item in the host file.

57
00:05:08,520 --> 00:05:11,400
So let's go ahead and do that with open

58
00:05:14,440 --> 00:05:22,830
hosts path. Sorry, that would be host temp. Host path is a path that we will be using later.

59
00:05:22,880 --> 00:05:28,800
So host temp and we want to open that file using�

60
00:05:28,950 --> 00:05:39,570
Well if we use the read method so R, we could read the file content but we wouldn't be able to write within

61
00:05:39,610 --> 00:05:42,510
this statement within the with statement.

62
00:05:42,790 --> 00:05:49,660
And if you pass W you wouldn't be able to read. So the middle solution that is to use our R plus.

63
00:05:49,990 --> 00:05:53,290
With that you would be able to read and write.

64
00:05:53,340 --> 00:05:54,300
So as file.

65
00:05:57,110 --> 00:06:01,380
And yeah, before writing anything we want to first check there.

66
00:06:01,720 --> 00:06:11,580
So let's say content equals file.read and what content will contain is the entire text

67
00:06:12,220 --> 00:06:13,780
of the host file.

68
00:06:14,230 --> 00:06:21,510
And actually we could print that out just to see what we have this far.

69
00:06:21,750 --> 00:06:27,690
So normally you don't want to print that out but in this case just for demonstration.

70
00:06:27,720 --> 00:06:31,020
And it's also good to check where we are this far.

71
00:06:31,370 --> 00:06:40,340
So python website blocker and so yeah, we got the text of host file printed out.

72
00:06:41,590 --> 00:06:48,180
And before that a working hours was printed out and now every five seconds the working hours and the

73
00:06:48,190 --> 00:06:50,490
content of the host file are being printed out.

74
00:06:50,760 --> 00:06:53,160
So I'll stop this.

75
00:06:53,160 --> 00:06:53,920
We're doing well.

76
00:06:56,990 --> 00:07:10,600
Now that we have content we want to iterate through the website list. So for web site in website list.

77
00:07:10,600 --> 00:07:12,680

78
00:07:13,380 --> 00:07:16,160
For instance the first item will be this one.

79
00:07:16,480 --> 00:07:19,620
So if Facebook dot com.

80
00:07:19,720 --> 00:07:27,110
If website in content.

81
00:07:27,490 --> 00:07:38,800
So if this is in this content, in this text and we are in the working hours in this part of the if conditional.

82
00:07:38,800 --> 00:07:39,220

83
00:07:39,220 --> 00:07:46,400
So if Facebook is there already we don't want to do anything because that will already block all our

84
00:07:46,380 --> 00:07:55,590
Facebook website so we pass that which means that Python will go ahead and check for other lines.

85
00:07:55,840 --> 00:08:06,790
So it will go here so it says else will not be executed in this case because if was executed.

86
00:08:06,810 --> 00:08:07,290

87
00:08:07,650 --> 00:08:14,850
So if that is not executed then Python goes to the next line and it sleeps for five seconds and then once

88
00:08:14,880 --> 00:08:19,040
it sleeps for five seconds the loop starts all over again.

89
00:08:19,040 --> 00:08:22,390
The condition is being evaluated again.

90
00:08:22,380 --> 00:08:24,210
Working hours is printed out.

91
00:08:24,220 --> 00:08:32,380
If we are between these two times then we open the file again in the second iteration of the while loop

92
00:08:32,950 --> 00:08:40,260
and we read the text and then we iterate in our list and check if any of our list of items is

93
00:08:40,260 --> 00:08:42,570
in the text of the host file.

94
00:08:43,600 --> 00:08:54,190
And then we pass to the next lines if it is not there and if it is there we want to file.right.

95
00:08:54,230 --> 00:08:55,330

96
00:08:55,920 --> 00:09:00,550
You do something like website and yeah, we are almost there.

97
00:09:00,750 --> 00:09:09,390
We can check what we got this far so lets you understand. So I executed this and now you see that

98
00:09:09,420 --> 00:09:13,950
www.Facebook.com was added in the host file.

99
00:09:14,290 --> 00:09:17,170
And then the next item of the list, Facebook.com

100
00:09:20,760 --> 00:09:22,860
and then my Hotmail account and so on.

101
00:09:22,890 --> 00:09:31,620
So the four items of the list which means the structure of our code is working well. We just need to work

102
00:09:31,620 --> 00:09:35,080
a bit on the style on the text formatting.

103
00:09:35,560 --> 00:09:38,030
So we are printing out the website

104
00:09:38,160 --> 00:09:48,220
but we also want the redirect IP in there. Actually the first one should be redirect variable. This one.

105
00:09:48,220 --> 00:09:51,570
And then according to the host file style

106
00:09:51,730 --> 00:09:59,620
we should have a space there as you can see it say the IP address and the host name should be separated

107
00:09:59,620 --> 00:10:04,100
by at least one space and that is what we will do.

108
00:10:05,050 --> 00:10:13,710
So redirect what you can do here is plus one space within double quotes and then we have plus website

109
00:10:13,700 --> 00:10:26,020
and to avoid the text being passed in one single line you want to pass here the backslash and special

110
00:10:26,010 --> 00:10:31,520
character that will create brake lines between these text items.

111
00:10:31,540 --> 00:10:31,980
Great.

112
00:10:31,990 --> 00:10:35,680
Now I can check this.

113
00:10:37,380 --> 00:10:41,180
Well nothing will happen because Facebook is already there.

114
00:10:41,350 --> 00:10:46,250
So it is going to stop this and really this.

115
00:10:51,660 --> 00:10:53,220
And yeah now it's working well.

116
00:10:53,300 --> 00:10:59,110

117
00:11:01,060 --> 00:11:09,070
And by the way if you have an antivirus in your system you probably have some warning there because

118
00:11:09,090 --> 00:11:13,460
the host file is a bit sensitive.

119
00:11:13,620 --> 00:11:21,020
So your antivirus will probably be notifying you that some program  is modifying your host file.

120
00:11:21,040 --> 00:11:27,650
If you want to you can just you can cancel this don't do any cleaning.

121
00:11:27,660 --> 00:11:33,300
You don't have to clean it because you know that you're the one who is accessing that host file.

122
00:11:33,790 --> 00:11:37,490
So cancel that and yeah it's working well

123
00:11:37,500 --> 00:11:43,150
I believe. We are done with the first part or the conditional.

124
00:11:43,140 --> 00:11:48,120
So now where we are in this time which is between these two hours.

125
00:11:48,120 --> 00:11:51,880
But if you were let's say 14 here.

126
00:11:54,550 --> 00:11:57,460
Stop this check again.

127
00:11:58,090 --> 00:12:04,740
In this case we get printed out these phone hours but the text in the host file will be still there.

128
00:12:04,750 --> 00:12:04,990

129
00:12:05,230 --> 00:12:11,310
So what we want to do next is remove this text from the host file when this part of the conditional

130
00:12:11,350 --> 00:12:14,010
is satisfied. I will do that

131
00:12:14,010 --> 00:12:15,910
the next lecture, so see you there.

