修改 Hosts 访问 Github

利用 Python 脚本自动修改 Hosts 文件访问 Github

依赖:Python + requests

感谢 ineo6 大佬的 Hosts 项目,不如给 Hosts项目 点个Star

脚本

import requests

# 读取文件
def fetch():
    with open(hosts,'r',encoding='UTF-8') as f:
        return f.read()

# 删除上一次写入
def delete(string):
    return string[::-1].split('\n',42)[-1][::-1]

# 获取hosts
def get_hosts():
    return requests.get(url).text

# 分割字符串
def split(string):
    return string.split('\n',4)[-1]

# 合并字符串
def merge_str(source,github):
    return source + '\n' + github

# 写入文件
def write(string):
    with open(hosts,'w',encoding='UTF-8') as f:
        f.write(string)

if __name__ == '__main__':
    hosts = 'C:\Windows\System32\drivers\etc\hosts'
    url = 'https://gitlab.com/ineo6/hosts/-/raw/master/next-hosts'
    write(merge_str(delete(fetch()),split(get_hosts())))
posted @   Methanol  阅读(317)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示