This article mainly introduces the Python script to automatically update hosts to achieve no-wall access to google. This article provides the implementation code directly. if you need it, you can refer to it and I believe many people still need to use google for search, however, the walls in China are getting worse and worse, and even vpn is being blocked. next we will give you a script to access google without turning over the wall.
Script content:
Vi google_update.py
#! /Usr/bin/env python # coding: UTF-8 # Author: toddlerya # Date: Jan 18 2015 import urllib2import reimport sysimport osurl = "http://www.360kb.com/kb/2_122.html" req = urllib2.Request (url) html = urllib2.urlopen (req ). read () head_ver = html. find (r'Google hosts') Ver_before = len ("Google hosts") Tail_ver = html. find (r'') Head_span = html. find ('# base services') tail_span = html. find ('# google source end') raw_hosts = html [head_span: tail_span] result, number = re. subn (r' <. *> ', '', raw_hosts) pure_hosts, number = re. subn (r '','', result) arch = "127.0.0.1 localhost # The following lines are desirable for IPv6 capable hosts: 1 ip6-localhost ip6-loopbackfe00: 0 ip6-localnetff00 :: 0 ip6-mcastprefixff02: 1 ip6-allnodesff0 2: 2 ip6-allrouters "" print "Update your host file start! "# Print" Please input your su password "f = file (r '/tmp/hosts', 'W +') new_host = [arch, pure_hosts] f. writelines (new_host) f. close () OS. system ('MV/tmp/hosts/etc/hosts') print "Update success! "
The result is as follows: