使用Python连接到iPhone的APNS
我正在尝试使用Python向iPhone发送推送通知。我已从keychain access将我的证书和私钥导出到p12文件中,然后使用以下命令将其转换为pem文件:使用Python连接到iPhone的APNS,iphone,python,ssl,push-notification,Iphone,Python,Ssl,Push Notification,我正在尝试使用Python向iPhone发送推送通知。我已从keychain access将我的证书和私钥导出到p12文件中,然后使用以下命令将其转换为pem文件: openssl pkcs12 -in cred.p12 -out cert.pem -nodes -clcerts 我正在用Python进行连接 我运行以下代码: deviceToken = 'Qun\xaa\xd ... c0\x9c\xf6\xca' # create wrapper wrapper = APNSNotif
openssl pkcs12 -in cred.p12 -out cert.pem -nodes -clcerts
我正在用Python进行连接
我运行以下代码:
deviceToken = 'Qun\xaa\xd ... c0\x9c\xf6\xca'
# create wrapper
wrapper = APNSNotificationWrapper('/path/to/cert/cert.pem', True)
# create message
message = APNSNotification()
message.token(deviceToken)
message.badge(5)
# add message to tuple and send it to APNS server
wrapper.append(message)
wrapper.notify()
deviceToken='Qun\xaa\xd。。。c0\x9c\xf6\xca'
#创建包装器
wrapper=APNSNotificationWrapper('/path/to/cert/cert.pem',True)
#创建消息
message=apnsonification()
message.token(deviceToken)
信息.徽章(5)
#将消息添加到元组并将其发送到APNS服务器
append(消息)
包装器
然后我得到了错误信息:
ssl.SSLError: (1, '_ssl.c:485:
error:14094416:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate unknown')
ssl.SSLError:(1',_ssl.c:485:
错误:14094416:SSL例程:SSL3\u读取字节:sslv3警报证书未知')
有人能帮我吗 你考虑过这个包裹了吗?以下代码取自:
来自结构导入包
从OpenSSL导入SSL
从twisted.internet导入
从twisted.internet.protocol导入客户端工厂,协议
从twisted.internet.ssl导入ClientContextFactory
APNS_服务器_主机名=“”
APNS_服务器_端口=2195
APNS_SSL_证书_文件=“”
APNS_SSL_PRIVATE_KEY_FILE=“”
APNSClientContextFactory类(ClientContextFactory):
定义初始化(自):
self.ctx=SSL.Context(SSL.SSLv3_方法)
self.ctx.use_certificate_文件(APNS_SSL_certificate_文件)
self.ctx.use_privatekey_文件(APNS_SSL_PRIVATE_KEY_文件)
def getContext(self):
return self.ctx
APNSProtocol类(协议):
def sendMessage(自身、设备停止、有效负载):
#通知消息是按网络顺序排列的二进制消息
#使用以下格式:
#
fmt=“!cH32cH%dc”%len(有效负载)
命令=0
msg=struct.pack(fmt,command,deviceToken,
len(有效载荷),有效载荷)
self.transport.write(msg)
类别APNSClientFactory(客户端工厂):
def构建协议(自身、地址):
打印“已连接到APNS服务器%s:%u”%(地址主机,地址端口)
返回APNSProtocol()
def clientConnectionLost(自身、连接器、原因):
打印“丢失连接。原因:%s”%s原因
def客户端连接失败(自身、连接器、原因):
打印“连接失败。原因:%s”%s原因
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
reactor.connectSSL(APNS\u服务器\u主机名,
APNS_服务器_端口,
APNSClientFactory(),
APNSClientContextFactory())
反应堆运行()
我最近使用Django-
可能有用吗?除了Python中已经包含的库之外,它没有使用其他库。提取send_message()方法不需要太多时间。最初发布的代码中有一些错误,因此这里有一个适合我的更正版本
from struct import pack
from OpenSSL import SSL
from twisted.internet import reactor
from twisted.internet.protocol import ClientFactory, Protocol
from twisted.internet.ssl import ClientContextFactory
import binascii
import struct
APNS_SERVER_HOSTNAME = "gateway.sandbox.push.apple.com"
APNS_SERVER_PORT = 2195
APNS_SSL_CERTIFICATE_FILE = "<your ssl certificate.pem>"
APNS_SSL_PRIVATE_KEY_FILE = "<your ssl private key.pem>"
DEVICE_TOKEN = "<hexlified device token>"
MESSAGE = '{"aps":{"alert":"twisted test"}}'
class APNSClientContextFactory(ClientContextFactory):
def __init__(self):
self.ctx = SSL.Context(SSL.SSLv3_METHOD)
self.ctx.use_certificate_file(APNS_SSL_CERTIFICATE_FILE)
self.ctx.use_privatekey_file(APNS_SSL_PRIVATE_KEY_FILE)
def getContext(self):
return self.ctx
class APNSProtocol(Protocol):
def connectionMade(self):
print "connection made"
self.sendMessage(binascii.unhexlify(DEVICE_TOKEN), MESSAGE)
self.transport.loseConnection()
def sendMessage(self, deviceToken, payload):
# notification messages are binary messages in network order
# using the following format:
# <1 byte command> <2 bytes length><token> <2 bytes length><payload>
fmt = "!cH32sH%ds" % len(payload)
command = '\x00'
msg = struct.pack(fmt, command, 32, deviceToken,
len(payload), payload)
print "%s: %s" %(binascii.hexlify(deviceToken), binascii.hexlify(msg))
self.transport.write(msg)
class APNSClientFactory(ClientFactory):
def buildProtocol(self, addr):
print "Connected to APNS Server %s:%u" % (addr.host, addr.port)
return APNSProtocol()
def clientConnectionLost(self, connector, reason):
print "Lost connection. Reason: %s" % reason
def clientConnectionFailed(self, connector, reason):
print "Connection failed. Reason: %s" % reason
if __name__ == '__main__':
reactor.connectSSL(APNS_SERVER_HOSTNAME,
APNS_SERVER_PORT,
APNSClientFactory(),
APNSClientContextFactory())
reactor.run()
来自结构导入包
从OpenSSL导入SSL
从twisted.internet导入
从twisted.internet.protocol导入客户端工厂,协议
从twisted.internet.ssl导入ClientContextFactory
导入binascii
导入结构
APNS\u SERVER\u HOSTNAME=“gateway.sandbox.push.apple.com”
APNS_服务器_端口=2195
APNS_SSL_证书_文件=“”
APNS_SSL_PRIVATE_KEY_FILE=“”
设备\u令牌=“”
消息='{“aps”:{“警报”:“扭曲测试”}'
APNSClientContextFactory类(ClientContextFactory):
定义初始化(自):
self.ctx=SSL.Context(SSL.SSLv3_方法)
self.ctx.use_certificate_文件(APNS_SSL_certificate_文件)
self.ctx.use_privatekey_文件(APNS_SSL_PRIVATE_KEY_文件)
def getContext(self):
return self.ctx
APNSProtocol类(协议):
def connectionMade(自):
打印“已建立连接”
self.sendMessage(binascii.unhexlify(设备令牌),MESSAGE)
self.transport.loseConnection()
def sendMessage(自身、设备停止、有效负载):
#通知消息是按网络顺序排列的二进制消息
#使用以下格式:
#
fmt=“!cH32sH%ds”%len(有效负载)
命令='\x00'
msg=struct.pack(fmt,command,32,deviceToken,
len(有效载荷),有效载荷)
打印“%s:%s”%(binascii.hexlify(deviceToken),binascii.hexlify(msg))
self.transport.write(msg)
类别APNSClientFactory(客户端工厂):
def构建协议(自身、地址):
打印“已连接到APNS服务器%s:%u”%(地址主机,地址端口)
返回APNSProtocol()
def clientConnectionLost(自身、连接器、原因):
打印“丢失连接。原因:%s”%s原因
def客户端连接失败(自身、连接器、原因):
打印“连接失败。原因:%s”%s原因
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
reactor.connectSSL(APNS\u服务器\u主机名,
APNS_服务器_端口,
APNSClientFactory(),
APNSClientContextFactory())
反应堆运行()
我尝试了apnswraper
和Lee Peckham的代码,但无法在使用Python 2.6的Snow Leopard下工作。经过大量的尝试和错误,它最终使用了pyOpenSSL
我已经发表了一篇文章,详细介绍了一些细节和代码片段,所以我将在这里向您介绍。尝试更新到最新的APNSWrapper版本(0.4)。现在有内置的openssl命令行工具(openssl s_客户端)支持。@coonj您是否尝试过成功?您是否有使用该代码发送消息的示例?谢谢。那有什么问题吗?证书还是服务?我建议把内容放在答案上而不是链接上。最终链接可能会过时,在这种情况下,其他人可能不会从答案中受益。正如@Walty所预测的,链接被破坏了。新的链接是-如果内容是正确的,那就太好了。如果您重新创建URL,我将不胜感激,它会将我指向未找到的页面。
from struct import pack
from OpenSSL import SSL
from twisted.internet import reactor
from twisted.internet.protocol import ClientFactory, Protocol
from twisted.internet.ssl import ClientContextFactory
import binascii
import struct
APNS_SERVER_HOSTNAME = "gateway.sandbox.push.apple.com"
APNS_SERVER_PORT = 2195
APNS_SSL_CERTIFICATE_FILE = "<your ssl certificate.pem>"
APNS_SSL_PRIVATE_KEY_FILE = "<your ssl private key.pem>"
DEVICE_TOKEN = "<hexlified device token>"
MESSAGE = '{"aps":{"alert":"twisted test"}}'
class APNSClientContextFactory(ClientContextFactory):
def __init__(self):
self.ctx = SSL.Context(SSL.SSLv3_METHOD)
self.ctx.use_certificate_file(APNS_SSL_CERTIFICATE_FILE)
self.ctx.use_privatekey_file(APNS_SSL_PRIVATE_KEY_FILE)
def getContext(self):
return self.ctx
class APNSProtocol(Protocol):
def connectionMade(self):
print "connection made"
self.sendMessage(binascii.unhexlify(DEVICE_TOKEN), MESSAGE)
self.transport.loseConnection()
def sendMessage(self, deviceToken, payload):
# notification messages are binary messages in network order
# using the following format:
# <1 byte command> <2 bytes length><token> <2 bytes length><payload>
fmt = "!cH32sH%ds" % len(payload)
command = '\x00'
msg = struct.pack(fmt, command, 32, deviceToken,
len(payload), payload)
print "%s: %s" %(binascii.hexlify(deviceToken), binascii.hexlify(msg))
self.transport.write(msg)
class APNSClientFactory(ClientFactory):
def buildProtocol(self, addr):
print "Connected to APNS Server %s:%u" % (addr.host, addr.port)
return APNSProtocol()
def clientConnectionLost(self, connector, reason):
print "Lost connection. Reason: %s" % reason
def clientConnectionFailed(self, connector, reason):
print "Connection failed. Reason: %s" % reason
if __name__ == '__main__':
reactor.connectSSL(APNS_SERVER_HOSTNAME,
APNS_SERVER_PORT,
APNSClientFactory(),
APNSClientContextFactory())
reactor.run()