推荐学习书目
› 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
wsycqyz
V2EX  ›  Python

gethostbyname()是真的会发 DNS 查询出去?还是会被操作系统截获,如果有 cache 就不发 DNS 查询?

  •  
  •   wsycqyz · Oct 18, 2017 · 4590 views
    This topic created in 3308 days ago, the information mentioned may be changed or developed.
    11 replies  •  2017-10-18 21:24:47 +08:00
    oott123
    oott123
    Oct 18, 2017 via Android
    1
    啥操作系统?
    mengyaoss77
    mengyaoss77
    Oct 18, 2017 via Android
    2
    抓个包试试。应该先通过操作系统的,过一圈 hosts 文件,至于缓存就不知了。
    noli
    noli
    Oct 18, 2017
    3
    有需要的话其实可以自己写一个 gethostbyname 的替代实现。
    找一个开源的实现也很容易。
    即使是同一种 OS 的不同机器,gethostbyname 到底查询了哪里不一定是一样的。
    fcten
    fcten
    Oct 18, 2017
    4
    gethostbyname 本身并不决定是否缓存,是否缓存是由系统决定的
    最简单的道理,hosts 文件总是优先于真正的 dns 查询的
    imn1
    imn1
    Oct 18, 2017
    5
    调用系统的
    wsycqyz
    wsycqyz
    OP
    Oct 18, 2017
    6
    测试了一下,某个网址 ping 每次都是不同的 IP,
    调用 gethostbyname 1000 次都是一个 IP
    如果加上 sleep(0.1) 可以得到 2 到 3 个不同 IP
    leavic
    leavic
    Oct 18, 2017
    7
    应该是调用系统的查询接口,本身并不会发起真正的查询。
    imnpc
    imnpc
    Oct 18, 2017
    8
    测试 hosts 可破...
    fzleee
    fzleee
    Oct 18, 2017❤️1
    9
    gethostbyname 其实是一个系统调用, 所以你的实际问题应该是这个系统效用会不会触发网络请求
    crab
    crab
    Oct 18, 2017
    10
    抓包试了下,顺序如下。
    1:先 hosts 文件查询
    2:再缓存查询
    3:没有就 dns 查询
    dayinfinte
    dayinfinte
    Oct 18, 2017
    11
    这个还是使用系统的 cache,。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1470 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 11:16 · PVG 19:16 · LAX 03:16 · JFK 06:16
    ♥ Do have faith in what you're doing.