Python自动化运维python 入门python开发

工具向——用python自动化修改hosts翻墙

2017-08-31  本文已影响2719人  treelake

简述

概要

import os
path = r'C:/Windows/System32/drivers/etc/'
os.chdir(path)
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
}

import requests
from functools import partial
p_get = partial(requests.get, headers=headers)
if p_get('https://google.com.hk', timeout=2).status_code == 200:
        print('You ALREADY can access Google!')
url = 'http://.......'
p_get(url).text
f_content = ''
with open(file, 'r', encoding='utf-8') as f1:
    f_content = f1.read()
with open(file, 'w', encoding='utf-8') as f1:
    f1.write(new_content)

代码

# pyinstaller Gethosts.py --onefile
import os
# import shutil
import time
import datetime
# import win32file

path = r'C:/Windows/System32/drivers/etc/'
os.chdir(path)
file = 'hosts'

backup_urls = [
    'https://raw.githubusercontent.com/wangchunming/2017hosts/master/hosts-ipv6-pc',
    'https://raw.githubusercontent.com/wangchunming/2017hosts/master/hosts-pc',
    'https://raw.githubusercontent.com/racaljk/hosts/master/hosts',
    'https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts-files/hosts',
]
# url2 = 'https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
}

import requests
from functools import partial
p_get = partial(requests.get, headers=headers)

EXTRA_HOSTS = u'127.0.0.1 get.daum.net\n'
REWRITE_FLAG = True

def update_hosts(url):
    global REWRITE_FLAG
    
    # os.access(path, os.W_OK)
    # shutil.copy (file, file + str(datetime.datetime.now().date()))
    # win32file.CopyFile(file, file + '-' + str(datetime.datetime.now().date()), 1)
    f_content = ''
    with open(file, 'r', encoding='utf-8') as f1:
        # global f_content
        f_content = f1.read()

    try:
        new_content = EXTRA_HOSTS + p_get(url).text
        # print(len(new_content))
        
        if new_content != f_content:
        
            if REWRITE_FLAG:  # 是否保留历史
                with open(file + '-' + str(datetime.datetime.now().date()),
                    'w', encoding='utf-8') as f2:
                    f2.write(f_content)
                REWRITE_FLAG = False
                
            with open(file, 'w', encoding='utf-8') as f1:
                f1.write(new_content)
            # import urllib.request
            # urllib.request.urlretrieve(url, file)
            print('hosts 已更改')

        os.popen('ipconfig /flushdns')
        # print(os.popen('ipconfig /flushdns').read())
        
    except:
        print('源连接错误', url)


def read_sources(file='./host_sources.txt'):
    '''
    添加自定义源:
    1. 在该应用程序旁新建文件'host_sources.txt'
    2. 在其中添加如下格式的源(链接, 优先级):
        http://a.com/b, 3
        http://c.com/d, 2
        http://x.com
    3. 策略为: 根据优先级排序, 数字大则权重大, 权重大的优先尝试
    '''
    try:
        with open(file, 'r', encoding='utf-8') as f:
            urls = [[i.strip() for i in line.split(',')] 
                if ',' in line else [line.strip(), 0] # 应对单行无权重情况, 可直接注释
                for line in f.readlines() if line != '\n']
            urls.sort(key=lambda x:int(x[1]), reverse=True)
            return [url for url, _ in urls]
    except:
        return []


def try_every_source_to_update_hosts():
    flag = 0
    urls = read_sources() + backup_urls
    for n, url in enumerate(urls):
        print('正在尝试第%s个源' % (n+1))
        print(url)
        update_hosts(url)
        print('正在尝试连接谷歌, 请稍候...')
        time.sleep(1)
        try:
            r = p_get('https://google.com.hk')
            if r.status_code == 200:
                flag = 1
                break
        except Exception:
            print('连接失败, 尝试下一个可用源')

    return flag

    
print(read_sources.__doc__)
try:
    if p_get('https://google.com.hk', timeout=2).status_code == 200:
        print('You ALREADY can access Google!')
except:
    if try_every_source_to_update_hosts():
        print('You can access Google!')
    else:
        print('Ah, sth went wrong...')
        print('请尝试手动访问:\n'
              'https://laod.cn/hosts/2017-google-hosts.html\n'
              '寻找解决方法')


if input('Enter键退出'):
    test_sites = [
        'https://www.google.com.hk',
        'https://www.youtube.com/',
        'https://www.facebook.com/',
        'https://www.tumblr.com/',
        'https://www.twitter.com/',
    ]

    for url in test_sites:
        try:
            r = p_get(url)
            if r.status_code == 200:
                print('success', url, ' ( 耗时: %s)' % r.elapsed)
        except:
            print('error', url)
    input('Enter键退出')
上一篇下一篇

猜你喜欢

热点阅读