Example #1
0
 def execute(self, sql, args=[]):
     try:
         self._cursor.execute(sql, args)
     except BaseException as e:
         logging.error('Error execute')
         logging.exception(traceback.format_exe())
         raise DbException(e)
Example #2
0
 def execute(self, sql, args=[]):
     try:
         self._cursor.execute(sql, args)
     except BaseException as e:
         logging.error('Error execute')
         logging.exception(traceback.format_exe())
         raise DbException(e)
Example #3
0
 def get(self, key):
     c = -1
     try:
         c = int(self.db.Get(key))
     except Exception as e:
         import traceback
         logging.error(traceback.format_exe())
     return c
Example #4
0
    def main(self):  # 创建多个接收线程
        #self.get3DCSN()
        try:
            _thread.start_new_thread(self.Server())  # 6001 接收标定工位端口
            #_thread.start_new_thread(Server, (ip, port))  # 7001 接收深度工位端口
            #_thread.start_new_thread(Server, (ip, 8001))  # 8001 接收 AE 工位端口
            while 1:
                pass
        except:
            print(traceback.format_exe())
            #print("Error: 无法启动线程")


# c=Accept
Example #5
0
    def get3DCSN(self):
        try:
            base_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
            #print(base_dir)
            #self.Txt_write("base_dir:%s"%base_dir)
            cc = os.path.join(base_dir, '3DCSN.xlsx')
            try:
                if os.path.exists(cc):
                    self.Txt_write('load_workbook')
                    wb = openpyxl.load_workbook(cc)  # 加载存在的Excel表
                    self.Txt_write("wb:%s" % str(wb))
                else:
                    self.Txt_write("error_exist")
                    return
            except:
                msg = traceback.format_exe()
                self.Txt_write("error_msg:%s" % (str(msg)))
            a_sheet = wb.get_sheet_by_name('Sheet1')  # 根据表名获取表对象

            now = time.strftime('%m%d', time.localtime(time.time()))
            count = a_sheet.max_row
            number = ("%06d" % count)
            sn = 'HMD3DC20' + now + number

            # 保存新的SN号
            ws = wb.active
            ws.append([sn])
            wb.save(cc)
            #self.Txt_write("now:%s" % now)
            print(a_sheet.cell(row=(a_sheet.max_row), column=1).value)
            self.Txt_write(
                "a_sheet.cell(row=(a_sheet.max_row),column=1).value:%s" %
                a_sheet.cell(row=(a_sheet.max_row), column=1).value)
            return a_sheet.cell(row=(a_sheet.max_row), column=1).value
        except:
            print(traceback.format_exe())
            self.Txt_write(traceback.format_exe())
Example #6
0
    def create_message(access_token,
                       channel_id,
                       text='',
                       attachments=[],
                       in_channel=False,
                       is_async=False):

        try:

            is_image = False
            if 'https://res.cloudinary.com/' in text:
                is_image = True

            if len(text) >= 3500:
                search_text = re.findall(
                    '^(.* posted the) <(https://.*)\|(.*)>.*:\n', text)
                if search_text:
                    new_content_text = search_text[0][0]
                    link = search_text[0][1]
                    new_content_type = search_text[0][2]
                    text = '%s %s. WARNING: this content cannot be displayed, ' \
                           'please read the complete content <%s|HERE>' \
                           % (new_content_text, new_content_type, link)

            params = {
                'channel': channel_id,
                'text': text,
                'attachments': json.dumps(attachments),
                'unfurl_links': False,
                'unfurl_media': is_image,
            }
            if in_channel:
                params['response_type'] = 'in_channel'

            if not is_async:
                return requests.get(
                    url='https://slack.com/api/chat.postMessage',
                    params=params,
                    headers={'Authorization': f'Bearer {access_token}'})
            else:
                return get_request_task(
                    'https://slack.com/api/chat.postMessage', params,
                    access_token)
        except:
            print(traceback.format_exe())
def copyfilesmulti(fileslist, threadsnum, targetfolder):
    try:
        po = multiprocessing.Pool(threadsnum)
        start = 0
        groupSize = int(len(fileslist) / threadsnum)
        for idx in range(threadsnum):
            if idx == threadsnum - 1:
                end = len(fileslist)
            else:
                end = (idx + 1) * groupSize

            po.apply_async(func=copyfile,
                           args=(start, end, fileslist, targetfolder),
                           callback=f_callback(start, end))
            start = start + groupSize
        po.close()
        po.join()
    except Exception as e:
        print(traceback.format_exe(e))
        exit()
Example #8
0
    def Server(self):
        try:
            self.get3DCSN()
            print(self.port)
            ip_port = (self.ip, self.port)

            print(ip_port)
            sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            print(sk)
            sk.bind(ip_port)
            sk.listen(128)
            path = os.path.dirname(os.path.realpath(sys.argv[0]))
        except:
            print(traceback.format_exe())
        # 当前路径
        while True:
            print('server waiting...')
            self.Txt_write('server waiting...')
            conn, addr = sk.accept()  # 阻塞等待客户端连接
            print("来自%s的连接:" % addr[0])
            self.Txt_write("来自%s的连接:" % addr[0])

            while True:
                client_data = conn.recv(1024)

                print(len(client_data))
                # 判断长度
                if len(client_data) == 6:  # 判断数据的长度
                    aa = client_data.hex()
                    print(aa)
                    if aa == "7e800501097e":  # 反馈SN号
                        print(self.get3DCSN())
                        conn.send(self.get3DCSN().encode())

                    elif aa == "7e800101097e":  # 接收SN
                        conn.send('start rsv SN'.encode())
                        data = conn.recv(1024)  # 接收SN
                        # 创建文件
                        file_name = path + '\\' + 'test' + '\\' + bytes.decode(
                            data)
                        print(file_name)
                        my_file = Path(file_name)
                        print(my_file)
                        if not my_file.exists():
                            os.mkdir(file_name)
                        conn.send('SN is OK'.encode())
                    elif aa == "7e800201097e":  # 接收ZIP包
                        conn.send('start rsv ZIP'.encode())
                        data = conn.recv(1024)  # 接收压缩包
                        f = open(file_name + "/data.zip", 'wb')
                        print('start rsv ZIPHASH')
                        while len(data) > 0:
                            f.write(data)
                            print(len(data))
                            if len(data) == 6 and data.hex() == "7e8002010a7e":
                                conn.send('start rsv ZIPHASH'.encode())
                                data = conn.recv(1024)  # 接收压缩包HASH指令
                                if bytes.decode(data) == self.CalcSha512(
                                        file_name + "/data.zip"):
                                    conn.send('ZIPHASH is OK'.encode())
                                    break
                                else:
                                    conn.send('ZIPHASH is ERROR'.encode())
                            elif len(data) != 1024:
                                conn.send('ZIP is OK'.encode())
                                self.Txt_write('ZIP is OK')
                                break
                                # ad1b6699ab5cd76c5edd0b31b61b1046b6efd02ea637277711f62931be1989561145b4fa3afc5dc207e42239c13039664818c417287903c1fa8c3629ebd5948a
                            data = conn.recv(1024)  # 接收压缩包
                        f.close()
                        print('ZIP is OK')
                    elif aa == "7e8002010a7e":  # 接收压缩包HASH指令
                        conn.send('start rsv ZIPHASH'.encode())
                        print(self.CalcSha512(file_name + "/data.zip"))
                        data = conn.recv(1024)
                        print(bytes.decode(data))
                        if bytes.decode(data) == self.CalcSha512(file_name +
                                                                 "/data.zip"):
                            conn.send('ZIPHASH is OK'.encode())
                            print('ZIPHASH is OK')
                            self.Txt_write('ZIPHASH is OK')
                        else:
                            conn.send('ZIPHASH is ERROR'.encode())
                            self.Txt_write('ZIPHASH is ERROR')
                    elif aa == "7e800601097e":  # 确认建立连接
                        conn.send('socket OK'.encode())
                        print('socket OK')
                    elif aa == "7e800401097e":  # 断开连接
                        conn.send('close OK'.encode())
                        print('close OK')
                        self.Txt_write('close OK')
                        break
                    else:
                        print("等待连接....")
        #except:
        #print("SOCKET ERROR")
        sk.close()
                               method="sync")
    producer.send(resp_msg)
    print(f"Response is sent: \n {resp_msg} \n")


if __name__ == '__main__':
    consumer = MessageConsumer(consumer_id="1",
                               consumer_group='actuator_req_manager',
                               message_types=["requests"],
                               auto_ack=False,
                               offset='earliest')

    while True:
        try:
            print("In receiver")
            message = consumer.receive(timeout=0)
            #consumer.ack()
            #continue

            msg = json.loads(message.decode('utf-8'))
            print(f"Got a request: \n {msg} \n")
            consumer.ack()

            # Create a new sender for topic requests
            send_response(json.dumps(msg))

        except Exception as e:
            print(e)
            print(traceback.format_exe())
            sys.exit(0)