Python APNs 不处理请求

发布于 2024-10-15 05:00:59 字数 2222 浏览 6 评论 0原文

我正在尝试实现一个服务器端脚本,用于将推送通知发送到苹果推送通知服务器。我创建了 ssl 连接,发送了有效负载 - 但无法从 APN 获得响应。这是我的代码:

import socket, ssl, pprint, struct, time, binascii

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# require a certificate from the server
ssl_sock = ssl.wrap_socket( s,
                            keyfile="/Users/Jeff/Desktop/pickmeup-key2-noenc.pem",
                            certfile="/Users/Jeff/Desktop/pickmeup-cert2.pem",
                            server_side=False,
                            do_handshake_on_connect=True,
                            cert_reqs=ssl.CERT_REQUIRED,
                            ca_certs="/Users/Jeff/Desktop/entrustrootcert.pem",)
                            #ciphers="ALL")
ssl_sock.connect(('gateway.sandbox.push.apple.com', 2195))

print repr(ssl_sock.getpeername())
print ssl_sock.cipher()
print pprint.pformat(ssl_sock.getpeercert())

command = '\x00'
identifier = 1987
expiry = time.time()
deviceToken = "9858d81caa236a86cc67d01e1a07ba1df0982178dd7c95aae115d033b93cb3f5"
alert = "This is a test message"
sound = "UILocalNotificationDefaultSoundName"
payload = "{\"aps\":{\"alert\":\"%s\",\"sound\":\"%s\"}}" %(alert, sound)

packetFormat = "!cIIH%dsH%ds" %(32, len(payload))

packet = struct.pack(packetFormat, 
                    command, 
                    identifier,
                    int(expiry),
                    32, 
                    binascii.unhexlify(deviceToken), 
                    len(payload), 
                    payload)
nBytesWritten = ssl_sock.write(packet)
print "nBytesWritten = %d" %(nBytesWritten)

data = ssl_sock.read(1024)
print len(data)

ssl_sock.close()

运行此脚本,我生成以下输出:

('17.149.34.132', 2195)
('AES256-SHA', 'TLSv1/SSLv3', 256)
{'notAfter': 'May 31 00:04:27 2012 GMT',
 'subject': ((('countryName', u'US'),),
             (('stateOrProvinceName', u'California'),),
             (('localityName', u'Cupertino'),),
             (('organizationName', u'Apple Inc'),),
             (('organizationalUnitName', u'Internet Services'),),
             (('commonName', u'gateway.sandbox.push.apple.com'),))}
nBytesWritten = 133
0

关于可能出现问题的任何想法? (我正在发送增强的推送通知,因此我期待苹果推送通知服务器的响应)

I'm trying to implement a server side script for sending push notifications to apple push notification server. I create the ssl connection, I send the payload - but am unable to get a response from the APNs. Here is my code:

import socket, ssl, pprint, struct, time, binascii

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# require a certificate from the server
ssl_sock = ssl.wrap_socket( s,
                            keyfile="/Users/Jeff/Desktop/pickmeup-key2-noenc.pem",
                            certfile="/Users/Jeff/Desktop/pickmeup-cert2.pem",
                            server_side=False,
                            do_handshake_on_connect=True,
                            cert_reqs=ssl.CERT_REQUIRED,
                            ca_certs="/Users/Jeff/Desktop/entrustrootcert.pem",)
                            #ciphers="ALL")
ssl_sock.connect(('gateway.sandbox.push.apple.com', 2195))

print repr(ssl_sock.getpeername())
print ssl_sock.cipher()
print pprint.pformat(ssl_sock.getpeercert())

command = '\x00'
identifier = 1987
expiry = time.time()
deviceToken = "9858d81caa236a86cc67d01e1a07ba1df0982178dd7c95aae115d033b93cb3f5"
alert = "This is a test message"
sound = "UILocalNotificationDefaultSoundName"
payload = "{\"aps\":{\"alert\":\"%s\",\"sound\":\"%s\"}}" %(alert, sound)

packetFormat = "!cIIH%dsH%ds" %(32, len(payload))

packet = struct.pack(packetFormat, 
                    command, 
                    identifier,
                    int(expiry),
                    32, 
                    binascii.unhexlify(deviceToken), 
                    len(payload), 
                    payload)
nBytesWritten = ssl_sock.write(packet)
print "nBytesWritten = %d" %(nBytesWritten)

data = ssl_sock.read(1024)
print len(data)

ssl_sock.close()

Running this script, I generate the following output:

('17.149.34.132', 2195)
('AES256-SHA', 'TLSv1/SSLv3', 256)
{'notAfter': 'May 31 00:04:27 2012 GMT',
 'subject': ((('countryName', u'US'),),
             (('stateOrProvinceName', u'California'),),
             (('localityName', u'Cupertino'),),
             (('organizationName', u'Apple Inc'),),
             (('organizationalUnitName', u'Internet Services'),),
             (('commonName', u'gateway.sandbox.push.apple.com'),))}
nBytesWritten = 133
0

Any ideas on what might be going wrong? (I am sending enhanced push notifications so I am expecting a response from apple push notification server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别念他 2024-10-22 05:00:59

需要注意的关键是 read() 没有返回任何数据。在 Python 中,read() 应该阻塞,直到数据可用或连接关闭。 Apple 正在关闭您的连接。

为什么?嗯,可能是因为您发送了格式错误的请求。 command=0 是普通的推送通知; command=1 得到增强。 big-endian 1987 将被解释为 0 字节设备令牌和 1987 字节有效负载,这两者都无效。

(FWIW,我会使用 B 而不是 c 作为命令 ID;它似乎更有意义。)

The key thing to note is that read() is returning no data. In Python, read() is supposed to block until data is available or the connection closes. Apple is closing your connection.

Why? Well, probably because you sent a malformed request. command=0 is a normal push notification; command=1 is enhanced. The big-endian 1987 will be interpreted as a 0-byte device token and a 1987-byte payload, neither of which are valid.

(And FWIW, I'd use B instead of c for the command ID; it seems to make more sense.)

极致的悲 2024-10-22 05:00:59

您可以考虑 https://github.com/djacobs/PyAPNs 它包含许多有用的功能,包括:

  • 错误处理
  • 支持增强的消息格式和自动重发消息,这些消息在错误响应之前发送
  • 非阻塞 ssl 套接字连接,具有出色的性能

you may consider https://github.com/djacobs/PyAPNs that wrapped lot of useful features, including:

  • error handling
  • support enhanced message format and auto resend messages which are sent before error response
  • non-blocking ssl socket connection with great performance
命比纸薄 2024-10-22 05:00:59

Apple Push 通知服务器不给出响应,它是一种单向二进制套接字。
您可以尝试 apns-python-wrapper 或 < a href="http://pypi.python.org/pypi/apns/1.1" rel="nofollow">apns

Apple Push notification server doesn't give a response, it's a one-way binary socket.
Rather than rolling your own solution you could try apns-python-wrapper or apns

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文