【发布时间】:2015-02-07 09:13:59
【问题描述】:
我已将我的 iOS 应用程序配置为启用了 Apple 推送通知 (APN) 服务。我能够使用 PHP 和 Python 3 脚本向设备发送通知。我用本地机器在本地服务器上进行了测试。但是现在我需要用 Python2 编写脚本。
下面是我编写的脚本,当我运行它时,我什么也得不到。既没有通知我的设备,也没有命令行错误。
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()
我错过了什么?如何检查错误发生在哪里?
我使用 XAMPP 在 localhost 中运行 PHP 脚本,并在命令行中运行 Python 脚本,因为我无法使用 XAMPP 设置 Python,我已经发布了一个问题 here。
【问题讨论】:
标签: ios sockets python-2.7 localhost apple-push-notifications