2009-07-23 29 views
12

Tôi đang cố gửi thông báo đẩy tới iPhone bằng Python. Tôi đã xuất khẩu chứng chỉ và tư nhân chính vào một tập tin p12 từ truy cập keychain và sau đó chuyển đổi nó thành tập tin pem sử dụng lệnh sau:Kết nối với APNS dành cho iPhone Sử dụng Python

openssl pkcs12 -in cred.p12 -out cert.pem -nodes -clcerts 

Tôi đang sử dụng APNSWrapper bằng Python cho kết nối.

tôi chạy đoạn mã sau:

 
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() 

Và sau đó tôi nhận được thông báo lỗi:

 
ssl.SSLError: (1, '_ssl.c:485: 
error:14094416:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate unknown') 

bất cứ ai có thể giúp tôi ra về vấn đề này?

+0

Vì vậy, vấn đề là gì? Giấy chứng nhận hoặc dịch vụ? – JackLeo

Trả lời

8

tôi gần đây đã này sử dụng Django - http://leecutsco.de/2009/07/14/push-on-the-iphone/

Có thể hữu ích? Nó làm cho việc sử dụng không có thêm các thư viện khác ngoài những thư viện đã có sẵn trong Python. Sẽ không mất nhiều thời gian để giải nén phương thức send_message().

+1

Tôi đề nghị đưa nội dung vào câu trả lời thay vì liên kết. Cuối cùng, liên kết có thể bị lỗi thời, trong trường hợp đó, các liên kết khác có thể không được hưởng lợi từ câu trả lời. –

+0

Giống như @Walty dự đoán, liên kết đã bị hỏng. Liên kết mới là http://leecutsco.de/2009/07/14/push-on-the-iphone/ - sẽ rất tuyệt nếu nội dung được trả lời. – johndodo

2

Bạn đã xem gói Twisted chưa? Mã dưới đây được lấy từ here:

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 

APNS_SERVER_HOSTNAME = "<insert the push hostname from your iPhone developer portal>" 
APNS_SERVER_PORT = 2195 
APNS_SSL_CERTIFICATE_FILE = "<your ssl certificate.pem>" 
APNS_SSL_PRIVATE_KEY_FILE = "<your ssl private key.pem>" 

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 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 = "!cH32cH%dc" % len(payload) 
     command = 0 
     msg = struct.pack(fmt, command, deviceToken, 
          len(payload), payload) 
     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() 
+0

@coonj Bạn đã thử điều này với bất kỳ thành công nào chưa? Bạn có ví dụ về việc sử dụng mã để gửi tin nhắn không? Cảm ơn. – elioty

1

có một vài lỗi trong mã được đăng ban đầu, vì vậy, đây là phiên bản được sửa chữa phù hợp với tôi.

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() 
0

Tôi đã thử cả hai mã APNSWrapper và Lee Peckham và không thể làm cho nó hoạt động trong Snow Leopard với Python 2.6. Sau rất nhiều thử nghiệm và lỗi cuối cùng nó đã làm việc với pyOpenSSL.

Tôi đã làm một bài đăng với các chi tiết và đoạn mã here vì vậy tôi sẽ chỉ giới thiệu bạn ở đó.

1

Cố gắng cập nhật lên phiên bản APNSWrapper mới nhất (0.4). Có hỗ trợ xây dựng trong công cụ dòng lệnh openssl (openssl s_client) ngay bây giờ.

Các vấn đề liên quan