-
-
Save henter/5906614 to your computer and use it in GitHub Desktop.
用python推送消息。 无密码验证。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket, ssl, json, struct | |
import binascii | |
# device token returned when the iPhone application | |
# registers to receive alerts | |
deviceToken = 'f435d683eb9d7e5680938c363ea6e38eba36a553e9b23ddd57f9a345c0da0a0e' | |
thePayLoad = { | |
'aps': { | |
'alert':'Oh no! Server\'s Down!', | |
'sound':'k1DiveAlarm.caf', | |
'badge':42, | |
}, | |
'test_data': { 'foo': 'bar' }, | |
} | |
# Certificate issued by apple and converted to .pem format with openSSL | |
#theCertfile = '/Users/henter/Downloads/weimi_rc.pem' | |
# cert file produced by openssl with pkey.p12 and cert.p12 | |
theCertfile = 'weimi_push_rc.pem' | |
# | |
theHost = ( 'gateway.push.apple.com', 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) | |
print("HELLOS") | |
print(byteToken) | |
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') | |
f.write(theNotification) | |
f.close() | |
# 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. | |
ssl_sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment