title: How to implement GCM HTTP server in Python while avoiding my server's IP being blacklisted by Google?
body: <p>I'm using Apache, WSGI (mod_wsgi) and Python, to implement a GCM HTTP server as describe in the Android Developer website:  </p>
  
  <p><a href="https://developer.android.com/google/gcm/server.html" rel="nofollow"> developer.android.com/google/gcm/server.html</a>  </p>
  
  <p>At first the code I've implemented on the server side to handle message sending to GCM was as the following:  </p>
  
  <pre><code>def send_to_gcm(data):
     url = 'https://android.googleapis.com/gcm/send'
     no = 1
     while True:
       try:
          request = Request(url=url, data=json.dumps(data))
          request.add_header('Authorization','key=AIzXXX')
          request.add_header('Content-Type', 'application/json')
          res = urlopen(request)
  
          if res.getcode() == 200: return
      except Exception: pass
  
      no += 1
  
      #Discard the message
      if no == 16: return 
  
      #Exponential backoff
      tts = randint(2**(no-1), (2**no) -1)
      sleep(tts)
  
  data = dict(registration_id=[regid], data=dict(mymessage=themessage))
  thread = Thread(target=send_to_gcm, args=(data,))
  thread.start()  
  </code></pre>
  
  <p>After a while (about a day) GCM stopped to accept the messages sent by the Server. So I started to dig here and there in the documentation of GCM and I found an important part of the specification I missed before:  </p>
  
  <p><a href="http://developer.android.com/google/gcm/http.html#response" rel="nofollow">developer.android.com/google/gcm/http.html#response</a>  </p>
  
  <p>"Honor the Retry-After header if it's included in the response from the GCM server. ... Senders that cause problems risk being blacklisted. ... Happens when the HTTP status code is between 501 and 599, or when the error field of a JSON object in the results array is Unavailable."</p>
  
  <p>So i patched my server code as follow:  </p>
  
  <pre><code>def send_to_gcm(data, environ):
     url = 'https://android.googleapis.com/gcm/send'
     no = 1
     while True:
        try:
           request = Request(url=url, data=json.dumps(data))
           request.add_header('Authorization','key=AIzXXX')
           request.add_header('Content-Type', 'application/json')
           res = urlopen(request)
  
           if res.getcode() == 200: return
        except HTTPError as error:
  
           if error.headers.has_key('Retry-After'):
              try: tts = int(response_headers['Retry-After'])
              except ValueError:
                 until = datetime.strptime(response_headers, '%a, %d %b %Y %H:%M:%S GMT')
                 diff = until - datetime.now()
                 tts = int(diff.total_seconds()) +1
              sleep(tts)
  
        no += 1
  
        #Discard the message
        if no == 16: return 
  
        #Exponential backoff
        tts = randint(2**(no-1), (2**no) -1)
        sleep(tts)
  </code></pre>
  
  <p>But actually it's likely my server has been blacklisted and for any request sent I receive a 401 status code and an "Unauthorized" error message. Here my questions:   </p>
  
  <p>Is there something wrong in my latest server implementation?<br>
  Will the static IP address of my server be unbanned and if yes when?</p>