Update the local hosts with a simple script written in Python

Source: Internet
Author: User

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

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.