jiangtiandao / smarthosts

Automatically exported from code.google.com/p/smarthosts
0 stars 0 forks source link

编码问题 #6

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
hosts.txt能不能改为utf-8编码. 注释都是乱码.

Original issue reported on code.google.com by Rojazz1...@gmail.com on 26 Oct 2011 at 2:57

GoogleCodeExporter commented 9 years ago
下一版本(大概在周日下午发布)会同时支持UTF-8和ANSI编码��
�
感谢反馈。

Original comment by azly...@gmail.com on 26 Oct 2011 at 4:34

GoogleCodeExporter commented 9 years ago

Original comment by azly...@gmail.com on 27 Oct 2011 at 4:56

GoogleCodeExporter commented 9 years ago
能不能搞个随机ip,每个人都一样,恐怕不适合身处不同网络
运营商的苦逼

Original comment by huhang...@gmail.com on 12 Nov 2011 at 1:39

GoogleCodeExporter commented 9 years ago
iconv -f GBK -t utf-8 /etc/hosts -o /etc/hosts.out
mv /etc/hosts.out /etc/hosts

Original comment by Ludo.A...@gmail.com on 2 Jan 2012 at 11:20

GoogleCodeExporter commented 9 years ago
linux勉强能用的版本。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "rikugun"
__modifier__ = "arnk"

import sys
import urllib
import os
from shutil import copyfile

HOSTS_URL = 'https://smarthosts.googlecode.com/svn/trunk/hosts'
LOCAL_HOSTS = '/etc/hosts'
LOCAL_HOSTS_BAK = '/etc/hosts.bak'
ICONV_CMD = "iconv -f GBK -t UTF-8 /etc/hosts -o /tmp/hosts"
MV_CMD = "mv /tmp/hosts /etc/hosts"

def main():
    """主函数.

    """
    copyfile(LOCAL_HOSTS, LOCAL_HOSTS_BAK) #备份文件
    with open(LOCAL_HOSTS, 'w') as hosts:
        hosts.write(os.linesep) # 使用平台独立的换行符
        for line in urllib.urlopen(HOSTS_URL):
            hosts.write(line.strip() + os.linesep)

    os.system(ICONV_CMD)
    os.system(MV_CMD)
    print "success!"

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

Original comment by Ludo.A...@gmail.com on 2 Jan 2012 at 11:29