V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
phuslu
V2EX  ›  Python

一个python解析DNS的小函数

  •  
  •   phuslu · 2012-03-13 00:09:18 +08:00 · 10880 次点击
    这是一个创建于 4421 天前的主题,其中的信息可能已经有所发展或是发生改变。
    https://gist.github.com/2023017

    比较粗糙,唯一的优点就是不依赖dnslib/pydns等第三方库。

    呃,我还不会在v2ex插入gist
    18 条回复    1970-01-01 08:00:00 +08:00
    freestyler
        1
    freestyler  
       2012-03-13 10:14:47 +08:00
    标准库不是有嘛..


    getaddrinfo

    gethostbyname
    phuslu
        2
    phuslu  
    OP
       2012-03-13 10:25:26 +08:00
    @freestyler 这个函数可以指定dnsserver, 用来防止dns劫持。
    bhuztez
        3
    bhuztez  
       2012-03-13 10:40:00 +08:00
    推荐 dnspython http://www.dnspython.org/
    phuslu
        4
    phuslu  
    OP
       2012-03-13 10:46:10 +08:00
    @bhuztez 这段代码是从最新版的goagent摘出来的,好处就不用引入额外的依赖。比较适合goagent对多平台支持的需求。
    其实我觉得gevent的dns解析函数更好用,推荐一下。
    可以看下这个链接的评论
    http://blog.gevent.org/2011/04/28/libev-and-libevent/
    liruqi
        5
    liruqi  
       2012-03-13 11:10:02 +08:00
    1. 拜托把几个import 写一下..
    2. 有些query 仅返回 cname, 可能需要递归查询, 不知道是否有处理.
    3. 我自己测试发现这个函数执行时间很长啊
    4. 给一个西厢代理初始版本的代码 https://gist.github.com/2026329 也是自己实现的dns query, 大体上来讲, 还是不如第三方库稳定.
    phuslu
        6
    phuslu  
    OP
       2012-03-13 11:18:40 +08:00
    @liruqi
    1. 恩,加上了。
    2. 这个就依赖具体的host和dnsserver了,在goagent还可以用hosts机制事前指定
    3. 是走的tcp,goagent不过在上层做了cache,和本身的Host机制结合很好。
    4. 恩,谢谢。
    phuslu
        7
    phuslu  
    OP
       2012-03-13 11:23:55 +08:00
    目前的话,dns查询走tcp还是一个比较不错的办法。因为不会有dns投毒了。
    虽然比udp慢些,但是一旦上层做了缓存,那么函数本身的效率就可以忽略了。
    monkeylyf
        8
    monkeylyf  
       2012-10-02 00:50:33 +08:00
    mark学习之. 之前用adns module写过一个.
    Livid
        9
    Livid  
    MOD
       2012-10-02 00:55:07 +08:00
    谢谢分享,把 https 改成 http 就可以显示 gist 了:

    http://gist.github.com/2023017
    liruqi
        10
    liruqi  
       2012-10-02 01:26:05 +08:00
    样式略有错乱。
    宽屏下可以考虑把页面宽度放大。
    clowwindy
        11
    clowwindy  
       2012-10-02 09:16:54 +08:00
    谢谢分享。gethostbyname 万恶的地方就在于它是阻塞的。
    iandyh
        12
    iandyh  
       2012-10-02 11:53:50 +08:00
    @clowwindy 用 gevent 呢?
    ayanamist
        13
    ayanamist  
       2012-10-03 15:30:26 +08:00
    楼主,try finally里是可以没有except的……你不想处理,就去掉那无用的except Exception吧……
    发现你很喜欢写except: xxx这样的代码,哪怕实际上不需要。
    ccp0101
        14
    ccp0101  
       2012-10-03 22:15:08 +08:00
    @liruqi 最近实现的Kernet替代,可以看看否? https://github.com/ccp0101/west-chamber-mac
    lililala
        15
    lililala  
       2012-10-05 10:35:08 +08:00
    @phuslu,lz,这个怎么用呢?
    liruqi
        16
    liruqi  
       2012-10-05 13:46:40 +08:00
    @ccp0101 看到了。最近在做tomato路由器,过段时间再看看。你如果有设备我可以给你个教程,效果不错。
    holmesabc
        17
    holmesabc  
       2012-10-05 15:40:31 +08:00
    @Livid 带头挖坟啊。

    顺便mark个,说不定有用
    asanailiulang
        18
    asanailiulang  
       2013-06-20 09:37:51 +08:00
    试了一下,其他的DNS服务器好像不支持~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1221 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:07 · PVG 02:07 · LAX 11:07 · JFK 14:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.