Python Forum

Full Version: Review my code: convert a HTTP date header to a datetime object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please review my code. Is this the right thing to do?

I get a HTTP date stamp from a web response, and convert it into a datetime object:

import datetime
from urllib import request

URL = "https://www.unicode.org/Public/UNIDATA/Scripts.txt"
response = request.urlopen(URL)
s = response.getheader('DATE')
obj = datetime.datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %Z')
This seems to work for the specific file I am trying to download. Is this the best way? (Note: for reasons, I'm limited to only using the stdlib, so no requests module, sorry.)

For arbitrary URLs, how do I know what datetime format string to use?

Thanks in advance.
(Dec-17-2022, 12:03 AM)stevendaprano Wrote: [ -> ]For arbitrary URLs, how do I know what datetime format string to use?
The http header date will usually be the same as it's stander set in RFC 9110 in Date/Time Formats.
The two obsolete formats i guess now is very rare.
Not all sites will allows this then will get 403.