如何使用 python3 urllib 获取最后修改时间?

发布于 2024-10-17 21:25:13 字数 535 浏览 11 评论 0原文

我正在从 python2 移植 我的程序到 python3,我遇到了以下错误: AttributeError: 'HTTPMessage' object has no attribute 'getdate'

这是代码:

conn = urllib.request.urlopen(fileslist, timeout=30)
last_modified = conn.info().getdate('last-modified')

本节在 python 2.7 下工作,到目前为止我还没有能够找到在python 3.1中获取此信息的正确方法。

完整的上下文是一个更新方法。它将新文件从服务器拉到本地数据库,但前提是服务器上的文件比本地文件新。如果有一种比仅比较本地和远程文件时间戳更智能的方法来实现此功能,那么我也对此持开放态度。

I'm porting over a program of mine from python2 to python3, and I'm hitting the following error: AttributeError: 'HTTPMessage' object has no attribute 'getdate'

Here's the code:

conn = urllib.request.urlopen(fileslist, timeout=30)
last_modified = conn.info().getdate('last-modified')

This section worked under python 2.7, and so far I haven't been able to find out the correct method to get this information in python 3.1.

The full context is an update method. It pulls new files from a server down to its local database, but only if the file on the server is newer than the local file. If there's a smarter way to achieve this functionality than just comparing local and remote file timestamps, then I'm open to that as well.

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

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

发布评论

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

评论(1

ζ澈沫 2024-10-24 21:25:13

conn.headers['last-modified'] 在 Python 2 和 Python 3 下都可以工作。比较文件戳对我来说似乎是合理的。

conn.headers['last-modified'] works under both Python 2 and Python 3. Comparing filestamps seems reasonable to me.

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