Setting and Getting Cookies in Google’s WebApp

Here’s the disclaimer: I’m currently teaching myself Python for use with Google’s App Engine and (unlike my PHP or Perl) I’m not entirely sure if the below is best practice or even remotely sane, but that said, it does seem to work for me!

Setting a Cookie

Notes: Obviously the code is running in a controller (or rather a Request Handler) that extends webapp.RequestHandler and you’ll notice that I had to use a Regular Expression to remove the header name from Cookie.SimpleCookie’s output so I could tie it in to webapp’s header frame work which should stop any output buffering issues in the future.


import Cookie
import re
import base64

class YourHandler(webapp.RequestHandler):
   cookieName = 'MyCookie'
   domain = ''
   expires = 360
   value = 'Some value or something...'

   def get(self):
         simpleCookieObj = Cookie.SimpleCookie()

         simpleCookieObj[self.cookieName] = str(base64.b64encode(self.value))
         simpleCookieObj[self.cookieName]['expires'] = self.expires
         simpleCookieObj[self.cookieName]['path'] = '/'
         simpleCookieObj[self.cookieName]['domain'] = self.domain
         simpleCookieObj[self.cookieName]['secure'] = ''

         #Cookie.SimpleCookie's output doesn't seem to be compatible with WebApps's http header functions
         #and this is a dirty fix

         headerStr = simpleCookieObj.output()
         regExObj = re.compile('^Set-Cookie: ')
         self.response.headers.add_header('Set-Cookie', str(regExObj.sub('', headerStr, count=1)))

Getting a Cookie

Notes: This is much easier, we just have to make sure that it exists.


import Cookie
import base64

class YourOtherHandler(webapp.RequestHandler):
   cookieName = 'MyCookie'

   def get(self):
      try:
         cookieValue = str(base64.b64decode(self.request.cookies[self.cookieName]))
      except KeyError:
         #There wasn't a Cookie called that
         cookieValue = ''

Changing Default hostname in Google App Engine Launcher

Google App Engine\'s WebApp preferences

These past few weeks I’ve mostly been writing Python for Google’s App Engine and listening to Country Music. They say a change is as good as a holiday, so two changes must be equivalent to a cruise or something.

To cut to the chase, today I was testing some Cookie functionality and needed to run my WebApp on another domain other than localhost and of course, since I was using the App Engine Launcher GUI rather than the command line, I couldn’t find any documentation on how to do it.

To be fair, I didn’t look very hard, I just tried the following:

I added –address= followed by the hostname I wanted to run my WebApp under and low and behold I could access it across my LAN!

Talk is Cheap.


The above image will be deleted once it’s been downloaded from my server 100 times.

Brilliant…

Mom\'s Mini Van: Less conformist than the buss

Rob found this on the internet today… I thought it was one of the funniest things I’ve ever seen!

After some consideration I decided to post it on my blog in order to stick it to the conformists out there.

Poor kid - I’m glad we didn’t have photo sharing sites when I was 14!

Impossible Gherkins vs. One Touch Jar Opener

Having spent 99p on a jar of pickled gherkins that no one could open, I hit Google in search of a solution and quickly found a device that appealed to the Heath Robinson in me and was only a mere 20 times what I paid for the gherkins in the first place.

The device is called the One Touch Jar Opener and is available exclusively (I think) from Best Direct, although I got it via their Amazon store… it’s one of those only available via our T.V. store (and website) companies.

The box turned up as promised and something in my head told me that it’d be hilarious to film and put on YouTube… even if the jar didn’t shatter potentially blinding me for life, the very fact that both a silly gadget and a jar of pickled gherkins were involved guaranteed a certain comedy value.

As you can see below, despite the promised 25 pounds of torque the Jar Opener did struggle with the problem jar. This made me feel slightly vindicated for not being able to open it by hand… although this feeling soon turned to anxiety when it looked increasingly likely broken glass was shortly going to be everywhere. Yes, I did jump when the lid finally gave in!

So below is my first (and potentially last) YouTube video… enjoy!

[SPOILERS] Death Note: Another Note

Death Note: Another Note

I’m a huge fan of the Manga series Death Note and have just finished the spin off novel Death Note: Another Note, The Los Angeles BB Murder Cases. The story is a prequel that sees my hero L team up with the future wife of doomed FBI agent Raye Penber in a battle of wits with another of L’s bitter heirs, B.

As the story progresses L’s investigation-by-proxy uncovers some increasingly tenuous clues that leave the reader wondering how the investigators could possibly have come to the conclusion that they did… until the inevitable twist is revealed and then all falls beautifully into place.

In some ways I did feel the huge plot twist was a bit unfair, as it relied solely on the original Manga’s audience making an assumption that someone new to Death Note wouldn’t necessarily have made, but it did turn what had been a slightly frustrating read into something really worth telling your friends about.

There is just one little geeky thing that does really bother me though. A huge part of the original Manga’s story was the rules that governed the supernatural Death Note world, they were misleadingly simple and very specific. These rules were at the centre of most of the plot twists and were a big part in what kept the reader trying to work out what was coming next… and this is where my geeky little problem with Another Note is.

The only real crossover with the original series (other than it shares some characters) is that the murderer was by chance born with the Shinigami Eyes, which normally would only be available to a Death Note’s owner in exchange for half their remaining life span. Through Shinigami Eyes you can see people’s true name and their remaining life span, unless they own a Death Note too, something that Rem tells Misa is due to a Death Note owing human becoming closer to a Shinigami and to prevent them from going mad with the knowledge of their own remaining life span. Misa later uses this rule to her advantage to spot another Death Note owner in a crowd, as he was the only one who’s life span wasn’t visible to her. In Another Note the murder’s attempted suicide fails because, essentially, it isn’t his time to die and the story implies that had he been able to see his own remaining life span he would have known that… but technically as he didn’t own a Death Note his life span should have remained visible to humans with Shinigami Eyes, including him self! A small thing… but it really bugged the hell out of me. I mentioned it was really geeky, right?

All in all the book is well worth reading for all Death Note fans - many of whom I’m sure might disagree with my interoperation of Shinigami Eyes rules!