Python urllib 缓存

发布于 2024-11-25 13:00:49 字数 801 浏览 3 评论 0原文

我正在用 Python 编写一个脚本来确定它是否可以访问互联网。

import urllib

CHECK_PAGE     = "http://64.37.51.146/check.txt"
CHECK_VALUE    = "true\n"
PROXY_VALUE    = "Privoxy"
OFFLINE_VALUE  = ""

page = urllib.urlopen(CHECK_PAGE)
response = page.read()
page.close()

if response.find(PROXY_VALUE) != -1:
    urllib.getproxies = lambda x = None: {}
    page = urllib.urlopen(CHECK_PAGE)
    response = page.read()
    page.close()

if response != CHECK_VALUE:
    print "'" + response + "' != '" + CHECK_VALUE + "'" # 
else:
    print "You are online!"

我在计算机上使用代理,因此正确的代理处理很重要。如果它无法通过代理连接到互联网,它应该绕过代理并查看它是否卡在登录页面(就像我使用的许多公共热点一样)。使用该代码,如果我没有连接到互联网,第一个 read() 将返回代理的错误页面。但是当我之后绕过代理时,我得到了相同的页面。如果我在发出任何请求之前绕过代理,我会得到一个错误,就像我应该的那样。我认为 Python 从第一次就开始缓存页面。

如何强制 Python 清除其缓存(或者这是其他问题)?

I'm writing a script in Python that should determine if it has internet access.

import urllib

CHECK_PAGE     = "http://64.37.51.146/check.txt"
CHECK_VALUE    = "true\n"
PROXY_VALUE    = "Privoxy"
OFFLINE_VALUE  = ""

page = urllib.urlopen(CHECK_PAGE)
response = page.read()
page.close()

if response.find(PROXY_VALUE) != -1:
    urllib.getproxies = lambda x = None: {}
    page = urllib.urlopen(CHECK_PAGE)
    response = page.read()
    page.close()

if response != CHECK_VALUE:
    print "'" + response + "' != '" + CHECK_VALUE + "'" # 
else:
    print "You are online!"

I use a proxy on my computer, so correct proxy handling is important. If it can't connect to the internet through the proxy, it should bypass the proxy and see if it's stuck at a login page (as many public hotspots I use do). With that code, if I am not connected to the internet, the first read() returns the proxy's error page. But when I bypass the proxy after that, I get the same page. If I bypass the proxy BEFORE making any requests, I get an error like I should. I think Python is caching the page from the 1st time around.

How do I force Python to clear its cache (or is this some other problem)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2024-12-02 13:00:49

每次调用 urllib.urlopen() 之前调用 urllib.urlcleanup() 即可解决问题。实际上,urllib.urlopen 将调用 urlretrive() 函数,该函数创建一个缓存来保存数据,而 urlcleanup() 将删除它。

Call urllib.urlcleanup() before each call of urllib.urlopen() will solve the problem. Actually, urllib.urlopen will call urlretrive() function which creates a cache to hold data, and urlcleanup() will remove it.

南巷近海 2024-12-02 13:00:49

您想要

page = urllib.urlopen(CHECK_PAGE, proxies={})

删除该

urllib.getproxies = lambda x = None: {}

行。

You want

page = urllib.urlopen(CHECK_PAGE, proxies={})

Remove the

urllib.getproxies = lambda x = None: {}

line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文