11.20 python - Google Cloud Messaging (GCM) HTTP connection server.

1.如何向app发送gcm推送?

https://developers.google.com/cloud-messaging/http
1.通过构造http请求,发送给https://gcm-http.googleapis.com/gcm/send
2.通过xmpp发送请求。

2.python如何发送请求?

http://www.findspace.name/easycoding/1137
http://www.01happy.com/python-httplib-get-and-post/

#Code1
#!/usr/bin/env python
#coding=utf8

import httplib, urllib

httpClient = None
try:
    params = urllib.urlencode({'name': 'tom', 'age': 22})
    headers = {"Content-type": "application/x-www-form-urlencoded"
                    , "Accept": "text/plain"}

    httpClient = httplib.HTTPConnection("localhost", 80, timeout=30)
    httpClient.request("POST", "/test.php", params, headers)

    response = httpClient.getresponse()
    print response.status
    print response.reason
    print response.read()
    print response.getheaders() #获取头信息
except Exception, e:
    print e
finally:
    if httpClient:
        httpClient.close()
#Code2
import urllib,urllib2

url = 'http://www.super-ping.com/ping.php?node='+node+'&ping=www.google.com'
        headers = { 'Host':'www.super-ping.com',
                    'Connection':'keep-alive',
                    'Cache-Control':'max-age=0',
                    'Accept': 'text/html, */*; q=0.01',
                    'X-Requested-With': 'XMLHttpRequest',
                    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
                    'DNT':'1',
                    'Referer': 'http://www.super-ping.com/?ping=www.google.com&locale=sc',
                    'Accept-Encoding': 'gzip, deflate, sdch',
                    'Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6'
                    }
        data = None
        req = urllib2.Request(url, data, headers)
        response = urllib2.urlopen(req)
        compressedData = response.read()

3.发送推送

__author__ = 'george.yang'

#!/usr/bin/env python
#coding=utf8

import urllib,urllib2
import json

serverKey = 'AI?a?yB?WUe8?a69?liy??q??5????3?dq?FlQ?'
regId = 'APA91bG4?yViVl79O?rdB_?YnbA5UP?M_V?3a-5p3?t42??6a?z?-nM??SS??z?O??OIN-sAB?D?oVa?lq6d4Ym81Nt?NU3?GaA???1-Palvu?nnB???G??p???9???N3?-?3?Y?r?lL'

url = 'https://gcm-http.googleapis.com/gcm/send'
headers = {"Content-type": "application/json",
            "Authorization":"key="+serverKey
}

#send to one
# data = {'to': regId, 'data': {'name':'george'}}
#send to many
data = {'registration_ids': [regId], 'data': {'name':'george'}}
req = urllib2.Request(url, json.dumps(data), headers)

response = urllib2.urlopen(req)
print  response
compressedData = response.read()
print compressedData

data 的选择:
“Content-type”: “application/json”时使用: json.dumps(data)
“Content-type”: “application/x-www-form-urlencoded format”时使用:data = urllib.urlencode(data)
http://stackoverflow.com/questions/3893292/python-unhashable-type-error-in-urllib2

转载请注明出处:http://blog.csdn.net/u010499721

如果发送成功,打印:

<addinfourl at 45211720L whose fp = <socket._fileobject object at 0x0000000002AE3318>>
{"multicast_id":???16881?7?99971?8?,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1??79?9??0505061%9b36?1b2?9?d7?cd"}]}

如果http请求数据不符合,返回400

如果regiestId不存在,发送失败:
{“multicast_id”:???16881?7?99971?8?,”success”:0,”failure”:1,”canonical_ids”:0,”results”:[{“error”:”InvalidRegistration”}]}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值