【如何使用Python的网站拦截器(用法图解)】这是一个现实世界的程序, 在你的工作时间内阻止某些分散注意力的网站, 例如Facebook, Youtube等。
关于程序:在此程序中, 我们要通过的是你认为会分散注意力的网站链接, 而你在计算机和程序上工作的时间将阻止这些网站。
程序架构:
每个系统都有主机文件,不管是Mac、Windows还是Linux
Mac和Linux中的主机文件:
/etc/hosts
Windows中的主机文件:
C:\Windows\System32\drivers\etc
主机文件的工作:
主机是一个操作系统文件, 它将主机名映射到IP地址。在此程序中, 我们将网站的主机名映射到我们的本地主机地址。使用python文件处理操作, 我们将在hosts.txt中写入主机名, 并在下班后删除行。
Mac中的主机文件:

文章图片
# Run this script as rootimport time
from datetime import datetime as dt# change hosts path according to your OS
hosts_path = "/etc/hosts"
# localhost's IP
redirect = "127.0.0.1"# websites That you want to block
website_list =
[ "www.facebook.com" , "facebook.com" , "dub119.mail.live.com" , "www.dub119.mail.live.com" , "www.gmail.com" , "gmail.com" ]while True :# time of your work
if dt(dt.now().year, dt.now().month, dt.now().day, 8 )
<
dt.now() <
dt(dt.now().year, dt.now().month, dt.now().day, 16 ):
print ( "Working hours..." )
with open (hosts_path, 'r+' ) as file :
content = file .read()
for website in website_list:
if website in content:
pass
else :
# mapping hostnames to your localhost IP address
file .write(redirect + " " + website + "\n" )
else :
with open (hosts_path, 'r+' ) as file :
content = file .readlines()
file .seek( 0 )
for line in content:
if not any (website in line for website in website_list):
file .write(line)# removing hostnmes from host file
file .truncate()print ( "Fun hours..." )
time.sleep( 5 )
Windows用户特别注意事项:Windows用户需要创建OS主机文件的副本。现在在中提供重复文件的路径hosts_path在脚本中提到。
在Mac中安排上述脚本:要在Mac中安排上述脚本, 你必须在终端中以root身份打开crontab。
在终端中编写以下命令:
sudo crontab -e
你的终端应如下所示:

文章图片

文章图片
现在按” i” 进入插入/编辑模式并编写@reboot python_script_path。
保存该选项卡, 方法是:先按ESC键退出写模式, 然后退回到命令模式, 然后写” :wq” , 最后按Enter进行确认。
重新启动系统并查看魔术。
在Windows中安排:安排以上脚本的时间有点技巧, 但是我将逐步指导你
首先, 将脚本的扩展名从” .py” 更改为” .pyw” 。
现在打开任务计划程序。任务计划程序应如下所示:

文章图片
你可能会看到已经计划了网站拦截器, 因为我已经在计算机中安排了测试目的。仔细遵循进一步的计划安排说明, 以便在你的计算机中计划网站拦截程序。
点击” 创建任务” 。填写你选择的名称, 并标记” 以最高特权运行” 。

文章图片

文章图片
现在转到触发器, 选择” 启动时” 开始任务。

文章图片
转到” 动作” 栏, 然后创建一个新动作并给出脚本的路径。

文章图片

文章图片
转到条件栏并取消标记电源部分。

文章图片

文章图片
按确定, 你可以看到计划的脚本。

文章图片
最后, 重新启动计算机, 然后看到魔术。

文章图片
注意:
你也可以通过单击运行按钮立即进行检查。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- 为什么C将数组参数视为指针()
- Web爬网/爬虫–合法还是非法的()
- Python Web将冠状病毒数据收集到MS Excel中
- 网站信息检索|向量空间模型详细介绍
- Alibaba中间件技术系列「Nacos技术专题」配置中心加载原理和配置实时更新原理分析(上)
- Alibaba中间件技术系列「Nacos技术专题」配置中心加载原理和配置实时更新原理分析(中)
- 字节跳动 iOS Heimdallr 卡死卡顿监控方案与优化之路
- 强大的开源规则引擎
- 碰一碰,鸿蒙原子化服务实战