推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
yuewolf
V2EX  ›  Python

天才 Pythoner 又写出了一个超级实用的脚本

  •  
  •   yuewolf · Apr 22, 2015 · 7718 views
    This topic created in 4177 days ago, the information mentioned may be changed or developed.
    呃哈哈~~ 不好意思,我标题党了,其实是:
    Python 新手磨了半天,弄了个自动更新 HOSTS 文件的小脚本。

    代码地址 https://coding.net/u/yuewolf/p/blog-wolf/git/raw/master/UpdateHosts.py

    非常简易,就是自动在本地 HOSTS 里把 http://www.findspace.name/adds/hosts2 中的内容加上去。

    如果曾经加过,就只替换这个网址中的部分.

    感觉写得有点别扭,真诚求高手老手们指点。
    20 replies  •  2015-04-26 12:45:41 +08:00
    blacktulip
        1
    blacktulip  
       Apr 22, 2015 via iPhone   ❤️ 1
    Pythonista , 不是 Pythoner
    clockwerk
        2
    clockwerk  
       Apr 22, 2015   ❤️ 4
    头像要是你自己,我一脚把你踹下去
    zeroten
        3
    zeroten  
       Apr 22, 2015
    我写过类似的东西。。。https://www.v2ex.com/t/165103#reply30
    est
        4
    est  
       Apr 22, 2015   ❤️ 2
    一行shell命令就够了:

    sudo wget -O /etc/hosts http://www.findspace.name/adds/hosts2
    gongpeione
        5
    gongpeione  
       Apr 22, 2015
    @zeroten 噗正准备@你
    zeroten
        6
    zeroten  
       Apr 22, 2015
    @gongpeione 来来来,qcon走起
    yuewolf
        7
    yuewolf  
    OP
       Apr 22, 2015
    @zeroten 呀,你这个确实很赞!!
    Fikhtengol
        8
    Fikhtengol  
       Apr 22, 2015
    bullshit
    lilydjwg
        9
    lilydjwg  
       Apr 22, 2015
    @est 这样会覆盖用户已有的配置,比如开发用的域名关联、自己的虚拟机/内网机器的关联等。
    Feiox
        10
    Feiox  
       Apr 22, 2015
    @blacktulip or Pythoneer
    yuewolf
        11
    yuewolf  
    OP
       Apr 22, 2015
    @lilydjwg 不会覆盖的。第一次追加在最后,以后的话只要用户没有在那个 #+BEGIN 后面配置,也不会覆盖。

    不过确实挺局限的。哈哈,自己用还是很不错的。
    lilydjwg
        12
    lilydjwg  
       Apr 22, 2015
    @yuewolf 我说的是 est 的那行 wget 啦。
    WKPlus
        13
    WKPlus  
       Apr 22, 2015
    文件打开和关闭的次数不对应,看着好别扭
    est
        14
    est  
       Apr 22, 2015
    @lilydjwg
    @yuewolf

    嗯,如果要实现merge的话,是需要更多的操作了。

    而且linux的hosts支持一个ip对应多个域名。这个规则更加复杂了。
    dalang
        15
    dalang  
       Apr 22, 2015 via iPhone
    这种代码还真的只能自己用用…
    ryd994
        16
    ryd994  
       Apr 23, 2015 via Android
    好久前就用C#写过
    hosts代理舰娘,用户都是小白,于是就做了一键。还自动UAC提权
    churchmice
        17
    churchmice  
       Apr 23, 2015 via Android
    @est 我就问问你们为啥这么喜欢sudo
    est
        18
    est  
       Apr 23, 2015
    @churchmice 因为要改 /etc/* 只能sudo?
    churchmice
        19
    churchmice  
       Apr 23, 2015
    @est 哦,看走眼了,没看到/etc/,抱歉
    sukai
        20
    sukai  
       Apr 26, 2015
    ^_^,hosts收下了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5433 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 02:25 · PVG 10:25 · LAX 19:25 · JFK 22:25
    ♥ Do have faith in what you're doing.