Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 27th, 2012  |  syntax: Python  |  size: 2.07 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from django.conf import settings
  2. from socket import socket
  3. import ssl
  4.  
  5. class IosPushServer():
  6.  
  7.     def _connect_socket(self):
  8.         raise NotImplementedError( "Should have implemented this" )
  9.  
  10.     def _write_apn_to_socket(self, msg, existing_socket=None):
  11.         raise NotImplementedError( "Should have implemented this" )
  12.  
  13.     def _get_msg_list(self):
  14.         raise NotImplementedError( "Should have implemented this" )
  15.  
  16.     class Meta:
  17.         abstract = True
  18.  
  19. class IosPushDummyServer(IosPushServer):
  20.  
  21.     c = []
  22.  
  23.     def _connect_socket(self):
  24.         return []
  25.  
  26.     def _write_apn_to_socket(self, msg, existing_socket=None):
  27.         if existing_socket:
  28.             existing_socket.append(msg)
  29.         else:
  30.             self.c = []
  31.             self.c.append(msg)
  32.  
  33.     def _get_msg_list(self):
  34.         return self.c
  35.  
  36. class IosPushLiveServer(IosPushServer):
  37.      
  38.     def __init__(self):
  39.         self._apn_cert_path=getattr(settings, 'APN_LIVE_CERT_PATH', None)
  40.         self._apn_host=getattr(settings, 'APN_LIVE_HOST', None)
  41.         if not self._apn_cert_path or not self._apn_host:
  42.             raise Exception("APN_LIVE_CERT_PATH and/or APN_LIVE_HOST not specified.")
  43.  
  44.     def _connect_socket(self):
  45.         s = socket()
  46.         c = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_SSLv3, certfile=self._apn_cert_path)
  47.         c.connect((self._apn_host, 2195))
  48.         return c
  49.  
  50.     def _write_apn_to_socket(self, msg, existing_socket=None):
  51.         if existing_socket:
  52.             existing_socket.write(msg)
  53.         else:
  54.             c = self._connect_socket()
  55.             c.write(msg)
  56.             c.close()
  57.  
  58.     def _get_msg_list(self):
  59.         raise Exception("Not implemented")
  60.  
  61. class IosPushSandBoxServer(IosPushLiveServer):
  62.  
  63.      def __init__(self):
  64.         self._apn_cert_path=getattr(settings, 'APN_SANDBOX_CERT_PATH', None)
  65.         self._apn_host=getattr(settings, 'APN_SANDBOX_HOST', None)
  66.         if not self._apn_cert_path or not self._apn_host:
  67.             raise Exception("APN_SANDBOX_CERT_PATH and/or APN_SANDBOX_HOST not specified.")