#!/usr/bin/env python#-*-coding:utf-8-*-ImportUrllib2,re,platform#urlib2 crawling Web pagesdefGetContent (URL, timeout=5): Content=NoneTry: Res= Urllib2.urlopen (URL, timeout=Timeout) content=Res.read ()exceptUrllib2. Urlerror, E:PrintE.reasonreturncontent#string to find before keyword contentdefGetmain (Content, keychars='#google hosts'): Pos1=Content.find (keychars) Pos2= Content.find (keychars,pos1+Len (keychars)) m= content[pos1:pos2+Len (keychars)] m= M.replace (' ',' ') R= Re.compile (r'<[\w\s\/]+>') returnR.sub ("", M)#Source Host URLURL ='http://www.360kb.com/kb/2_122.html'#Flag StringKeychars ='#google hosts'#get the current operating system and determine the path of the modified fileSyst =Platform.system ()ifsyst=='Windows': #Windowshosts ='c:\\windows\\system32\\drivers\\etc\\hosts'Else: #Linuxhosts ='/etc/hosts'content= GetContent (URL, 5)ifcontent==none:exithostcontents=Getmain (content,keychars) FP= Open (Hosts,'RB') C=Fp.read () fp.close () pos1=C.find (keychars)ifPos1==-1: Result= c+"\ n"*2+hostcontentsElse: Result= c[:p os1]+"\ n"*2+HOSTCONTENTSFP= Open (Hosts,'WB') fp.write (Result) fp.close ()
Python script, update host automatically, add or update native hosts