Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2012 21:21
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Sends an Apple Push Notification with Python
import socket, ssl, json, struct
import binascii
# device token returned when the iPhone application
# registers to receive alerts
deviceToken = '39cac56f 986a0e66 3c4fd4f4 68df5598 024d2ca3 8b9f307c 741c180e 9fc30c62'
thePayLoad = {
'aps': {
'alert':'Oh no! Server\'s Down!',
'test_data': { 'foo': 'bar' },
# Certificate issued by apple and converted to .pem format with openSSL
theCertfile = 'cert_ios_production.pem'
theHost = ( '', 2195 )
data = json.dumps( thePayLoad )
# Clear out spaces in the device token and convert to hex
deviceToken = deviceToken.replace(' ','')
# byteToken = bytes.fromhex( deviceToken )
byteToken = binascii.unhexlify(deviceToken)
theFormat = '!BH32sH%ds' % len(data)
theNotification = struct.pack( theFormat, 0, 32, byteToken, len(data), data )
#Just writing to a file for hex inspection
f = open('the_notification', 'w')
# Create our connection using the certfile saved locally
ssl_sock = ssl.wrap_socket( socket.socket( socket.AF_INET, socket.SOCK_STREAM ), certfile = theCertfile )
ssl_sock.connect( theHost )
# Write out our data
ssl_sock.write( theNotification )
# Close the connection -- apple would prefer that we keep
# a connection open and push data as needed.
Copy link

Traceback (most recent call last):
File "../", line 30, in
theNotification = struct.pack(theFormat, 0, 32, deviceToken, len(data), data)
struct.error: argument for 's' must be a bytes object

Copy link

This was written a long time ago with Python 2. It's not Python 3 compatible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment