Skip to content

Instantly share code, notes, and snippets.

@holtwick
Created December 16, 2009 08:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holtwick/257679 to your computer and use it in GitHub Desktop.
Save holtwick/257679 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.4
# Needs python 2.6 - for 2.5 replace ssl and json (with simplejson)
print "Content-Type: text/plain"
print
import socket, ssl, struct
# import simplejson as json
import cgi
form = cgi.FieldStorage()
def main():
# Export your "Apple Development Push Services" certificate from your Keychain (important: select it from the "My Certificates" category in the sidebar).
# Export the certificate as a "Personal Information Exchange (.p12) file. You shouldn't give the exported file a password (well you could but YMMV).
# Convert the .p12 file you just exported into a pem file:
# openssl pkcs12 -in certificates.p12 -out certfile.pem -nodes -clcerts
# Replace the device token with YOUR applications device token.
# For testing don't forget to quit the app (you wont see the push notification dialogs while the app is running).
isProduction = str(form.getfirst("production", "0")) == '1'
deviceToken = form.getfirst("devtoken", "")
thePayLoad = form.getfirst("payload", "")
if not deviceToken:
print "Device Token is missing!"
return
if not thePayLoad:
print "Payload is missing!"
return
if isProduction:
theCertfile = 'prodcertfile.pem'
theHost = ('gateway.push.apple.com', 2195)
print "PROD"
else:
theCertfile = 'devcertfile.pem'
theHost = ('gateway.sandbox.push.apple.com', 2195)
print "DEV"
deviceToken = deviceToken.replace(' ','').decode('hex')
theFormat = '!BH32sH%ds' % len(thePayLoad)
theNotification = struct.pack(theFormat, 0, 32, deviceToken, len(thePayLoad), thePayLoad)
ssl_sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM), certfile=theCertfile)
ssl_sock.connect(theHost)
ssl_sock.write(theNotification)
ssl_sock.close()
print 'OK'
if __name__=="__main__":
try:
main()
except Exception, e:
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment