更新hosts的python脚本

时间:2014-05-03 00:29:18   收藏:0   阅读:445
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#author:mrsimple

import sys
import urllib
import os
from shutil import copyfile


HOSTS_URL=‘https://smarthosts.googlecode.com/svn/trunk/hosts‘

LOCAL_HOSTS=‘/etc/hosts‘

def update():
    """update hosts from smarthost"""
    # backup hosts file
    copyfile(LOCAL_HOSTS,‘hosts.bak‘)
    with open(LOCAL_HOSTS,‘aw‘) as hosts:
       # 字符串给出当前平台使用的行终止符。例如,Windows使用‘\r\n‘,Linux使用‘\n‘而Mac使用‘\r‘。
        hosts.write(os.linesep)
        for line in urllib.urlopen(HOSTS_URL):
            hosts.write(line.strip()+os.linesep)

    print "success!"

if __name__ == ‘__main__‘:
    if len(sys.argv)>1:
        HOSTS_URL = sys.argv[1]
    update()

更新hosts的python脚本,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!