
Untitled
By: a guest on
Jun 27th, 2012 | syntax:
Python | size: 2.07 KB | hits: 14 | expires: Never
from django.conf import settings
from socket import socket
import ssl
class IosPushServer():
def _connect_socket(self):
raise NotImplementedError( "Should have implemented this" )
def _write_apn_to_socket(self, msg, existing_socket=None):
raise NotImplementedError( "Should have implemented this" )
def _get_msg_list(self):
raise NotImplementedError( "Should have implemented this" )
class Meta:
abstract = True
class IosPushDummyServer(IosPushServer):
c = []
def _connect_socket(self):
return []
def _write_apn_to_socket(self, msg, existing_socket=None):
if existing_socket:
existing_socket.append(msg)
else:
self.c = []
self.c.append(msg)
def _get_msg_list(self):
return self.c
class IosPushLiveServer(IosPushServer):
def __init__(self):
self._apn_cert_path=getattr(settings, 'APN_LIVE_CERT_PATH', None)
self._apn_host=getattr(settings, 'APN_LIVE_HOST', None)
if not self._apn_cert_path or not self._apn_host:
raise Exception("APN_LIVE_CERT_PATH and/or APN_LIVE_HOST not specified.")
def _connect_socket(self):
s = socket()
c = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_SSLv3, certfile=self._apn_cert_path)
c.connect((self._apn_host, 2195))
return c
def _write_apn_to_socket(self, msg, existing_socket=None):
if existing_socket:
existing_socket.write(msg)
else:
c = self._connect_socket()
c.write(msg)
c.close()
def _get_msg_list(self):
raise Exception("Not implemented")
class IosPushSandBoxServer(IosPushLiveServer):
def __init__(self):
self._apn_cert_path=getattr(settings, 'APN_SANDBOX_CERT_PATH', None)
self._apn_host=getattr(settings, 'APN_SANDBOX_HOST', None)
if not self._apn_cert_path or not self._apn_host:
raise Exception("APN_SANDBOX_CERT_PATH and/or APN_SANDBOX_HOST not specified.")