How to parse HTTP time header?

View: New views
5 Messages — Rating Filter:   Alert me  

How to parse HTTP time header?

by Kevin Ar18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
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?
How about in one of the Python web frameworks?  Do any have a more robust solution already done?


Hotmail: Trusted email with powerful SPAM protection. Sign up now.
--
http://mail.python.org/mailman/listinfo/python-list

Re: How to parse HTTP time header?

by Philip Semanchuk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Nov 7, 2009, at 10:39 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.


HTH
Philip
--
http://mail.python.org/mailman/listinfo/python-list

Parent Message unknown Re: How to parse HTTP time header?

by Kevin Ar18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
>> 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. :)


Hotmail: Trusted email with powerful SPAM protection. Sign up now.
--
http://mail.python.org/mailman/listinfo/python-list

Re: How to parse HTTP time header?

by Philip Semanchuk :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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
--
http://mail.python.org/mailman/listinfo/python-list

Parent Message unknown RE: How to parse HTTP time header?

by Kevin Ar18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
>>>> 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.
That's exactly what I had in mind when I said, I could write one quickly.  :)  I guess I can always do it that way if the email.util one doesn't work right. :)
Thanks,
Kevin


Hotmail: Trusted email with Microsoft's powerful SPAM protection. Sign up now.
--
http://mail.python.org/mailman/listinfo/python-list