The Google Wall has been serious these two days, so the idea of making a one-click update of the hosts script.
Because I'm learning python, it's natural to write this script in Python.
Contact More is urllib2 this library, habitual import into. Also import a library of re to allow Python to support regular expressions. About regular Expressions I do not study much, only a bit simple, if you want to know the next regular expression can be on this site http://deerchao.net/tutorials/regex/regex.htm.
Python is relatively concise, and here is a syntax for writing to a file. Paste the code below.
# coding:utf-8import Reimport urllib2r=urllib2.urlopen (' http://googless.sinaapp.com '). Read () Link_list = Re.findall ( ' 1\d+.\d+.\d+.\d+ ', r) NewList = []def unique (oldlist): For x in Oldlist:if x not in Newlist:newlist.append (x) return NewList Unique (link_list) f = open ('/private/etc/hosts ', ' W ') for Each_link in Newlist:f.write (each_link+ ' + ') www.google.com ' + ' \ n ') F.close ()
Later I saw a friend on his blog has been updated to use the hosts, simply do not have regular expression, a brain to his page to climb down, hey
Import urllib2content = Urllib2.urlopen (' http://www.findspace.name/adds/hosts '). Read () F = open ('/private/etc/hosts ' , ' W ') f.write (content) F.close ()
is not super simple, haha ~
Update the local hosts with a simple script written in Python