Python script, update host automatically, add or update native hosts

Source: Internet
Author: User
Tags python script

#!/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 ('&nbsp;',' ') 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.