[Tutor] cookie expiration date format

Tim Johnson tim at johnsons-web.com
Mon Mar 19 10:30:43 CET 2007


On Monday 19 March 2007 15:33, Mike Hansen wrote:
 
 
> Some of the modules in the Python standard library make things a little
> more difficult than other languages.(Perl, Ruby, ...) This is a good
> example of it. Are there any 3rd party modules that let you set the
> expiration date to 'yesterday'? I know I could write a wrapper, but I'd
> rather not re-invent the wheel and save some time.

 I've written my own. The time format function was all I needed to handle
the expiration. Having had to write cookie objects in other languages from
scratch, I knew what I was after, just a matter of finding the right python
module and function to do it. datetime was the key.

> Another example is ftplib. Other language's ftp modules/libraries allow
> you do something like sendaciifile(fh) or sendbinaryfile(fh) yet you
> need to build your own wrapper functions in Python.
> http://effbot.org/librarybook/ftplib.htm

 Wrote my own wrapper for ftplib years ago. with those function included...

 I think it is inevitable that wrappers get written because programmers have
 unique circumstance and needs. In my case, I not only needed to expire a
 cookie, it served my needs to embedded multiple names and values in
 the cookie value.

 another example, I wrote a cgi wrapper so I could make calls like
 cgi["first_name"] ## where __getitem__ gets the value for posted field name

 I think the cookie library is great. Just needs some more docs and examples.
 (IMHO)
 that would make it easier for other programmers to write their own wrappers.

The function looks like this:
	def expire(self,name):
		c = self.get(name) ## retrieve cookie by name
		if c: 
			time_format = "%a, %d %b %Y"
			expires = "%s" % (
				(datetime.datetime.now() ## set the expiration date to yesterday
				+ datetime.timedelta(-1)).strftime(time_format)
				)   ## one second might be good enough!
			self.expires = expires
			self.set(name,c)
I choose not to override del (using __del__) because it would have 
necessitated a second argument to del(), as in del(cookie,cookie_name)
My class handless multiple cookies.

Thanks Mike
tim

> Mike
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Tim Johnson <tim at johnsons-web.com>
Palmer, Alaska, USA


More information about the Tutor mailing list