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
leavic
V2EX  ›  Python

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

  •  1
     
  •   leavic · 2017-04-26 15:30:09 +08:00 · 4483 次点击
    这是一个创建于 2549 天前的主题,其中的信息可能已经有所发展或是发生改变。
    简单点说,我想用 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 了。
    第 1 条附言  ·  2017-04-27 10:43:14 +08:00
    搞定了,暴力重写 Connector 中的_resolve_host()函数

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

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

    不过正确的实现像五楼说的那样。
    TJT
        7
    TJT  
       2017-04-27 00:18:54 +08:00
    不过正确的实现*应该*像五楼说的那样。
    leavic
        8
    leavic  
    OP
       2017-04-27 10:12:48 +08:00
    @TJT 我希望的就是这个东西能够完全独立于其他的服务,只依赖基本的网络连接。
    andrewhxism
        9
    andrewhxism  
       2017-04-27 10:16:39 +08:00
    @leavic 不可能的,基本网络连接也需要初始 DNS 的
    leavic
        10
    leavic  
    OP
       2017-04-27 10:39:44 +08:00
    @andrewhxism 别扯,我已经搞定了。
    leavic
        11
    leavic  
    OP
       2017-04-27 10:47:48 +08:00
    @andrewhxism 好吧,我错了,好像还是有问题。
    leavic
        12
    leavic  
    OP
       2017-04-27 11:00:54 +08:00
    @andrewhxism 哈哈哈,是我代码写错了,还是可以的。
    TJT
        13
    TJT  
       2017-04-27 13:22:31 +08:00
    @leavic 基本的网络连接怎么定义???
    candane
        14
    candane  
       2017-04-27 13:50:43 +08:00
    楼主搞定了没有??
    leavic
        15
    leavic  
    OP
       2017-04-27 14:45:18 +08:00
    @TJT 有 TCP 和 UDP 连接就行啊
    leavic
        16
    leavic  
    OP
       2017-04-27 14:45:54 +08:00
    @candane append 的部分就是可用的代码啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3083 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:39 · PVG 22:39 · LAX 07:39 · JFK 10:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.