Python Forum
Review my code: convert a HTTP date header to a datetime object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Review my code: convert a HTTP date header to a datetime object
#1
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.
Reply
#2
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 249 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python best library for Excel reports & review of existing code MasterOfDestr 4 677 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
  Python date format changes to date & time 1418 4 621 Jan-20-2024, 04:45 AM
Last Post: 1418
  convert address and broadcast to network object Skaperen 9 1,808 Mar-09-2023, 06:55 PM
Last Post: Skaperen
  Convert Date to another format lonesoac0 2 1,680 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Filter dataframe by datetime.date column glidecode 2 5,181 Dec-05-2021, 12:51 AM
Last Post: glidecode
  Date format and past date check function Turtle 5 4,280 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  convert c code python satyanarayana 5 5,617 Sep-21-2021, 07:45 PM
Last Post: deanhystad
  This is an Object - Code interpretation JoeDainton123 2 1,818 Jun-16-2021, 08:11 PM
Last Post: snippsat
  Docx Convert Word Header to Body CaptainCsaba 3 2,772 Jun-02-2021, 01:25 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020