Re: How to parse HTTP time header?

Discussion in 'Python' started by Philip Semanchuk, Nov 8, 2009.

  1. On Nov 7, 2009, at 10:56 PM, Kevin Ar18 wrote:

    >
    >>> Basically, I'm wondering if it is part of the standard library
    >>> somewhere before I code my own.
    >>>
    >>> Page 20 of RFC2616 (HTTP) describes the format(s) for the time
    >>> header. It wouldn't be too difficult for me to code up a solution
    >>> for the 3 standard formats, but what get's me is the little note
    >>> about how some servers may still send badly format time headers. :(
    >>> So, I'm curious if this has already been done in the standard Python
    >>> library?

    >>
    >> The parsedate() function in the rfc822 module does this and claims to
    >> be tolerant of slightly malformed dates, but that module is
    >> deprecated
    >> as of Python 2.5 in favor of the email module which hopefully has an
    >> equivalent function.

    > Thanks, I'll give 'em a look. :)




    Sorry, my mistake -- 2616 != 2822. I'm not sure if there's something
    in the standard library for parsing RFC 2616 dates.

    When I faced the problem of parsing HTTP dates, I wrote my own
    function although this was in an application that was deliberately
    unforgiving of invalid input and therefore my code makes no allowances
    for it. FWIW, it parsed over 1 million dates without encountering any
    that raised an error.

    Here it is, written in a time when I obviously didn't have total
    respect for PEP 8.

    ASCTIME_FORMAT = "%a %b %d %H:%M:%S %Y"
    RFC_850_FORMAT = "%A, %d-%b-%y %H:%M:%S GMT"
    RFC_1123_FORMAT = "%a, %d %b %Y %H:%M:%S GMT"

    def HttpDateToFloat(HttpDateString):
    # Per RFC 2616 section 3.3, HTTP dates can come in three flavors --
    # Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123
    # Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036
    # Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
    if not HttpDateString.endswith("GMT"):
    date = time.strptime(HttpDateString, ASCTIME_FORMAT)
    else:
    if "-" in HttpDateString:
    # RFC 850 format
    date = time.strptime(HttpDateString, RFC_850_FORMAT)
    else:
    # RFC 822/1123
    date = time.strptime(HttpDateString, RFC_1123_FORMAT)

    return calendar.timegm(date)



    bye
    Philip
    Philip Semanchuk, Nov 8, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivek Mehta
    Replies:
    3
    Views:
    8,646
    Vivek Mehta
    Aug 12, 2004
  2. flamesrock
    Replies:
    8
    Views:
    430
    Hendrik van Rooyen
    Nov 24, 2006
  3. mlt
    Replies:
    2
    Views:
    805
    Jean-Marc Bourguet
    Jan 31, 2009
  4. Peter Krantz
    Replies:
    2
    Views:
    183
  5. John Carter

    a=Time.now;(Time.parse(a.to_s)-a)!=0

    John Carter, Oct 26, 2007, in forum: Ruby
    Replies:
    3
    Views:
    140
    ara.t.howard
    Oct 26, 2007
Loading...

Share This Page