everyone,
I met a problem about APNs when I used the code below.
I have found many kinds of source code to achieve this service
import socket, ssl, json, struct import binascii
deviceToken = 'XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX'
thePayLoad = {
'aps': {
'alert':'Hello world',
'sound':'default',
'badge':42,
},
'test_data': { 'foo': 'bar' },
}
theCertfile = 'iphone_ck.pem'
theHost = ( 'gateway.sandbox.push.apple.com', 2195 )
data = json.dumps( thePayLoad )
deviceToken = deviceToken.replace(' ','')
byteToken = binascii.unhexlify(deviceToken)
theFormat = '!BH32sH%ds' % len(data) theNotification = struct.pack( theFormat, 0, 32,
byteToken, len(data), data )
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()
After I executed the code, and I got an error below.
Everytime when I tried to use PyAPNs on GitHub or APNWrapper on Google
I found that the error at last.
So, I decided to implement by myself.
Traceback (most recent call last):
File "testAPN.py", line 38, in <module>
ssl_sock.connect( theHost )
File "/usr/lib/python2.6/ssl.py", line 309, in connect
self.do_handshake()
File "/usr/lib/python2.6/ssl.py", line 293, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [Errno 1] _ssl.c:480: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3
alert handshake failure
Could anyone help me to figure out this error or give me some possible directions to
finish this function?
I think you should try to use APNSWrapper module for this task.
http://code.google.com/p/apns-python-wrapper/
It is simple and common use.