修改 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 @ 2022-05-27 00:01  Methanol  阅读(331)  评论(0)    收藏  举报