title: Receiving multiple HTTP requests from Firefox 12.0 on an App Engine handler
body: <p>I'm experiencing some bizarre behavior with Firefox 12.0 and a Python handler on GAE.</p>
  
  <p>When I request this handler in Firefox, it runs 3 times -- but ONLY when it's returning a GIF.</p>
  
  <p>I'm currently hacking around it by setting a memcache entry based on the handler's querystring. I'm hoping this will prevent duplicate db.put()s of the same info.</p>
  
  <p>Here's a working URL: <a href="http://test-o-tron.appspot.com" rel="nofollow">http://test-o-tron.appspot.com</a> -- note you can change these querystring parameters:</p>
  
  <ul>
  <li>format ("gif" or "html")</li>
  <li>hack ("true" or "false")</li>
  <li>mkey_suffix (string used in memcache keys for easily resetting the counter)</li>
  </ul>
  
  <p>Here's the code:</p>
  
  <pre><code>from google.appengine.api import urlfetch, memcache
  from google.appengine.ext import db
  import webapp2, random
  
  class MainHandler(webapp2.RequestHandler):
      def get(self):
  
          #If user doesn't have an mkey_suffix, make one
          if self.request.get("mkey_suffix") == "":
              self.redirect("/?format=gif&amp;hack=false&amp;mkey_suffix=" + 
                            self.request.remote_addr + 
                            "." + str(random.randint(0, 1000)))
  
  
          OUTPUT_GIF = self.request.get("format") == "gif"
          USE_HACK = self.request.get("hack") == "true"
  
          #Memcache keys
          mkey_suffix = self.request.get("mkey_suffix")
          mkey_log = "log" + mkey_suffix
          mkey_hack = "hack" + mkey_suffix
  
          #Count the number of requests using memcache
          if memcache.get(mkey_log) is None:
              memcache.set(mkey_log, 0, 60)
          counter = memcache.get(mkey_log)
  
          #Hack!! Only handle a given request ONCE every second
          if not USE_HACK or memcache.get(mkey_hack) is None:
              memcache.set(mkey_hack, True, time=1)
  
              #Show I'm not crazy
              counter += 1
              memcache.set(mkey_log, counter, 60)
  
          #Return counter value 
          if OUTPUT_GIF:
  
              self.response.headers["Content-Type"] = "image/gif"
              img_url = "http://placehold.it/{counter}x{counter}"
              img_url = img_url.format(counter=str(400 + counter))
              img_data = urlfetch.Fetch(img_url).content
              content = db.Blob(img_data)
  
  
          else:
  
              #Output HTML 
              self.response.headers["Content-Type"] = "text/html"
              content = "Counter == " + str(counter)
  
          self.response.out.write(content)
  
  
  app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
  </code></pre>