#!/usr/bin/env python
# coding: utf-8
# Author : toddlerya
# Date: Jan 18 2015
# Last edit: Feb 20 2015
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 2015 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