Created
August 13, 2010 03:59
-
-
Save RTfXGaGeqSeItbMUgpFlekUs/522244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## ap.py | |
import json | |
import socket | |
import ssl | |
import struct | |
class ApplePush: | |
token = '' | |
host = 'gateway.push.apple.com' | |
path = '/' | |
port = 2195 | |
cert = 'pushy.pem' | |
def __init__(self, cert, token, sandbox=False): | |
self.cert = cert | |
self.token = token.replace(' ', '').decode('hex') | |
if sandbox: | |
self.host = 'gateway.sandbox.push.apple.com' | |
def send(self, msg, badge=0): | |
payload = { | |
'aps': { | |
'alert': msg, | |
'badge': badge, | |
} | |
} | |
data = json.dumps(payload) | |
header = '!BH32sH%ds' % len(data) | |
to_send = struct.pack(header, 0, 32, self.token, len(data), data) | |
sock = ssl.wrap_socket( | |
socket.socket(socket.AF_INET, socket.SOCK_STREAM), | |
certfile=self.cert) | |
sock.connect((self.host, self.port)) | |
sock.write(to_send) | |
sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment