github加速访问——Python代码自动化实现

Python代码实现

使用代码前可能要修改hosts的权限。如果是windows直接运行代码就可以。

import re
import requests
import os

#1.若hosts中本身配置过就删除掉原来配置的。 地址是windows默认存放hosts的位置
with open("C:\Windows\System32\drivers\etc\hosts","r",encoding="utf-8") as f:
    lines = f.readlines()
    #print(lines)
with open("C:\Windows\System32\drivers\etc\hosts","w",encoding="utf-8") as f_w:
    for line in lines:
        #删除包括github.com的那一行内容
        if "github.com" in line:
            continue
        #和上面的同理
        if "github.global.ssl.fastly.net" in line:
            continue
        f_w.write(line)

#2.获取域名对应的ip地址
#二级域名
SecondLevelDomain = "github.com"
url1 = "https://{SecondLevelDomain}.ipaddress.com/".format(SecondLevelDomain=SecondLevelDomain)
#次级域名
SubDomain = "assets-cdn.github.com"
url2 = "https://{SecondLevelDomain}.ipaddress.com/{SubDomain}".format(SecondLevelDomain=SecondLevelDomain, SubDomain=SubDomain)
#大概是给github搞加速的厂商的域名fastly.net
url3 = "https://fastly.net.ipaddress.com/github.global.ssl.fastly.net"

#请求链接
r1 = requests.get(url1)
r1.status_code
r1.encoding = 'utf-8'
#获取响应体
r1=r1.text
# print(r1)
pattern = re.compile('https://www.ipaddress.com/ipv4/[0-9]*.[0-9]*.[0-9]*.[0-9]*')
#https://www.ipaddress.com/ipv4/140.82.113.4
res = pattern.findall(r1)
res = res[0].split("/ipv4/");#切片
res1 = res[1];#获取到ip地址
res1 = res1+" github.com"
print(res1)

r2 = requests.get(url2)
r2.encoding = 'utf-8'
#获取响应体
r2=r2.text
# print(r2)
pattern = re.compile('https://www.ipaddress.com/ipv4/[0-9]*.[0-9]*.[0-9]*.[0-9]*')
#https://www.ipaddress.com/ipv4/140.82.113.4
res = pattern.findall(r2)
res = res[0].split("/ipv4/");#切片
res2 = res[1];#获取到ip地址
res2 = res2+" assets-cdn.github.com"
print(res2)

r3 = requests.get(url3)
r3.encoding = 'utf-8'
#获取响应体
r3=r3.text
# print(r3)
pattern = re.compile('https://www.ipaddress.com/ipv4/[0-9]*.[0-9]*.[0-9]*.[0-9]*')
#https://www.ipaddress.com/ipv4/140.82.113.4
res = pattern.findall(r3)
res = res[0].split("/ipv4/");#切片
res3 = res[1];#获取到ip地址
res3 = res3+" github.global.ssl.fastly.net"
print(res3)

#3.写入hosts文件
f=open("C:\Windows\System32\drivers\etc\hosts","a")
f.write(res1+"\n");
f.write(res2+"\n");
f.write(res3+"\n");
f.close();

#4.刷新DNS缓存
os.system("ipconfig /flushdns")

下面是原本需要的操作。上面的操作是基于下面的理论的。

1. 获取GitHub官方CDN地址

链接:https://www.ipaddress.com/

打开后界面:

image-20210411155236706

2. 进入后查询这三个链接的DNS解析地址,并记录下,后面有用

①:github.com

②:assets-cdn.github.com

③:github.global.ssl.fastly.net

分别输入上面是三个地址,以github.com为例

image-20210411155538104

3. 修改系统Hosts文件

打开系统hosts文件(需管理员权限)。
路径:C:\Windows\System32\drivers\etc

image-20210411155445551

在末尾添加三行记录并保存。(需管理员权限,注意IP地址与域名间需留有空格)

image-20210411155345178

4. 刷新系统DNS缓存

Windows+X 打开系统命令行(管理员身份)或powershell

运行

 ipconfig /flushdns 

手动刷新系统DNS缓存。

到这里所有工作完毕,可以直接访问了,发现访问速度很快!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值