推荐学习书目
› 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 · 7886 views
    This topic created in 4218 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
    blacktulip
    Apr 22, 2015 via iPhone❤️1
    1
    Pythonista , 不是 Pythoner
    clockwerk
    clockwerk
    Apr 22, 2015❤️4
    2
    头像要是你自己,我一脚把你踹下去
    zeroten
    zeroten
    Apr 22, 2015
    3
    我写过类似的东西。。。https://www.v2ex.com/t/165103#reply30
    est
    est
    Apr 22, 2015❤️2
    4
    一行shell命令就够了:

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

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

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

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