Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Block Websites Using Python in Windows

#1
Block Websites Using Python in Windows

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1038393&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</p></div>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" decoding="async" width="640" height="427" src="https://blog.finxter.com/wp-content/uploads/2023/01/do-not-enter-600755_640.jpg" alt="" class="wp-image-1038574" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/do-not-enter-600755_640.jpg 640w, https://blog.finxter.com/wp-content/uplo...00x200.jpg 300w" sizes="(max-width: 640px) 100vw, 640px" /></figure>
<p>Recently I came up with an idea to block certain websites because I didn’t want my little brother to surf certain sites while he was using my laptop. Being a smart kid, he knew ways of uninstalling chrome extensions that I used to block websites. That is when I came up with the idea of creating my own website blocker script. He wouldn’t have any clue what actually happened in the backend.<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f61c.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<h2>Project Description</h2>
<p>Thus, in this project, we are going to create a website blocker with the help of Python. Our script will bring up a window where we can enter the name of the websites that we want to block and then we can use the block button in the window to block those websites. We will also create another script that will unblock the websites that we previously blocked. The unblock script will be a simple command line script that will help you unblock the previously blocked websites. </p>
<p>Note that we will be working with the <strong>hosts</strong> file of our system to block and unblock the websites. Simply put, the hosts file is a system file which has the capability to override DNS and redirect URLs or IP addresses to different locations. In our case, we will simply redirect the websites to be blocked to our local ip address, i.e. ‘127.0.0.1’ . </p>
<p>So, without further ado let us dive into our fun project. </p>
<h2>Step 1: Import the Necessary Libraries</h2>
<p>All we need is the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/tkinter.html" target="_blank">Tkinter Module</a> to create our GUI blocker application. You can install <code>Tkinter</code> using the pip installer on your terminal: </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install tk</pre>
<h2>Step 2: Create the GUI Display Window</h2>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="507" height="336" src="https://blog.finxter.com/wp-content/uploads/2023/01/image-28.png" alt="" class="wp-image-1041354" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/image-28.png 507w, https://blog.finxter.com/wp-content/uplo...00x199.png 300w" sizes="(max-width: 507px) 100vw, 507px" /></figure>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">root = Tk()
root.geometry('500x300')
root.resizable(0, 0)
root.title("Finxter Website Blocker")
Label(root, text='WEBSITE BLOCKER', font='arial 20 bold').pack()
Label(root, text='Finxter', font='arial 20 bold').pack(side=BOTTOM)
Label(root, text='Enter Website :', font='arial 13 bold').place(x=5, y=60)
Websites = Text(root, font='arial 10', height='2', width='40', wrap=WORD, padx=5, pady=5)
Websites.place(x=140, y=60)</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>Tk()</code> allows you to create an empty GUI window where you can add labels and buttons,</li>
<li>The <code>geometry()</code> function will allow you to specify a width and height of the window.</li>
<li><code>resizable(0,0)</code> allows you to set a fixed size for the window. </li>
<li>The <code>title()</code> function allows you to set the title of the window. </li>
<li>The <code>Label()</code> widget allows you to display more lines of text which can only be seen but cannot be modified by the user.</li>
<li> Let’s explore the different parameters that I used in the Label widget.
<ul>
<li><code>root</code> – This is the name which use to refer our window.</li>
<li><code>text</code> – Used to specify the text that we want to display on the label.</li>
<li><code>font</code> – This determines the type of font our label text will appear in.</li>
<li><code>pack</code> – Used to organize the widget in a block. </li>
</ul>
</li>
<li><code>Text()</code> is used to create a widget for multi-line text areas. Let’s understand some of the parameters used within <code>Text()</code> –
<ul>
<li><strong>wrap = WORD</strong> allows you to break a line after the occurrence of the last word.</li>
<li><strong>padx</strong> allows you to specify the padding around the text. This means, it allows you to put an extra bit of space on right and left ends of the text widget. </li>
<li><strong>pady</strong> allows you to specify the padding on top and bottom side of the text widget.</li>
</ul>
</li>
</ul>
<h2>Step 3: Define the Host Address and IP Address </h2>
<p>Since we want to block the websites on our system, hence you need to add them to the system host file. To do so, you will need to specify the host address and the IP address.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">host_path = 'C:\Windows\System32\drivers\etc\hosts'
ip_address = '127.0.0.1'</pre>
<h2>Step 4: Creating the Blocker Function</h2>
<p>Now, we need to create the function that will allow us to block a certain website. Therefore, as soon as you feed in the website name and click on the block button on the application, the website will be blocked immediately. If the website is already present within the host file then the application window will display a message that the website is already blocked. </p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def Blocker(): website_lists = Websites.get(1.0, END) Website = list(website_lists.split(",")) with open(host_path, 'r+') as host_file: file_content = host_file.read() for website in Website: if website in file_content: Label(root, text='Already Blocked', font='arial 12 bold').place(x=200, y=200) pass else: host_file.write(ip_address + " " + website + '\n') Label(root, text=" Blocked ", font='arial 12 bold').place(x=230, y=200)</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>website_lists</code> will store all the websites entered to be blocked by the user. Note that the <code>get</code> function used will allow you to fetch the websites entered within the text widget of the application.</li>
<li>Open up the system host file in the read and write mode (r+).</li>
<li>We then check the contents of the host file and if a website is already present within the file then we ask the widget to display a message -” Already Blocked!”. Otherwise, we simply go ahead and the website to the host file and then display the message – “Blocked”.</li>
</ul>
<p>That’s it. All that remains to be done is to create a block button for our application.</p>
<h2>Step 5: Creating the Block Button</h2>
<p>We need a button that triggers the <code>Blocker</code> function as soon as it s pressed. Thus, our next step is to create the block button and attach the <code>Blocker</code> function to this button.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">block = Button(root, text='Block', font='arial 12 bold', pady=5, command=Blocker, width=6, bg='royal blue1', activebackground='sky blue')
block.place(x=230, y=150)
root.mainloop()</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>Button()</code> function allows you to create a button on the application.
<ul>
<li>The <code>command</code> parameter is used to call the Blocker function as soon as the button is clicked by the user. </li>
<li>The <code>activebackground</code> parameter is used to set a background color for the button when it is clicked.</li>
<li>Note that we have already discussed all the other parameters used within the Button function previously in step 2. </li>
</ul>
</li>
<li>The <code>place</code> function allows you to align or place the button at a particular position in the application.</li>
<li><code>root.mainloop()</code> is the Tkinter method that tells Python to run the Tkinter event loop. The <code>mainloop</code> method listens to events like button clicks or keypresses, and halts any code that comes after it from executing until you close the window where you called the method.</li>
</ul>
<h2><strong>Putting It All Together</strong></h2>
<p>Woohoo!!! We have successfully created our “Website Blocker” application. Finally, when you put everything together, this is how the complete script looks like –</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from tkinter import * host_path = 'C:\Windows\System32\drivers\etc\hosts'
ip_address = '127.0.0.1'
root = Tk()
root.geometry('500x300')
root.resizable(0, 0)
root.title("Finxter Website Blocker")
Label(root, text='WEBSITE BLOCKER', font='arial 20 bold').pack()
Label(root, text='Finxter', font='arial 20 bold').pack(side=BOTTOM)
Label(root, text='Enter Website :', font='arial 13 bold').place(x=5, y=60)
Websites = Text(root, font='arial 10', height='2', width='40', wrap=WORD, padx=5, pady=5)
Websites.place(x=140, y=60) def Blocker(): website_lists = Websites.get(1.0, END) Website = list(website_lists.split(",")) with open(host_path, 'r+') as host_file: file_content = host_file.read() for website in Website: if website in file_content: Label(root, text='Already Blocked', font='arial 12 bold').place(x=200, y=200) pass else: host_file.write(ip_address + " " + website + '\n') Label(root, text=" Blocked ", font='arial 12 bold').place(x=230, y=200) block = Button(root, text='Block', font='arial 12 bold', pady=5, command=Blocker, width=6, bg='royal blue1', activebackground='sky blue') block.place(x=230, y=150)
root.mainloop()</pre>
<p><strong>Output</strong></p>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2023/01/website-blocker.gif" alt="" class="wp-image-1042117" /></figure>
<h2>The Unblock Script</h2>
<p>Once you have successfully blocked the required websites, what if you want to unblock them later on? That’s exactly what the next script will do. </p>
<p>The idea is to replicate the above process, the only difference in this case is we will remove the webiste name from the hosts file now. </p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">host_path = 'C:\Windows\System32\drivers\etc\hosts'
name = input("Enter the Website you want to Unblock: ")
unblock_web = '127.0.0.1' + " " + name + "\n"
def unblock(): flag = 0 with open(host_path, 'r+') as host_file: lines = host_file.readlines() with open(host_path, 'w') as fw: for line in lines: if unblock_web != line: fw.write(line) else: flag = 1 if flag == 1: print("Unblocked!") else: print("Already Unblocked!") unblock()</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2023/01/website_unblocker.gif" alt="" class="wp-image-1042164" /></figure>
<h2>Conclusion</h2>
<p>There we go! We have our complete application. This can be extremely handy if you wish to block or unblock websites from your system. I hope this project added some value and helped you in your coding quest. Stay tuned and <strong><a href="https://blog.finxter.com/subscribe/" target="_blank" rel="noreferrer noopener">subscribe</a></strong> for more interesting projects and tutorials. </p>
<p class="has-base-background-color has-background"><strong>Useful Read: <a href="https://blog.finxter.com/top-10-tkinter-cheat-sheets/" target="_blank" rel="noreferrer noopener">Top 10 Tkinter Cheat Sheets</a></strong></p>
</div>


https://www.sickgaming.net/blog/2023/01/...n-windows/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016