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

当系统 DNS 不可用的时候,如何给 Python 程序指定 DNS 服务器,或者有没有类似 Hosts 的实现方法?

  •  1
     
  •   leavic · Apr 26, 2017 · 5675 views
    This topic created in 3484 days ago, the information mentioned may be changed or developed.
    简单点说,我想用 Google 的 DNS Over Https 。

    但这里有一个问题,如果我只是作为一个候选的 DNS 服务来启动是没问题的,但如果要作为本机唯一的 DNS 服务器,这就有个鸡生蛋蛋生鸡的问题了:

    我需要访问 dns.google.com 才能解析数据,可是我不知道 dns.google.com 的 IP 地址,于是我向自己查询,然后死循环。

    所以,有没有什么办法可以给 python 程序指定一个 DNS 解析器用于一些必要的基本解析呢,例如用 114DNS 先完成必要的域名解析。

    或者有没有类似写 Hosts 的方法,直接告诉 python 这些域名的地址,具体的来说,是告诉 urllib 和 aiohttp 域名的 A 地址,这样就不需要额外的 DNS 来解析 dns.google.com 了。
    Supplement 1  ·  Apr 27, 2017
    搞定了,暴力重写 Connector 中的_resolve_host()函数

    Supplement 2  ·  Apr 28, 2017
    16 replies  •  2017-04-27 14:45:54 +08:00
    yexiaoxing
    yexiaoxing
    Apr 26, 2017
    1
    request 的时候直接用 IP 替代 dns.google.com ,然后加上一个 HOST: dns.google.com 的 header ?
    denghongcai
    denghongcai
    Apr 26, 2017❤️1
    2
    先用 dnspython 去查 ip ,然后给 urllib 设置 Host 头,用 IP 去请求就好了
    qinc
    qinc
    Apr 26, 2017
    3
    dnspod 家的 httpdns 貌似可以解决
    leavic
    leavic
    OP
    Apr 26, 2017
    4
    @denghongcai urllib 对付 http 请求这样搞是可以,不过用 aiohttp 请求 https://dns.google.com 的时候就会爆 ssl 错误,还要看看。
    denghongcai
    denghongcai
    Apr 26, 2017❤️1
    5
    ssl 握手包里有域名,这样确实不行

    自定义一个 connector 应该可以 http://aiohttp.readthedocs.io/en/stable/_modules/aiohttp/connector.html
    TJT
    TJT
    Apr 27, 2017
    6
    简单粗暴的方法,起个 dnsmasq 中转 DNS 查询,然后 Python 用 dnspython 去查 IP ,查到就更新到 dnsmasq 里面,再去请求就好了。

    不过正确的实现像五楼说的那样。
    TJT
    TJT
    Apr 27, 2017
    7
    不过正确的实现*应该*像五楼说的那样。
    leavic
    leavic
    OP
    Apr 27, 2017
    8
    @TJT 我希望的就是这个东西能够完全独立于其他的服务,只依赖基本的网络连接。
    andrewhxism
    andrewhxism
    Apr 27, 2017
    9
    @leavic 不可能的,基本网络连接也需要初始 DNS 的
    leavic
    leavic
    OP
    Apr 27, 2017
    10
    @andrewhxism 别扯,我已经搞定了。
    leavic
    leavic
    OP
    Apr 27, 2017
    11
    @andrewhxism 好吧,我错了,好像还是有问题。
    leavic
    leavic
    OP
    Apr 27, 2017
    12
    @andrewhxism 哈哈哈,是我代码写错了,还是可以的。
    TJT
    TJT
    Apr 27, 2017
    13
    @leavic 基本的网络连接怎么定义???
    candane
    candane
    Apr 27, 2017
    14
    楼主搞定了没有??
    leavic
    leavic
    OP
    Apr 27, 2017
    15
    @TJT 有 TCP 和 UDP 连接就行啊
    leavic
    leavic
    OP
    Apr 27, 2017
    16
    @candane append 的部分就是可用的代码啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1627 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 154ms · UTC 16:18 · PVG 00:18 · LAX 08:18 · JFK 11:18
    ♥ Do have faith in what you're doing.