So how do we go about blocking websites with Python?
Windows, MAC or Linux
they have a host file which is something like this.
So this is the host file.
And if you open the host file
you'll see some lines there.
And what this host file is for is that if you want to block certain web sites such as Facebook
or this mail live here
what you do is you tag them here as I have done here.
I've typed them via Python
in this case.
But you can also type them manually
in that case you don't have control of the times when you want them to be here.
So if you have a look at this, this is saying that
the IP address should be placed in the first column followed by the corresponding hostname which means
this is a redirect IP address.
And this is a host name which underlies that if you want to block Facebook you want to redirect the browser
to this IP address which is the local IP address of your computer which means when you try to access
Facebook
you'll be redirected to that IP that shows this error web page.
Good.
Now this hosts file
on Windows it is located in this directory here.
So Windows system 32 drivers.
And then you have the host file which doesn't have an extension but you can open it as a text file
in Atom or other text editors. On Linux or Mac that host file path would be something like
this and host.
So, very straight forward.
This is both for Linux and Mac and on Windows
that was this one here.
So I'll copy that and just put it here.
So that's for MAC and Linux and Windows.
Good.
Now that we know how this host file works
we can open that as a file via Python
using file handling methods and we can add lines of text at certain times of the day and
this host file. For instance let's say I work between 8 a.m. and 4 p.m..
In that case I'll write a Python script that runs the entire day and checks every let's say every
five minutes it checks the time.
And if it's past 4:00 p.m. then it will delete these two lines.
But if you are between 08:00 and 16:00 Python on will add these two lines in this host file
and the host file
will block access to these two websites. So that is starting to make sense I believe and all we need to start
off is you have to think about let's say two websites that you want a block or more or just one.
And also figure out where your host is located which I already showed to you.
And yeah we should be good to go to start writing the program and we'll do that in the next lecture.