#!/usr/bin/env python # coding: utf-8 # Author : toddlerya & stararly # Last edit: Feb 21 2017 import urllib2 import re import sys import os import time url = "http://www.360kb.com/kb/2_122.html" req = urllib2.Request(url) html = urllib2.urlopen(req).read() head_span = html.find('#google hosts') tail_span = html.find('#google hosts 2016 end') raw_hosts = html[head_span:tail_span+22] result, number = re.subn(r'<.*>', '', raw_hosts) pure_hosts, number = re.subn(r' ', ' ', result) date = time.strftime('%Y-%m-%d',time.localtime(time.time())) arch = """127.0.0.1 localhost # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters %s """ % date print "Update your hosts file start!" f = file(r'C:\\Windows\\System32\\drivers\etc\\hosts' ,'w+') new_host = [arch,pure_hosts] f.writelines(new_host) f.close() print "Update success! The hosts date %s" % pure_hosts while True: flag = raw_input("Enter q to exit: ") if flag == 'q': break
本文原始代码转自toddler的博客“https://my.oschina.net/u/2003106/blog/369088”
原来代码已经失效,我简单进行了修改。在此表示感谢!
没有评论,留下你的印记,证明你来过。
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。