Skip to content

Instantly share code, notes, and snippets.

@MioGreen
Created February 6, 2015 09:30
Show Gist options
  • Save MioGreen/4573bf56e41e205c4851 to your computer and use it in GitHub Desktop.
Save MioGreen/4573bf56e41e205c4851 to your computer and use it in GitHub Desktop.
import json
import logging
import os
import socket
import ssl
import struct
import sys
import time
import uuid
APNS_HOST = 'gateway.push.apple.com'
APNS_PORT = 2195
APNS_ERRORS = {
1:'Processing error',
2:'Missing device token',
3:'missing topic',
4:'missing payload',
5:'invalid token size',
6:'invalid topic size',
7:'invalid payload size',
8:'invalid token',
255:'Unknown'
}
def push(cert_path, device):
if not os.path.exists(cert_path):
logging.error("Invalid certificate path: %s" % cert_path)
sys.exit(1)
device = device.decode('hex')
expiry = time.time() + 3600
try:
sock = ssl.wrap_socket(
socket.socket(socket.AF_INET, socket.SOCK_STREAM),
certfile=cert_path
)
sock.connect((APNS_HOST, APNS_PORT))
sock.settimeout(1)
except Exception as e:
logging.error("Failed to connect: %s" % e)
sys.exit(1)
logging.info("Connected to APNS\n")
for ident in range(1,4):
logging.info("Sending %d of 3 push notifications" % ident)
payload = json.dumps({
'aps': {
'alert': 'Push Test %d: %s' % (ident, str(uuid.uuid4())[:8])
}
})
items = [1, ident, expiry, 32, device, len(payload), payload]
try:
sent = sock.write(struct.pack('!BIIH32sH%ds'%len(payload), *items))
if sent:
logging.info("Message sent\n")
else:
logging.error("Unable to send message\n")
except socket.error as e:
logging.error("Socket write error: %s", e)
# If there was an error sending, we will get a response on socket
try:
response = sock.read(6)
command, status, failed_ident = struct.unpack('!BBI',response[:6])
logging.info("APNS Error: %s\n", APNS_ERRORS.get(status))
sys.exit(1)
except socket.timeout:
pass
except ssl.SSLError:
pass
sock.close()
if __name__ == '__main__':
if not sys.argv[2:]:
print "USAGE %s " % sys.argv[0]
sys.exit(1)
logging.basicConfig(level=logging.INFO)
push(sys.argv[1], sys.argv[2])
logging.info("Complete\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment