如何清除socket.gethostbyname响应的缓存? [英] How to flush cache for socket.gethostbyname response?

查看:861
本文介绍了如何清除socket.gethostbyname响应的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人之前遇到过:

更新DNS记录后,我做一个挖掘test.somedomain.com我得到167.69.143.234,但是我做一个socket.gethostbyname('test.somedomain.com')我得到167.69.6.234。

After updating DNS records..I do a dig for 'test.somedomain.com' I get 167.69.143.234, however when I do a socket.gethostbyname('test.somedomain.com') I get 167.69.6.234.

我猜猜socket还在使用缓存...我该如何清除?或者刷新?

I'm guessing socket is still using cache...how do I clear it ? or flush it?

我的代码非常简单:

Linux Termianl

Linux Termianl

dig test.somedomain.com

Python:

import socket
socket.gethostbyname('test.somedomain.com')

应该返回167.69.143.234地址,因为这是DNS中更新的地址。

It should be returning the 167.69.143.234 address as that is the updated one in DNS.

推荐答案

Python的 socket.gethostbyname 使用操作系统解析器,没有用于清除其缓存的API。缓存(可能是由操作系统或操作系统或标准库组件使用的缓存DNS服务器)是DNS系统的基本元素,并且正确的方法来处理它是等到记录的TTL值到期(操作系统应该从缓存中删除陈旧的值)。当您更新DNS时,您应该可以更早地调整旧值的TTL。

Python's socket.gethostbyname uses the operating system resolver and has no API for clearing its cache. The cache (which may be a caching DNS server used by the operating system or a operating system or standard library component) is a fundamental element of the DNS system and 'the right way' to cope with it is to wait until the record's TTL value expires (operating system should remove the stale value from the cache then). When updating the DNS you should probably have TTL of the old value adjusted earlier.

您还可以使用Python DNS实现,例如DNSPython,而不是使用 socket.gethostbyname - 您应该完全控制解析器缓存(但不是解析器使用的NS缓存)。虽然,它不会解决您的问题(使用现有的代码,我猜)。

You could also use a Python DNS implementation, like DNSPython instead of using socket.gethostbyname – you should have the full control over the resolver cache (but not the caches of NS the resolver uses) then. Though, it won't probably fix your problem (with an existing code, I guess).

这篇关于如何清除socket.gethostbyname响应的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆