【问题标题】:How to get the ip address when to browse the domain name?浏览域名时如何获取ip地址?
【发布时间】:2017-06-20 11:35:23
【问题描述】:
dig www.sina.com.cn
jupiter.sina.com.cn.        30      IN      A       183.232.24.117
jupiter.sina.com.cn.        30      IN      A       183.232.24.115
jupiter.sina.com.cn.        30      IN      A       183.232.24.112
jupiter.sina.com.cn.        30      IN      A       183.232.24.114
jupiter.sina.com.cn.        30      IN      A       183.232.24.111
jupiter.sina.com.cn.        30      IN      A       183.232.24.113
jupiter.sina.com.cn.        30      IN      A       183.232.24.116

表示域名www.sina.com.cn可以解析为上述7个ip之一。

我想知道下面的代码究竟要访问哪个ip。

import requests
source = 'http://www.sina.com.cn'
r = requests.get(source)
print(r.headers)

{'Server': 'nginx', 'Expires': 'Fri, 03 Feb 2017 06:41:23 GMT', 'Date': 'Fri, 03 Feb 06:40:23 GMT', 'Age ': '10', 'Content-Encoding': 'gzip', 'Content-Length': '133766', 'Content-Type': 'text/html', 'X-Cache': 'HIT from cmnet.xxg .18a4.34.spool.sina.com.cn', 'X-Powered-By': 'shci_v1.03', 'Vary': 'Accept-Encoding', 'Cache-Control': 'max-age=60 ', 'Last-Modified': 'Fri, 03 Feb 2017 06:40:00 GMT'}

没有IP地址信息。

ping  cmnet.xxg.18a4.34.spool.sina.com.cn
ping: unknown host cmnet.xxg.18a4.34.spool.sina.com.cn

上述python代码向www.sina.com.cn域名发送请求时访问的是哪个IP地址? 打开wireshark监控进程。

此时www.sina.com.cn被解析成183.232.24.111,也许下次可能会被解析成183.232.24.117,不管是哪一个,我都希望用我的python代码显示。

IP 将在 (183.232.24.111-183.232.24.117) 范围内从一个更改为另一个。

【问题讨论】:

  • 您会看到一个轮询 DNS 设置,它有意从池中返回一个 IP 地址。这主要用于负载平衡和故障转移。出于同样的原因,还有一个反向代理。所有这些对用户来说并不重要,而且很可能对你也不重要。

标签: python dns ip python-requests


【解决方案1】:
>>> import requests
>>> r = requests.get('http://www.sina.com.cn', stream=True)
>>> r.raw._connection.sock.getpeername()
('151.249.91.56', 80)

Body Content Workflow

more

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-16
    • 2011-04-04
    • 2012-05-11
    • 2011-01-28
    • 2011-01-28
    • 2023-04-08
    • 2012-09-04
    • 2016-06-25
    相关资源
    最近更新 更多