Github修改hosts文件实现国内访问加速

  1. hosts所在的文件夹:
    1.  Windows 系统位于 C:\Windows\System32\drivers\etc\ Mac
    2. 苹果系统hosts位于 /etc/ Linux系统hosts位于 /etc/ 绝大多数Unix系统都是在 /etc/
    3. 最好把hosts文件备份一下,把hosts文件拷贝一份后改个名即可。
  2. 打开DNS查询:站长之家DNS查询页面      切换到DNS选项
  3. 查询http://github.com和http://github.global.ssl.fastly.net选择TTL最小的DNS,复制ip地址,可以多复制几个。
  4. 把复制好的IP地址添加到hosts文件中,类似于这个样子140.82.113.3 http://github.com 140.82.113.3 http://github.global.ssl.fastly.net
  5. 让hosts生效

    Windows:开始 -> 运行 -> 输入cmd -> 在CMD窗口输入 : ipconfig /flushdns
    Linux:终端输入 : sudo rcnscd restart
    Mac OS X终端输入 : sudo killall -HUP mDNSResponder   

    其他:断网,再开网
    终极方法: 重启机器

  第二种方法:https://gist.github.com/2033329616/02665f19270ee562108defe8d4be361c

  在上述第2步运行下面的程序获取github的有效IP更新host文件和网络配置

 

import re
import time
import  requests
from selenium import webdriver
from bs4 import BeautifulSoup

class GetIP(object):
    def __init__(self, driver_path='E:\\test\\chromedriver.exe'):
        """
        driver_path:the path of the chrome driver, which is downloaded from http://chromedriver.storage.googleapis.com/index.html
        note the version should be corresponding between chrome driver and chrome webbrowser 
        """
        self.path = driver_path 
        self.urls = [
            "https://github.com.ipaddress.com/www.github.com",
            "https://github.com.ipaddress.com/",                         #github.com
            "https://github.com.ipaddress.com/gist.github.com",
            "https://githubusercontent.com.ipaddress.com/gist.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/raw.githubusercontent.com",
            "https://github.com.ipaddress.com/assets-cdn.github.com",
            "https://githubusercontent.com.ipaddress.com/cloud.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/camo.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars0.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars1.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars2.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars3.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars4.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars5.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars6.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars7.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars8.githubusercontent.com",
        ]
    
    def getip_req(self, url):
        """
        use request to get the ip of github (better and faster)
        url: the url which ip you want to search
        """
        response = requests.get(url)  # 部分网站可用
        status = response.status_code 
    #     print(status)
        # print (response.headers['content-type'])
        soup = BeautifulSoup(response.content,"html.parser")     # ,from_encoding="utf-8")
        link_node = soup.find('a',href=re.compile(r"ipv4"))
        ip = link_node.get_text() if status == 200 else None     # success 200, fail 404
        return ip
    
    def getip_sel(self, url):
        """
        use selenium to get the ip of github
        url: the url which ip you want to search 
        """
        self.driver = webdriver.Chrome(executable_path=self.path)
        self.driver.get('https://github.com.ipaddress.com')
        self.driver.implicitly_wait(7)
        inputElement = self.driver.find_elements_by_name('host')
        searchButton = self.driver.find_elements_by_class_name('search')
        inputElement[0].send_keys(url)                  # search url in search box
        searchButton[0].click()                         # click the corresponding button
        browser_res = self.driver.page_source
#         print(browser_res)
        soup = BeautifulSoup(browser_res,"html.parser") # ,from_encoding="utf-8")
        link_node = soup.find('a',href=re.compile(r"ipv4"))
        # print (link_node.name,link_node['href'],link_node.get_text())
        return link_node.get_text() if link_node != None else None   # get the ip of the url
    
    def github_hosts(self, get_type=0):
        """
        get_type: the way we use to get ip, get_type=0:request(default) | get_type=1:selenium
        """
        success_hosts = {}
        failure_hosts = {}
        
        for url in self.urls:
            host_url = url.strip().split('/')[-1]
            host_url = host_url if host_url != '' else 'github.com'
            try:
    #             print(host)
                ip = self.getip_req(url) if get_type==0 else self.getip_sel(host_url)
        
                print(ip, host_url)
                if ip == None:
                    failure_hosts[host_url] = ip   # sometimes different host has the same ip
                else:
                    success_hosts[host_url] = ip
                
                if get_type == 1:     
                    time.sleep(0.7)
                    self.driver.quit()
            except Exception as err:
                failure_hosts[host_url] = None
                print(err)
        print('------------------------------------------------')
        print('faiure_hosts:\n', failure_hosts)
        print('------------------------------------------------')
        print('success_hosts:')
        for k, v in success_hosts.items():
            print(v, k)
        print('------------------------------------------------')
        
if __name__ == '__main__':  
    ip_obj = GetIP()
    ip_obj.github_hosts(get_type=0)  

 

posted @ 2021-05-10 22:54  麦穗鱼~  阅读(5422)  评论(0编辑  收藏  举报