# session.py

import random
import xmltodict

# try python 3 imports, fall back to python 2
try:
    from urllib.parse import urlencode
    from urllib.request import urlopen, Request
    from urllib.error import URLError, HTTPError
except ImportError:
    from urllib import urlencode
    from urllib2 import urlopen, Request, URLError, HTTPError

class Session:
    def __init__(self, user, pw, host='localhost', port=8834):
        self.host = host
        self.port = port

    def __enter__(self):
        self.token = self.get('login', login=user,password=pw)['token']

    def __exit__(self, type, value, traceback):
        self.close()

    def close(self):
        self.get('logout')['contents'] == 'OK'
    
    def get(self, path, **kwargs):
        if 'token' not in kwargs and hasattr(self, 'token'):
            kwargs['token'] = self.token
        kwargs['seq'] = random.randrange(1000000)
        url = 'https://{0}:{1}/{2}'.format(self.host,self.port,path)
        request = Request(url, urlencode(kwargs))
        try:
            response = urlopen(request)
            response_data = xmltodict.parse(response.read())
            if response_data['seq'] != kwargs['seq']:
                raise Exception("Unique number did not match!")
            elif response_data['status'] != 'OK':
                raise AuthenticationError("Invalid credentials")
            return response_data
        except HTTPError as e:
            raise AuthenticationError
        except URLError as e:
            raise ConnectionError

class ConnectionError(Exception):
    pass

class AuthenticationError(Exception):
    pass


@@ -473,34 +473,8 @@
 port
-%0A%0A    def __enter__(self):
 %0A   
@@ -547,78 +547,8 @@
 '%5D%0A%0A
-    def __exit__(self, type, value, traceback):%0A        self.close()%0A%0A
     
@@ -568,24 +568,31 @@
 f):%0A        
+return 
 self.get('lo
@@ -601,20 +601,8 @@
 ut')
-%5B'contents'%5D
  == 
@@ -1030,16 +1030,25 @@
 .read())
+%5B'reply'%5D
 %0A       
@@ -1055,16 +1055,20 @@
      if 
+int(
 response
@@ -1079,16 +1079,17 @@
 a%5B'seq'%5D
+)
  != kwar
@@ -1310,16 +1310,28 @@
 nse_data
+%5B'contents'%5D
 %0A       

Make things work properly