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

我正在使用Apache,WSGI(mod_wsgi)和Python来实现GCM HTTP服务器,如Android Developer网站所述:

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

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

首先,我在服务器端实现的代码处理向GCM发送消息的代码如下:

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

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:

过了一会儿(大约一天),GCM停止接受服务器发送的消息。所以我开始在GCM的文档中随处挖掘,我发现了之前错过的规范的一个重要部分:

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

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

“如果它包含在来自GCM服务器的响应中,请尊重Retry-After标头。...导致问题的发件人可能会被列入黑名单。...当HTTP状态代码介于501和599之间时,或者当错误字段出现时结果数组中的JSON对象不可用。“

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)

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:

但实际上我的服务器可能已被列入黑名单,并且对于发送的任何请求,我收到401状态代码和“未授权”错误消息。在这里我的问题:

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

我最新的服务器实现有什么问题吗?我的服务器的静态IP地址是否会被取消,如果是的话?

1 个解决方案

#1


2

I was searching for the same subject. This module may help you https://github.com/geeknam/python-gcm

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

更多相关文章

  1. 自动完成在VS代码和Python中的自动化对象
  2. 在生产中是否应该减少服务器代码?
  3. 支持c和python之间的跨语言(c)标记的代码编辑器
  4. 【小白自学笔记】【机器学习实战】【Python代码逐行理解】CH02
  5. 三个猜数字游戏代码(Python)
  6. 在混合的Bash-Python代码片段中,变量的双引号和单引号
  7. 不能使用python HTTPSConnection()连接到web服务器
  8. 八大经典排序算法基本思想及代码实现(插入排序,希尔排序,选择排序,
  9. 贝叶斯学习 -- matlab、python代码分析(3)

随机推荐

  1. 禁用Chrome严格的MIME类型检查
  2. JavaScript正则表达式定义字符集
  3. 用npm-run自动化任务
  4. “错误:路径必须是字符串”(v5.10.0)
  5. 放在和中的javascript语句,但是语句不在函
  6. 使用php Ajax在数据库中插入动态数据
  7. 打字稿:无法访问类属性[重复]
  8. Change ALL links, hrefs,urls with vani
  9. 1、突然对jQuery的心血来潮
  10. 常用验证JS代码基础及实例