如何实现GCM HTTP服务器在Python,同时避免我的服务器的IP被列入黑名单谷歌? [英] How to implement GCM HTTP server in Python while avoiding my server's IP being blacklisted by Google?

查看:273
本文介绍了如何实现GCM HTTP服务器在Python,同时避免我的服务器的IP被列入黑名单谷歌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Apache,WSGI(mod_wsgi的)和Python,实现GCM HTTP服务器在Android开发人员网站描述:

I'm using Apache, WSGI (mod_wsgi) and Python, to implement a GCM HTTP server as describe in the Android Developer website:

developer.android.com/google/gcm/server.html

起初,code,我在服务器端实现来处理消息发送到GCM是如下:

At first the code I've implemented on the server side to handle message sending to GCM was as the following:

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()  

在一段时间(大约一天)GCM停止接受服务器发送的消息。于是我开始在这里和那里挖GCM的文档中,我发现我以前错过了规范的一个重要的部分:

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:

developer.android.com/google/gcm/http.html#response

重Retry-After头,如果它包括从GCM服务器的响应。......导致问题的风险发件人被列入黑名单。......当HTTP状态code是501和599之间发生了,或者当结果数组中的JSON对象的错误字段不可用。

"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."

所以我修补我的服务器code如下:

So i patched my server code as follow:

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)

但实际上很可能我的服务器已经被列入黑名单,并提出任何要求送我收到一个401状态code和一个未经授权的错误消息。在这里,我的问题:

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:

有什么错在我最新的服务器实现?结果
请问我的服务器的静态IP地址已解除封锁,如果是什么时候?

Is there something wrong in my latest server implementation?
Will the static IP address of my server be unbanned and if yes when?

推荐答案

我在寻找相同主题。
该模块可以帮助你
https://github.com/geeknam/python-gcm

这篇关于如何实现GCM HTTP服务器在Python,同时避免我的服务器的IP被列入黑名单谷歌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆