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