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/
打开后界面:
2. 进入后查询这三个链接的DNS解析地址,并记录下,后面有用
①:github.com
②:assets-cdn.github.com
③:github.global.ssl.fastly.net
分别输入上面是三个地址,以github.com为例
3. 修改系统Hosts文件
打开系统hosts文件(需管理员权限)。
路径:C:\Windows\System32\drivers\etc
在末尾添加三行记录并保存。(需管理员权限,注意IP地址与域名间需留有空格)
4. 刷新系统DNS缓存
Windows+X 打开系统命令行(管理员身份)或powershell
运行
ipconfig /flushdns
手动刷新系统DNS缓存。
到这里所有工作完毕,可以直接访问了,发现访问速度很快!