Python test APN certificates

4 篇文章 0 订阅

How do I Test my PEM Key?

 

PubNub Support

posted this on February 21, 2013 06:40

 

Test your PEM Key using the following command, which shouldhang if successful until you press enter:

 

openssl s_client -connectgateway.sandbox.push.apple.com:2195 -cert pnpush.pem -key pnpush.pem

 

The above tests the PEM Key in sandbox mode. For productionmode, use the following command:

 

openssl s_client -connect gateway.push.apple.com:2195 -certpnpush.pem -key pnpush.pem

 

You can also test your PEM Key by running the Python scriptbelow using the command:

 

python ./debug_push.py

 

debug_push.py

 

import json

import logging

import os

import socket

import ssl

import struct

import sys

import time

import uuid

 

APNS_HOST = 'gateway.sandbox.push.apple.com'

APNS_PORT = 2195

 

APNS_ERRORS = {

    1:'Processing error',

    2:'Missing device token',

    3:'missing topic',

    4:'missing payload',

    5:'invalid token size',

    6:'invalid topic size',

    7:'invalid payload size',

    8:'invalid token',

    255:'Unknown'

}

 

def push(cert_path, device):

    if not os.path.exists(cert_path):

        logging.error("Invalidcertificate path: %s" % cert_path)

        sys.exit(1)

 

    device = device.decode('hex')

    expiry = time.time() + 3600

 

    try:

        sock =ssl.wrap_socket(

           socket.socket(socket.AF_INET, socket.SOCK_STREAM),

           certfile=cert_path

        )

       sock.connect((APNS_HOST, APNS_PORT))

       sock.settimeout(1)

    except Exception as e:

       logging.error("Failed to connect: %s" % e)

        sys.exit(1)

 

    logging.info("Connected toAPNS\n")

 

    for ident in range(1,4):

       logging.info("Sending %d of 3 push notifications" % ident)

 

        payload =json.dumps({

           'aps': {

               'alert': 'Push Test %d: %s' % (ident, str(uuid.uuid4())[:8])

           }

        })

 

        items = [1,ident, expiry, 32, device, len(payload), payload]

 

        try:

           sent = sock.write(struct.pack('!BIIH32sH%ds'%len(payload), *items))

           if sent:

               logging.info("Message sent\n")

           else:

               logging.error("Unable to sendmessage\n")

        exceptsocket.error as e:

           logging.error("Socket write error: %s", e)

 

        # If there was anerror sending, we will get a response on socket

        try:

           response = sock.read(6)

           command, status, failed_ident = struct.unpack('!BBI',response[:6])

           logging.info("APNS Error: %s\n", APNS_ERRORS.get(status))

           sys.exit(1)

        exceptsocket.timeout:

           pass

        exceptssl.SSLError:

           pass

 

    sock.close()

 

if __name__ == '__main__':

    if not sys.argv[2:]:

        print "USAGE%s  " % sys.argv[0]

        sys.exit(1)

 

    logging.basicConfig(level=logging.INFO)

    push(sys.argv[1], sys.argv[2])

    logging.info("Complete\n")

 

 

Topic is closed for comments

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值