简体   繁体   English

用python和cmd查找ip地址之间的区别

[英]difference between finding ip address by python an cmd

I wrote this code for finding google ip in python 我写了这段代码来查找python中的google ip

import socket
print socket.gethostbyname('google.com')
.
.
173.194.39.0

but if we use command prompt and ping command for finding google ip result is:216.58.208.36 why there is difference between two results? 但是如果我们使用命令提示符和ping命令查找google ip,结果是:216.58.208.36为什么两个结果之间有区别?

Both of those IP addresses resolve to Google.com. 这两个IP地址都解析为Google.com。 We can verify this from the command line with the unix whois command. 我们可以使用unix whois命令从命令行进行验证。

$ whois 216.58.208.36 

NetRange:       216.58.192.0 - 216.58.223.255
CIDR:           216.58.192.0/19
NetName:        GOOGLE

$ whois 173.194.39.0

NetRange:       173.194.0.0 - 173.194.255.255
CIDR:           173.194.0.0/16
NetName:        GOOGLE

I ran into this same issue and the cause was that the first command that required an IP address was using a cached DNS entry (because the DNS entry's time to live (TTL) hadn't expired yet) and then by the time the second command was issued the TTL had expired on the cached entry so a new DNS request was made for the domain therefore grabbing a new IP address from the DNS server which happened to be different because the domain had a lot of IP addresses just like Google.com. 我遇到了同样的问题,原因是第一个需要IP地址的命令正在使用缓存的DNS条目(因为DNS条目的生存时间(TTL)尚未到期),然后在第二条命令之前发出TTL后,缓存项上的TTL过期了,因此对该域发出了新的DNS请求,因此从DNS服务器获取了一个新的IP地址,该地址恰好是不同的,因为该域具有许多类似Google.com的IP地址。

Python just relies on the Operating System's DNS resolver (or whatever daemon is running) and as far as I know the socket module doesn't give you the ability to clear the DNS cache before it tries to resolve an address. Python仅依赖于操作系统的DNS解析器(或正在运行的守护程序),据我所知,套接字模块无法让您在尝试解析地址之前清除DNS缓存。 If you want more control over this functionality you can use DNSPython or something similar. 如果要对该功能进行更多控制,可以使用DNSPython或类似的方法。 If you are using a daemon for DNS on your operating system (like on Linux, for example) then usually restarting the daemon will force a flush of DNS cache and you find both addresses to the be same (unless you run into the timing issue as described above with the TTL's expiring). 如果您在操作系统上使用DNS守护程序(例如,在Linux上),那么通常重新启动守护程序将强制刷新DNS缓存,并且您发现两个地址都相同(除非您遇到了如下计时问题) TTL过期)。

Hostnames are translated to IP addresses through something called a DNS server. 主机名通过称为DNS服务器的名称转换为IP地址。 When you type a name into a web browser or use a program such as ping, the hostname that you provide (google.com) eventually reaches an authoritative DNS server for that domain-separate from the server that you correspond with for the actual content. 当您在网络浏览器中键入名称或使用诸如ping之类的程序时,您提供的主机名(google.com)最终到达该域的权威DNS服务器,该服务器与实际内容所对应的服务器分开。

google.com has multiple different servers that can respond to data requests. google.com有多个不同的服务器,可以响应数据请求。 Depending on the implementation of the different programs you are using to generate the request and other factors such as the network traffic at the time that you make the request, multiple requests from the same host may be directed to different servers by the authoritative DNS server. 根据用于生成请求的不同程序的实现以及其他因素(例如,发出请求时的网络流量),权威DNS服务器可能会将来自同一主机的多个请求定向到不同的服务器。 This is accomplished by returning different IP addresses to your machine. 这是通过将不同的IP地址返回到您的计算机来完成的。

FWIW, both ping and socket.gethostbyname() for google.com resolve to 216.58.217.14 on my machine, running OS X Yosemite. FWIW,适用于google.com的ping和socket.gethostbyname()都在运行OS X Yosemite的计算机上解析为216.58.217.14。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM