Exemple #1
0
    def __init__(self,
                 app=None,
                 address="push_sandbox",
                 failure_callback=None,
                 **cert_params):
        """
        :param app: The app to init
        :param address: The APNS address as understood by
            :py:meth:`apnsclient.apns.Session.get_connection`
        :param failure_callback: Called when calls to
            :py:meth:`apnsclient.apns.APNs.send` returns failures
        :param cert_params: Parameters to initialize
            :py:class:`apnsclient.apns.Certificate`
        """

        self.session = Session()

        self.failed_callback = failure_callback

        self._cert_params = cert_params
        self._address = address
        self._certificate = None

        if app is not None:
            self.session = self.init_app(app)
Exemple #2
0
    def test_send(self):
        # success, retry + include-failed, don't-retry + include-failed
        backend = DummyBackend(push=(None, 1, 3))
        session = Session(pool=backend)

        msg = Message(["0123456789ABCDEF", "FEDCBA9876543210"], alert="my alert", badge=10, content_available=1, my_extra=15)
        push_con = session.get_connection("push_production", cert_string="certificate")
        srv = APNs(push_con)
        res = srv.send(msg)
        self.assertEqual(len(res.failed), 0)
        self.assertEqual(len(res.errors), 0)
        self.assertFalse(res.needs_retry())

        push_con2 = session.get_connection("push_production", cert_string="certificate")
        srv = APNs(push_con2)
        self.assertEqual(session.pool.push_result_pos, 0)
        session.pool.push_result_pos += 1
        res = srv.send(msg)
        self.assertEqual(len(res.failed), 0)
        self.assertEqual(len(res.errors), 1)
        self.assertTrue(res.needs_retry())

        # indeed, we have used the cache
        self.assertEqual(session.pool.new_connections, 1)

        push_con = session.new_connection("push_production", cert_string="certificate")
        srv = APNs(push_con)
        res = srv.send(msg)
        self.assertEqual(len(res.failed), 0)
        self.assertEqual(len(res.errors), 1)
        self.assertFalse(res.needs_retry())

        # indeed, new connection, we haven't used the cache
        self.assertEqual(session.pool.new_connections, 2)
Exemple #3
0
 def connect_apns_server(cls, sandbox, p12, secret, timestamp):
     pub_key, priv_key = cls.gen_pem(p12, secret)
     session = Session(read_tail_timeout=1)
     address = 'push_sandbox' if sandbox else 'push_production'
     conn = session.get_connection(address,
                                   cert_string=pub_key,
                                   key_string=priv_key)
     apns = APNs(conn)
     return apns
Exemple #4
0
 def __init__(self, push_connection=False):
     if not APNSProvider.session:
         APNSProvider.session = Session()
     if settings.DEBUG:
         feedback_property = "feedback_sandbox"
         push_property = "push_sandbox"
     else:
         feedback_property = "feedback_production"
         push_property = "push_production"
     self.connection = APNSProvider.session.new_connection(feedback_property, cert_file=settings.APNS_CERT) \
         if not push_connection else APNSProvider.session.get_connection(push_property,
                                                                         cert_file=settings.APNS_CERT)
Exemple #5
0
    def connect_apns(cls, appid):
        logging.debug("connecting apns")
        p12, secret, timestamp = cls.get_p12(appid)
        if not p12:
            return None

        if sandbox:
            pem_file = "/tmp/app_%s_sandbox_%s.pem" % (appid, timestamp)
            address = 'push_sandbox'
        else:
            pem_file = "/tmp/app_%s_%s.pem" % (appid, timestamp)
            address = 'push_production'

        if not os.path.isfile(pem_file):
            pem = cls.gen_pem(p12, secret)
            f = open(pem_file, "wb")
            f.write(pem)
            f.close()

        session = Session(read_tail_timeout=1)

        conn = session.get_connection(address, cert_file=pem_file)
        apns = APNs(conn)
        return apns
Exemple #6
0
from __future__ import absolute_import

from zerver.models import PushDeviceToken
from zerver.lib.timestamp import timestamp_to_datetime
from zerver.decorator import statsd_increment

from apnsclient import Session, Message, APNs
import gcmclient

from django.conf import settings

import base64, binascii, logging, os

# Maintain a long-lived Session object to avoid having to re-SSL-handshake
# for each request
session = Session()
connection = None
if settings.APNS_CERT_FILE is not None and os.path.exists(
        settings.APNS_CERT_FILE):
    connection = session.get_connection(settings.APNS_SANDBOX,
                                        cert_file=settings.APNS_CERT_FILE)

# We maintain an additional APNS connection for pushing to Zulip apps that have been signed
# by the Dropbox certs (and have an app id of com.dropbox.zulip)
dbx_session = Session()
dbx_connection = None
if settings.DBX_APNS_CERT_FILE is not None and os.path.exists(
        settings.DBX_APNS_CERT_FILE):
    dbx_connection = session.get_connection(
        settings.APNS_SANDBOX, cert_file=settings.DBX_APNS_CERT_FILE)
Exemple #7
0
 def setUp(self):
     self.session = Session(pool="apnsclient.backends.stdio")
Exemple #8
0
#

from apnsclient import (
        Session,
        Message,
        APNs
)
from pprint import pprint

# device token and private key's passwd
deviceToken = '097271a3744d951fe233729b649b0d5bcdfbf7e0c8c10e11fa99d02e5cfa87ac';
invalid_deviceToken = '097271a3744d951fe233729b649b0d5bcdfbf7e0c8c10e11fa99d02e5cfa87ed';
passphrase = '1234';

# 使用session对象来创建一个连接池
session = Session()
conn = session.get_connection("push_sandbox", cert_file="apns.pem", passphrase=passphrase)

# 发送推送和得到反馈
tokenList = []
tokenList.append(deviceToken)
#tokenList.append(invalid_deviceToken)
msg = Message(tokenList, alert="use python send", badge=10)

# send message
service = APNs(conn)
res = service.send(msg)

pprint(vars(res))
print res.message.__dict__
Exemple #9
0
 def test_feedback(self):
     backend = DummyBackend(feedback=5)
     session = Session(pool=backend)
     feed_con = session.new_connection("feedback_production", cert_string="certificate")
     srv = APNs(feed_con)
     self.assertEqual(len(list(srv.feedback())), 5)