10 from nebula3.fbthrift.transport
import (
16 from nebula3.fbthrift.transport.TTransport
import TTransportException
17 from nebula3.fbthrift.protocol
import THeaderProtocol
19 from nebula3.common.ttypes
import ErrorCode
20 from nebula3.graph
import GraphService
21 from nebula3.graph.ttypes
import VerifyClientVersionReq
22 from nebula3.logger
import logger
24 from nebula3.Exception
import (
27 ClientServerIncompatibleException,
40 self.
_port_port =
None
44 def open(self, ip, port, timeout):
45 """open the connection
47 :param ip: the server ip
48 :param port: the server port
49 :param timeout: the timeout for connect and execute
52 self.
open_SSLopen_SSL(ip, port, timeout,
None)
54 def open_SSL(self, ip, port, timeout, ssl_config=None):
55 """open the SSL connection
57 :param ip: the server ip
58 :param port: the server port
59 :param timeout: the timeout for connect and execute
60 :ssl_config: configs for SSL
64 self.
_port_port = port
68 if ssl_config
is not None:
69 s = TSSLSocket.TSSLSocket(
72 ssl_config.unix_socket,
73 ssl_config.ssl_version,
76 ssl_config.verify_name,
79 ssl_config.allow_weak_ssl_versions,
82 s = TSocket.TSocket(self.
_ip_ip, self.
_port_port)
86 buffered_transport = TTransport.TBufferedTransport(s)
87 header_transport = THeaderTransport.THeaderTransport(buffered_transport)
88 protocol = THeaderProtocol.THeaderProtocol(header_transport)
89 header_transport.open()
91 self.
_connection_connection = GraphService.Client(protocol)
92 resp = self.
_connection_connection.verifyClientVersion(VerifyClientVersionReq())
93 if resp.error_code != ErrorCode.SUCCEEDED:
95 raise ClientServerIncompatibleException(resp.error_msg)
100 """reopen the connection
111 """authenticate to graphd
113 :param user_name: the user name
114 :param password: the password
119 if resp.error_code != ErrorCode.SUCCEEDED:
120 raise AuthFailedException(resp.error_msg)
122 resp.session_id, resp.time_zone_offset_seconds, resp.time_zone_name
124 except TTransportException
as te:
125 if te.message.find(
"timed out"):
127 if te.type == TTransportException.END_OF_FILE:
129 raise IOErrorException(IOErrorException.E_CONNECT_BROKEN, te.message)
132 """execute interface with session_id and ngql
134 :param session_id: the session id get from result of authenticate interface
135 :param stmt: the ngql
136 :return: ExecutionResponse
141 """execute interface with session_id and ngql
142 :param session_id: the session id get from result of authenticate interface
143 :param stmt: the ngql
144 :param params: parameter map
145 :return: ExecutionResponse
148 resp = self.
_connection_connection.executeWithParameter(session_id, stmt, params)
150 except Exception
as te:
151 if isinstance(te, TTransportException):
152 if te.message.find(
"timed out") > 0:
154 raise IOErrorException(IOErrorException.E_TIMEOUT, te.message)
155 elif te.type == TTransportException.END_OF_FILE:
156 raise IOErrorException(
157 IOErrorException.E_CONNECT_BROKEN, te.message
159 elif te.type == TTransportException.NOT_OPEN:
160 raise IOErrorException(IOErrorException.E_NOT_OPEN, te.message)
162 raise IOErrorException(IOErrorException.E_UNKNOWN, te.message)
166 """execute_json interface with session_id and ngql
167 :param session_id: the session id get from result of authenticate interface
168 :param stmt: the ngql
169 :return: string json representing the execution result
174 """execute_json interface with session_id and ngql with parameter
175 :param session_id: the session id get from result of authenticate interface
176 :param stmt: the ngql
177 :param params: parameter map
178 :return: string json representing the execution result
181 resp = self.
_connection_connection.executeJsonWithParameter(session_id, stmt, params)
183 except Exception
as te:
184 if isinstance(te, TTransportException):
185 if te.message.find(
"timed out") > 0:
187 raise IOErrorException(IOErrorException.E_TIMEOUT, te.message)
188 elif te.type == TTransportException.END_OF_FILE:
189 raise IOErrorException(
190 IOErrorException.E_CONNECT_BROKEN, te.message
192 elif te.type == TTransportException.NOT_OPEN:
193 raise IOErrorException(IOErrorException.E_NOT_OPEN, te.message)
195 raise IOErrorException(IOErrorException.E_UNKNOWN, te.message)
199 """tells the graphd can release the session info
201 :param session_id:the session id
206 except TTransportException
as te:
207 if te.type == TTransportException.END_OF_FILE:
211 """close the connection
217 except Exception
as e:
219 'Close connection to {}:{} failed:{}'.format(self.
_ip_ip, self.
_port_port, e)
223 """check the connection if ok
224 :return: True or False
233 """reset the idletime
240 """get idletime of connection
249 """get the address of the connected service
253 return (self.
_ip_ip, self.
_port_port)
def open(self, ip, port, timeout)
def signout(self, session_id)
def open_SSL(self, ip, port, timeout, ssl_config=None)
def execute(self, session_id, stmt)
def execute_parameter(self, session_id, stmt, params)
def execute_json(self, session_id, stmt)
def execute_json_with_parameter(self, session_id, stmt, params)
def authenticate(self, user_name, password)