首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >访问github的hosts文件自动更新脚本

访问github的hosts文件自动更新脚本

作者头像
胡八万
发布2022-05-16 11:58:29
发布2022-05-16 11:58:29
1.7K0
举报
文章被收录于专栏:软件测试技术软件测试技术
因为访问github很麻烦,每次都需要更新dns ,于是写了这个脚本。

因为是更改C盘hosts文件,所以执行会自动申请管理员权限,完整脚本见下:

代码语言:javascript
复制
#updateDNS.py
import ctypes
import sys


def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False


if is_admin():

    import requests
    from time import sleep
    import os
    import datetime

    url = 'https://cdn.jsdelivr.net/gh/521xueweihan/GitHub520@main/hosts'  # 目标下载链接
    r = requests.get(url)  # 发送请求

    now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    content = f"""
# For example:
#
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 www.xmind.net

# 以上为你默认配置的hosts文件,删除github相关,其余的全部复制粘贴进去
\n
#{now}
    \n
    {r.text}
    """
    with open('C:\\Windows\\System32\\drivers\\etc\\hosts', 'w+') as f:
        f.write(content)
        f.close

    dnsFlush = "ipconfig /flushdns"
    os.system(dnsFlush)
    print("github DNS刷新成功")
    sleep(3)
else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(
        None, "runas", sys.executable, __file__, None, 1)

执行脚本,弹出下图

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-04-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档