Exemple #1
0
    def connect(self, host, port, userid="", password=""):
        self.host = host
        self.port = port
        self.userid = userid
        self.password = password
        self.encrypted = False
        body = "connect\n"
        msg = "API 0 " + str(len(body.encode('utf-8'))) + '\n' + body
        try:
            self.socket.connect((host, port))
            sent = socket_util.sendall(self.socket, msg)
            if sent:
                header = socket_util.readline(self.socket, self.socketbuffer)
                headers = header.split()
                if len(headers) != 3:
                    raise Exception('Header Length Incorrect', header)
                sid, _, is_remote = headers
                msg = socket_util.readline(self.socket, self.socketbuffer)
                if msg != 'OK':
                    raise Exception('Connection failed', msg, b"")
                self.sessionID = sid
                self.remoteLittleEndian = False if is_remote == '0' else True
                data_factory.endianness = '>'.__add__ if is_remote == '0' else '<'.__add__
                if userid and password and len(userid) and len(password):
                    self.signon()

        except Exception as e:
            traceback.print_exc()
            return False
        return True
Exemple #2
0
    def run(self, script, *args):
        # print("run")

        if not script or len(script.strip()) == 0:
            raise Exception('Empty Script Received', script)
        if not self.sessionID:
            raise Exception(
                'Connection has not been established yet; please call function connect!'
            )
        """msg send"""
        objs = []
        if len(args):
            """ function with arguments"""
            body = "function\n" + script
            body += "\n" + str(len(args)) + "\n"
            body += "1" if self.remoteLittleEndian else "0"
            message = "API " + self.sessionID + " " + str(
                len(body.encode('utf-8'))) + '\n' + body
            for arg in args:
                objs.append(self.write_python_obj(arg))
        else:
            """pure script"""
            body = "script\n" + script
            message = "API " + self.sessionID + " " + str(
                len(body.encode('utf-8'))) + '\n' + body
        reconnected = False
        try:
            # print(message)
            # print(objs)
            socket_util.sendall(self.socket, message, objs)
        except IOError:
            reconnected = self._reconnect(message)
        """msg receive"""
        header = socket_util.readline(self.socket, self.socketbuffer)
        while header == "MSG":
            socket_util.read_string(
                self.socket, self.socketbuffer
            )  # python API doesn't support progress listener
            header = socket_util.readline(self.socket, self.socketbuffer)

        headers = header.split()
        if len(headers) != 3:
            raise Exception('Header Length Incorrect', header)
        if reconnected:
            if not self.sessionID == headers[0]:
                print(
                    "old sessionID %s is invalid after reconnection; new sessionID is %s\n"
                    % (self.sessionID, headers[0]))
                self.sessionID = headers[0]

        sid, obj_num, _ = headers
        msg = socket_util.readline(self.socket, self.socketbuffer)
        if msg != 'OK':
            raise Exception('Server Exception', msg)
        if int(obj_num) == 0:
            return None
        return self.read_dolphindb_obj
Exemple #3
0
    def rpc(self, function_name, *args):
        """

        :param function_name: remote function call name
        :param args: arguments for remote function
        :return: return remote function call result
        """
        """msg send"""
        # if len(args):
        """ function with arguments"""
        body = "function\n" + function_name
        body += "\n" + str(len(args)) + "\n"
        body += "1" if self.remoteLittleEndian else "0"
        message = "API " + self.sessionID + " " + str(len(
            body.encode('utf-8'))) + '\n' + body
        for arg in args:
            message = self.write_python_obj(arg)
        # else:
        #     """pure script"""
        #     body = "script\n"+function_name
        #     message = "API "+self.sessionID + " " + str(len(body)) + '\n' + body

        #print(message)

        reconnected = False
        try:
            socket_util.sendall(self.socket, message)
        except IOError:
            reconnected = self._reconnect(message)
        """msg receive"""
        header = socket_util.readline(self.socket, self.socketbuffer)
        while header == "MSG":
            socket_util.read_string(
                self.socket, self.socketbuffer
            )  # python API doesn't support progress listener
            header = socket_util.readline(self.socket, self.socketbuffer)

        headers = header.split()
        if len(headers) != 3:
            raise Exception('Header Length Incorrect', header)
        if reconnected:
            if not self.sessionID == headers[0]:
                print(
                    "old sessionID %s is invalid after reconnection; new sessionID is %s\n"
                    % (self.sessionID, headers[0]))
                self.sessionID = headers[0]

        sid, obj_num, _ = headers
        msg = socket_util.readline(self.socket, self.socketbuffer)
        if msg != 'OK':
            raise Exception('Server Exception', msg)
        if int(obj_num) == 0:
            return None
        return self.read_dolphindb_obj
Exemple #4
0
 def upload(self, nameObjectDict):
     if not isinstance(nameObjectDict, dict) or not nameObjectDict:
         print('\n Empty name/object mapping received - no upload\n')
         return None
     if not self.sessionID:
         raise Exception(
             'Connection has not been established yet; please call function connect!'
         )
     """upload variable names """
     body = "variable\n"
     objects = []
     pyObj = []
     for key in nameObjectDict:
         if not isinstance(key, str):
             raise ValueError("variable name" + key + " is not a string")
         if re.match(r"[^a-zA-Z]", key) or re.search(r"[^_a-zA-Z0-9]", key):
             raise ValueError("variable name" + key + " is not valid")
         body += key + ","
         objects.append(nameObjectDict[key])
     body = body[:-1]
     """upload objects"""
     body += "\n" + str(len(objects)) + "\n"
     body += "1" if self.remoteLittleEndian else "0"
     message = "API " + self.sessionID + " " + str(len(
         body.encode('utf-8'))) + '\n' + body
     for o in objects:
         pyObj.append(self.write_python_obj(o))
     reconnected = False
     try:
         totalsent = socket_util.sendall(self.socket, message, pyObj)
     except IOError as e:
         traceback.print_exc(e)
         reconnected = self._reconnect(message)
     """msg receive"""
     header = socket_util.readline(self.socket, self.socketbuffer)
     headers = header.split()
     if len(headers) != 3:
         raise Exception('Header Length Incorrect', header)
     if reconnected:
         if not self.sessionID == headers[0]:
             print(
                 "old sessionID %s is invalid after reconnection; new sessionID is %s\n"
                 % (self.sessionID, headers[0]))
             self.sessionID = headers[0]
     sid, obj_num, _ = headers
     msg = socket_util.readline(self.socket, self.socketbuffer)
     if msg != 'OK' and obj_num == 0:
         raise Exception('Server Exception', msg)
     return None