def __init__(self, host=None, port=10000, authMechanism=None, user=None, password=None, database=None, configuration=None): super(Connection, self).__init__(authMechanism) #Must set a password for thrift, even if it doesn't need one #Open issue with python-sasl password = self._check_password(authMechanism, password) socket = TSocket(host, port) if authMechanism == 'NOSASL': transport = TBufferedTransport(socket) else: saslc, sasl_mech = self._get_sasl_client(host, authMechanism, user, password, configuration) transport = TSaslClientTransport(saslc, sasl_mech, socket) self.client = TCLIService.Client(TBinaryProtocol(transport)) transport.open() res = self.client.OpenSession( TOpenSessionReq(configuration=configuration)) self.session = res.sessionHandle if database is not None: with self.cursor() as cur: query = "USE {0}".format(database) cur.execute(query)
def __init__(self, host=None, port=10000, authMechanism=None, user=None, password=None, database=None, cursorclass=Cursor): authMechanisms = set(['NOSASL', 'PLAIN', 'KERBEROS', 'LDAP']) if authMechanism not in authMechanisms or authMechanism == 'KERBEROS': raise NotImplementedError( 'authMechanism is either not supported or not implemented') #Must set a password for thrift, even if it doesn't need one #Open issue with python-sasl if authMechanism == 'PLAIN' and (password is None or len(password) == 0): password = '******' socket = TSocket(host, port) self.cursorclass = cursorclass if authMechanism == 'NOSASL': transport = TBufferedTransport(socket) else: saslc = sasl.Client() saslc.setAttr("username", user) saslc.setAttr("password", password) saslc.init() transport = TSaslClientTransport(saslc, "PLAIN", socket) self.client = TCLIService.Client(TBinaryProtocol(transport)) transport.open() res = self.client.OpenSession(TOpenSessionReq()) self.session = res.sessionHandle if database is not None: with self.cursor() as cur: query = "USE {0}".format(database) cur.execute(query)
def __init__(self, unix_socket=None, host=None, port=10000, authMechanism=None, user=None, password=None, database=None, configuration=None, timeout=None): authMechanisms = set(['NOSASL', 'PLAIN', 'KERBEROS', 'LDAP']) if authMechanism not in authMechanisms: raise NotImplementedError( 'authMechanism is either not supported or not implemented') #Must set a password for thrift, even if it doesn't need one #Open issue with python-sasl if authMechanism == 'PLAIN' and (password is None or len(password) == 0): password = '******' if unix_socket is not None: socket = TSocket(unix_socket=unix_socket) else: socket = TSocket(host, port) socket.setTimeout(timeout) if authMechanism == 'NOSASL': transport = TBufferedTransport(socket) else: sasl_mech = 'PLAIN' saslc = sasl.Client() saslc.setAttr("username", user) saslc.setAttr("password", password) if authMechanism == 'KERBEROS': krb_host, krb_service = self._get_krb_settings( host, configuration) sasl_mech = 'GSSAPI' saslc.setAttr("host", krb_host) saslc.setAttr("service", krb_service) saslc.init() transport = TSaslClientTransport(saslc, sasl_mech, socket) self.client = TCLIService.Client(TBinaryProtocol(transport)) transport.open() res = self.client.OpenSession( TOpenSessionReq(username=user, password=password, configuration=configuration)) self.session = res.sessionHandle if database is not None: with self.cursor() as cur: query = "USE {0}".format(database) cur.execute(query)
def sasl_factory(): saslc = sasl.Client() saslc.setAttr("username", username) saslc.setAttr("password", password) saslc.init() return saslc try: print "1) Preparing the connection..." sock = TSocket(host, port) if auth == 'NOSASL': transport = TBufferedTransport(sock) else: transport = TSaslClientTransport(sasl_factory, "PLAIN", sock) client = TCLIService.Client(TBinaryProtocol(transport)) transport.open() print "\n2) Opening Session..." res = client.OpenSession( TOpenSessionReq(username=username, password=password)) session = res.sessionHandle print('Session opened. ( %s )' % session.sessionId) ## 3) Show tables print "\n3) Try fetching table list..." query = TExecuteStatementReq(session, statement="show tables", confOverlay={}) response = client.ExecuteStatement(query)