How can I get the last-modified time with python3 urllib?
Asked Answered
B

1

12

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.

Bridoon answered 16/2, 2011 at 21:5 Comment(0)
M
17

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

Multitudinous answered 16/2, 2011 at 22:50 Comment(1)
Also, the header returns a string, but the old method returned a struct_time (or similar). This should work to massage it: time_struct = time.strptime(last_modified, '%a, %d %b %Y %H:%M:%S %Z')Bridoon

© 2022 - 2024 — McMap. All rights reserved.