SHARE
TWEET

pySDEE with hardcoded TLSv1

a guest May 22nd, 2014 258 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import urllib
  2. import urllib2
  3. import base64
  4. import time
  5. import types
  6.  
  7. import xml.dom.minidom
  8. from xml.dom.minidom import Node
  9.  
  10. # The section below is to override the default socket connection
  11. # which will fail with these devices. The newer version of openssl
  12. # in Python does not support the ciphers these devices would like to use
  13. import httplib
  14. from httplib import HTTPConnection, HTTPS_PORT
  15. import ssl
  16. import socket
  17.  
  18. class HTTPSConnection(HTTPConnection):
  19.         default_port = HTTPS_PORT
  20.  
  21.         def __init__(self, host, port=None, key_file=None, cert_file=None,
  22.                      strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
  23.                      source_address=None):
  24.             HTTPConnection.__init__(self, host, port, strict, timeout,
  25.                                     source_address)
  26.             self.key_file = key_file
  27.             self.cert_file = cert_file
  28.  
  29.         def connect(self):
  30.             sock = socket.create_connection((self.host, self.port),
  31.                                             self.timeout, self.source_address)
  32.             if self._tunnel_host:
  33.                 self.sock = sock
  34.                 self._tunnel()
  35.             self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file, ssl_version=ssl.PROTOCOL_TLSv1)
  36.  
  37.  
  38.  
  39.  
  40.  
  41. def parse_open(action, data):
  42.         doc = xml.dom.minidom.parseString(data)
  43.         try:   
  44.                 sess = doc.getElementsByTagName('env:Header')[0].getElementsByTagName('sd:oobInfo')[0].getElementsByTagName('sd:sessionId')[0]
  45.                 sessionid = sess.firstChild.wholeText
  46.         except:
  47.                 sessionid = "IOS_ROUTER"
  48.  
  49.  
  50.         subscript = doc.getElementsByTagName('env:Body')[0].getElementsByTagName('sd:subscriptionId')[0]
  51.         subscriptionid = subscript.firstChild.wholeText
  52.  
  53.         return [sessionid, subscriptionid]
  54.  
  55. def nano(epoch):
  56.  
  57.         return int(epoch * 1e9)
  58.  
  59. def epoch(nano):
  60.  
  61.         return ( nano / 1e9 )  
  62.  
  63. class SDEE:
  64.  
  65.         def __init__(self, **kwargs):
  66.  
  67.                 try: self._callback = kwargs['callback']
  68.                 except: self._callback = ''
  69.  
  70.                 try: self._format = kwargs['format']
  71.                 except: self._format = 'raw'
  72.  
  73.                 try:  self._timeout = kwargs['timeout']
  74.                 except: self._timeout = 1
  75.  
  76.                 try: self._user = kwargs['user']
  77.                 except: self._user = ''
  78.  
  79.                 try: self._password = kwargs['password']
  80.                 except: self._password = ''
  81.  
  82.                 try: self._host = kwargs['host']
  83.                 except: self._host = 'localhost'
  84.  
  85.                 try: self._method = kwargs['method']
  86.                 except: self._method = 'https'
  87.                
  88.                 try: self._resource = kwargs['resource']
  89.                 except: self._resource = 'cgi-bin/sdee-server'
  90.  
  91.                 self._uri = "%s://%s/%s" % (self._method, self._host, self._resource)  
  92.  
  93.                 try: self._sessionid = kwargs['sessionid']
  94.                 except:  self._sessionid = ''
  95.  
  96.                 try: self._subscriptionid = kwargs['subscriptionid']
  97.                 except: self._subscriptionid = ''
  98.  
  99.                 try: self._starttime = kwargs['starttime']
  100.                 except: self._starttime = nano(time.time())
  101.                
  102.                 self._b64pass = base64.encodestring("%s:%s" % (self._user, self._password) )
  103.                
  104.                 self._response = ''
  105.  
  106.                 try: self._force = kwargs['force']
  107.                 except: self._force = 'yes'
  108.  
  109.         def data(self):
  110.                 return self._response
  111.  
  112.         def Password(self, passwd):
  113.                 self._password = passwd
  114.                 self._b64pass = base64.encodestring("%s:%s" % (self._user, self._password) )
  115.  
  116.         def User(self, username):
  117.                 self._user = username
  118.                 self._b64pass = base64.encodestring("%s:%s" % (self._user, self._password) )
  119.  
  120.  
  121.         def Host(self, host):
  122.                 self._host = host
  123.                 self._uri = "%s://%s/%s" % (self._method, self._host, self._resource)  
  124.        
  125.         def Method(self, method):
  126.                 self._method = method
  127.                 self._uri = "%s://%s/%s" % (self._method, self._host, self._resource)  
  128.        
  129.         def Resource(self, resource):
  130.                 self._resource = resource
  131.                 self._uri = "%s://%s/%s" % (self._method, self._host, self._resource)  
  132.  
  133.                
  134.         def _request(self, params, **kwargs):
  135.                 req = urllib2.Request("%s?%s" % (self._uri, params))
  136.                 req.add_header('Authorization', "BASIC %s" % (self._b64pass) )
  137.                 data = urllib2.urlopen(req)
  138.                 self._response = data.read()
  139.                 if self._action == 'open':
  140.                         self._sessionid, self._subscriptionid = parse_open(self._action, self._response)       
  141.  
  142. #                       print self._sessionid
  143. #                       print self._subscriptionid
  144.                 elif self._action == 'close':
  145.                         print data.read()
  146.                 elif self._action == 'cancel':
  147.                         print data.read()
  148.                 elif self._action == 'get':
  149.                         if type(self._callback) is types.FunctionType:
  150.                                 self._callback(**kwargs)
  151.                 elif self._action == 'query':
  152.                         pass
  153.  
  154.         def open(self, **kwargs):
  155.                 self._action = 'open'
  156.                 param_dict = {"events" : "evIdsAlert", "action" :"open", "force": self._force}
  157.                 if self._subscriptionid != '':
  158.                         param_dict['subscriptionId'] = self._subscriptionid
  159.                 params = urllib.urlencode(param_dict)
  160.                 self._request(params)
  161.         def close(self, **kwargs):
  162.                 self._action = 'close'
  163.                 params = urllib.urlencode({ "action":"close",
  164.                                         "subscriptionId":self._subscriptionid})
  165.                 req = self._request(params)
  166.        
  167.         def cancel(self, **kwargs):
  168.                 self._action = 'cancel'
  169.                 params = urllib.urlencode({
  170.                                         "action":"cancel",
  171.                                         "subscriptionId":self._subscriptionid,
  172.                                         "sessionId": self._sessionid})
  173.                 req = self._request(params)
  174.        
  175.         def get(self, **kwargs):
  176.                
  177.                 self._action = 'get'
  178.                 params = urllib.urlencode({"confirm":"yes",
  179.                                 "timeout":"1",
  180.                                 "maxNbrofEvents":"20",
  181.                                 "action":self._action,
  182.                                 "subscriptionId":self._subscriptionid})        
  183.                 req = self._request(params, **kwargs)
  184.  
  185.         def query(self, **kwargs):
  186.                 pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top