Apple Push Notification (APN) Python 2 script server support on iOS
I have configured an iOS app with Apple Push Notification (APN) service enabled. I was able to send notifications to devices with PHP and Python 3 scripts. I tested on both local server and local machine. But now I need to write a script in Python2.
Below is the script I wrote and when I run this I get nothing. Neither my device notification nor command line error.
import socket, ssl, json, struct import binascii import os deviceToken = 'my_device_tocken_without_spaces' thePayLoad = { 'aps': { 'alert':'My first push notification!', 'sound':'default' } } theCertfile = 'ck3_2.pem' theHost = ( 'gateway.sandbox.push.apple.com', 2195 ) data = json.dumps( thePayLoad ) theFormat = '!BH32sH%ds' % len(data) theNotification = struct.pack( theFormat, 0, 32, deviceToken, 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()
What did I miss? How can I check where the error occurred?
I ran a PHP script on localhost using XAMPP and I ran a Python script on the command line because I was unable to set up Python with XAMPP which has already posted the question here .
source to share
you can consider https://github.com/djacobs/PyAPNs , which includes many useful features, including:
- error processing
- support extended message format and automatically forward messages that are sent before error response
- non-blocking ssl socket connection with excellent performance
source to share
I think there is nothing wrong with your code. You can try adding the following lines afterssl_sock.connect( theHost )
print repr(ssl_sock.getpeername())
print ssl_sock.cipher()
print pprint.pformat(ssl_sock.getpeercert())
The ssl information of your connection will be printed.
Alternatively, you can create a sample server and change the connections in your client and test it on the server. http://carlo-hamalainen.net/blog/2013/1/24/python-ssl-socket-echo-test-with-self-signed-certificate
source to share