Ejemplo n.º 1
0
def add_router_device(rd: RouterDevice) -> RouterDevice:
    session = DBSession()
    try:

        rd.router_device_id = my_uuid()
        rd_tmp = session.query(RouterDevice).order_by(desc(
            RouterDevice.code)).first()
        max_code = 0
        if rd_tmp is None or rd.code is None:
            max_code = 0
        else:
            max_code = rd.code
        rd.axis_id = 3
        rd.code = max_code + 1
        rd.start_time = datetime.datetime.now()
        rd.status = 0
        rd.name = 'test'
        rd.voltage = 0

        session.add(rd)
        session.commit()
    except Exception as e:
        session.rollback()
        logger.error(e)
        logger.error('add end device fialed %s', rd)
        session.close()
    return rd
Ejemplo n.º 2
0
def commit_session(sess):
    try:
        sess.commit()
    except Exception as e:
        logger.error(e)
        sess.rollback()
        logger.warning('commit failed')
    sess.close()
Ejemplo n.º 3
0
 def fuzzer_thread(ip, port, data, secure, stats):
     """
     Represent a nested thread routine
     """
     while True:
         #  if we got something to process from our parent process let's process it
         while not fuzzer_queue.empty():
             #  get the element to fuzz
             fuzzed = fuzzer_queue.get()
             result = [None, 0, 0, None, None]
             #  perform the request until we got a result
             while result[1] == 0:
                 #  make the actual request and return the stats for the fuzzed request
                 result = basic_info(ip, port, HTTPRequestParser(
                     clean_template(data, fuzzed)), secure)
                 # we really got a result? :D
                 if result[1] > 0:
                     # write to a console output
                     print_to_console(result, fuzzed)
                     break
                 else:
                     #  maybe we are going to fast?
                     time.sleep(1)
             # process_queue.put(result)
             #  lock the global stats
             global_thread_lock.acquire()
             #  check against stats
             # FIXME: pga, 777 is special case when exception occured
             # if is_interesting(result, stats, fuzzed):
             if result[0] == 500 or result[0] not in [400, 403, 200, 204]:
                 #  we got something interesting update global stats
                 merge_stats(result, stats)
                 #  we got something interesting let's notify parent process
                 process_queue.put("Got something interesting!\n\n"
                                   "     Payload: {0}\n"
                                   "     HTTP Code: {1}\n"
                                   "     Execution time: {2}\n"
                                   "     Response Length: {3}\n"
                                   "     Response Hash: {4}\n"
                                   "     Response: {5}\n"
                                   .format(fuzzed, result[0], result[1],
                                           result[2], result[3], result[4]))
                 logger.error("Got something interesting!\n\n"
                              "     Payload: {0}\n"
                              "     HTTP Code: {1}\n"
                              "     Execution time: {2}\n"
                              "     Response Length: {3}\n"
                              "     Response Hash: {4}\n"
                              "     Response: {5}\n"
                              " whole result : {6} \n"
                              .format(fuzzed, result[0], result[1],
                                      result[2], result[3], result[4], result))
             # unlock the global stats
             global_thread_lock.release()
             # skip to the next element
             fuzzer_queue.task_done()
             # sleep to prevent high CPU usage
             time.sleep(0.2)
Ejemplo n.º 4
0
 def run(self):
     while True:
         try:
             time.sleep(self.t)
             set_coor_clock()
         except Exception as e:
             time.sleep(5)
             logger.error(e)
     pass
Ejemplo n.º 5
0
 def execute_sql(self, sql, params=None):
     if params is None:
         params =[]
     if self.conn:
         try:
             curr = self.conn.cursor()
             curr.execute(sql,params)
             self.conn.commit()
         except Exception as e:
             logger.error(f"Error {e}")
Ejemplo n.º 6
0
 def fuzzer_thread(ip, port, data, secure, stats):
     """
     Represent a nested thread routine
     """
     while True:
         #  if we got something to process from our parent process let's process it
         while not fuzzer_queue.empty():
             #  get the element to fuzz
             fuzzed = fuzzer_queue.get()
             print("fuzzed: {0}".format(fuzzed))
             result = [None, 0, 0, None]
             #  perform the request until we got a result
             while result[1] == 0:
                 #  make the actual request and return the stats for the fuzzed request
                 result = basic_info(
                     ip, port,
                     HTTPRequestParser(clean_template(data, fuzzed)),
                     secure)
                 # we really got a result? :D
                 if result[1] > 0:
                     print result  # add by hzx
                     break
                 else:
                     #  maybe we are going to fast?
                     time.sleep(0)
             # process_queue.put(result)
             #  lock the global stats
             global_thread_lock.acquire()
             #  check against stats
             # if is_interesting(result, stats, fuzzed):  #comment by hzx
             if result[0] == 500:
                 #  we got something interesting update global stats
                 merge_stats(result, stats)
                 #  we got something interesting let's notify parent process
                 process_queue.put("Got something interesting!\n\n"
                                   "     Payload: {0}\n"
                                   "     HTTP Code: {1}\n"
                                   "     Execution time: {2}\n"
                                   "     Response Length: {3}\n"
                                   "     Response Hash: {4}\n".format(
                                       fuzzed, result[0], result[1],
                                       result[2], result[3]))
                 logger.error("Got something interesting!\n\n"
                              "     Payload: {0}\n"
                              "     HTTP Code: {1}\n"
                              "     Execution time: {2}\n"
                              "     Response Length: {3}\n"
                              "     Response Hash: {4}\n"
                              " whole result : {5} \n".format(
                                  fuzzed, result[0], result[1], result[2],
                                  result[3], result))
             # unlock the global stats
             global_thread_lock.release()
             #  skip to the next element
             fuzzer_queue.task_done()
Ejemplo n.º 7
0
def add_room_axis(axis: RoomAxis):
    session = DBSession()
    try:
        session.add(axis)
        session.commit()
    except Exception as e:
        session.rollback()
        logger.error(e)
        logger.error('add end device fialed %s', axis)
        session.close()
    return axis
    pass
Ejemplo n.º 8
0
def main(argv=None):
    # init logger first
    my_logger.LoggerInit()
    # check opt
    if argv is None:
        argv = sys.argv

    config_file = argv[1]
    if config_file == '':
        logger.info("using default config file: price_cfg.json")
        config_file = './price_cfg.json'

    # load config
    config = my_config.LoadConfig(config_file)
    if config is None:
        logger.error("get config failed, config file is " + config_file)
        return 2

    # get check point
    news_check.LoadCheckPoint()

    all_ok = False
    a = True
    while (a):
        logger.info("start to get news presell buidings")
        buildings_alias_list = get_news.GetPreSellBuildingsAlias()
        bda_cnt = len(buildings_alias_list)
        logger.info("get news presell buidings, count: " + str(bda_cnt))
        if bda_cnt == 0:
            time.sleep(120)
            continue

        logger.info("start to tp get price detail and out to xls")
        files2mail = []
        update_info = get_news.PreSellInfo("", "")
        is_first = True
        for bdinfo in buildings_alias_list:
            logger.info("proc building: " + bdinfo.name)
            if is_first:
                update_info = bdinfo
                is_first = False
            all_ok = building_price.GetAndSavePriceInfo(bdinfo, files2mail)
            if not all_ok:
                logger.info(
                    "get and save price info failed, building alias : " +
                    bdinfo.name.encode(encoding='UTF-8'))

        if all_ok:
            logger.info("these files need to email: ")
            email_op.SendEMail(files2mail)
            news_check.UpdateCheckPoint(update_info)

        time.sleep(120)
Ejemplo n.º 9
0
 def coronavirus_analysis(self, path):
     try:
         with open(path, encoding="UTF8") as csv_file:
             all_row = csv.reader(csv_file, delimiter=',')
             next(csv_file)
             for row in all_row:
                 self.insert_analysis_of_cases(date=row[0],
                                               total_cases=row[13],
                                               total_positive_cases=row[7],
                                               new_positive_cases=row[8],
                                               hospitalized=row[2])
     except Exception as e:
         logger.error("Coronavirus cases analysis failed", exc_info=True)
Ejemplo n.º 10
0
 def vacinnes_analysis(self, path):
     try:
         with open(path, encoding="UTF8") as csv_file:
             all_row = csv.reader(csv_file, delimiter=',')
             next(csv_file)
             for row in all_row:
                 self.insert_analysis_of_vacinnes(age_range=row[0],
                                                  both_sexes=row[1],
                                                  males=row[2],
                                                  females=row[3],
                                                  first_dose=row[11],
                                                  second_dose=row[12])
     except Exception as e:
         logger.error("Coronavirus vacinnes analysis failed", exc_info=True)
Ejemplo n.º 11
0
def update_net_param(net_param: NetParams):
    sess = DBSession()
    try:
        query = sess.query(NetParams)
        rst = query.filter_by(net_param_id=1).first()
        rst.remote_uart_addr = 1234
        logger.info(rst)
        sess.commit()
    except Exception as e:
        logger.error(e)
        sess.rollback()
        logger.warning('update router_device failed %s' % net_param)
    sess.close()
    pass
Ejemplo n.º 12
0
 def insert_doses_for_regions(self, region, administrated_doses,
                              delivered_doses):
     sql = """
         INSERT INTO doses_for_regions
         (region, administrated_doses, delivered_doses)
         VALUES (?,?,?)    
     """
     analysis_data = [region, administrated_doses, delivered_doses]
     try:
         super().__init__(self.db_name)
         super().execute_sql(sql, analysis_data)
     except Exception as e:
         logger.error(f"Can not insert new data for region {region}  {e}")
         exit()
Ejemplo n.º 13
0
 def create_coronavirus_cases_table(self):
     sql_create_coronavirus_cases = """
         CREATE TABLE IF NOT EXISTS coronavirus_cases(
             date timestamp without time zone PRIMARY KEY,
             total_cases integer, 
             total_positive_cases integer,
             new_positive_cases integer,
             hospitalized integer);
     """
     try:
         super().__init__(self.db_name)
         super().execute_sql(sql_create_coronavirus_cases)
     except Exception as e:
         logger.error(f"Can not create table analysis {e}")
Ejemplo n.º 14
0
    def doses_analysis(self):
        try:
            req = requests.get(
                'https://raw.githubusercontent.com/italia/covid19-opendata-vaccini/master/dati/vaccini-summary-latest.json'
            )
            jsonResponse = req.json()

            for x in jsonResponse["data"]:
                self.insert_doses_for_regions(
                    region=x["nome_area"],
                    administrated_doses=x["dosi_somministrate"],
                    delivered_doses=x["dosi_consegnate"])
        except Exception as e:
            logger.error("Doses analysis failed", exc_info=True)
Ejemplo n.º 15
0
 def create_coronavirus_vacinnes_table(self):
     sql_create_vacinnes_cases = """
         CREATE TABLE IF NOT EXISTS coronavirus_vacinnes(
             age_range text PRIMARY KEY, 
             both_sexes integer,
             males integer,
             females integer,
             first_dose integer,
             second_dose integer);
     """
     try:
         super().__init__(self.db_name)
         super().execute_sql(sql_create_vacinnes_cases)
     except Exception as e:
         logger.error(f"Can not create table analysis {e}")
Ejemplo n.º 16
0
def update_router_device(router_device: RouterDevice, params):
    sess = DBSession()
    try:
        query = sess.query(RouterDevice)
        _filter = query.filter_by(ext_addr=router_device.ext_addr)
        if 'router_device_id' in params:
            _filter = _filter.filter(
                RouterDevice.router_device_id == params['router_id'])
        if 'ext_addr' in params:
            _filter = _filter.filter(
                RouterDevice.ext_addr == params['ext_addr'])
        if 'net_addr' in params:
            _filter = _filter.filter(
                RouterDevice.net_addr == params['net_addr'])
        if 'name' in params:
            _filter = _filter.filter(RouterDevice.name == params['name'])
        if 'status' in params:
            _filter = _filter.filter(RouterDevice.status == params['status'])
        rst = _filter.all()
        for device in rst:
            if router_device.ext_addr:
                device.ext_addr = router_device.ext_addr
            if router_device.net_addr:
                device.net_addr = router_device.net_addr
            if router_device.status:
                device.status = router_device.status
            if router_device.name:
                device.name = router_device.name
            if router_device.voltage:
                device.voltage = router_device.voltage
            if router_device.start_time:
                device.start_time = router_device.start_time
            if router_device.update_time:
                device.update_time = router_device.update_time
            if router_device.rssi:
                device.rssi = router_device.rssi
            if router_device.lqi:
                device.lqi = router_device.lqi
            if router_device.parent:
                device.parent = router_device.parent
        sess.commit()
    except Exception as e:
        logger.error(e)
        sess.rollback()
        logger.warning('update router_device failed %s' % router_device)
    sess.close()
    pass
Ejemplo n.º 17
0
 def insert_analysis_of_cases(self, date, total_cases, total_positive_cases,
                              new_positive_cases, hospitalized):
     sql = """
         INSERT INTO coronavirus_cases
         (date, total_cases, total_positive_cases, new_positive_cases, hospitalized)
         VALUES (?,?,?,?,?)    
     """
     analysis_data = [
         date, total_cases, total_positive_cases, new_positive_cases,
         hospitalized
     ]
     try:
         super().__init__(self.db_name)
         super().execute_sql(sql, analysis_data)
     except Exception as e:
         logger.error(f"Can not insert new data from date {date} {e}")
         exit()
Ejemplo n.º 18
0
 def insert_analysis_of_vacinnes(self, age_range, both_sexes, males,
                                 females, first_dose, second_dose):
     sql = """
         INSERT INTO coronavirus_vacinnes
         (age_range, both_sexes, males, females, first_dose, second_dose)
         VALUES (?,?,?,?,?,?)    
     """
     analysis_data = [
         age_range, both_sexes, males, females, first_dose, second_dose
     ]
     try:
         super().__init__(self.db_name)
         super().execute_sql(sql, analysis_data)
     except Exception as e:
         logger.error(
             f"Can not insert new data from age range {age_range} {e}")
         exit()
Ejemplo n.º 19
0
def ExecuteSQLs(sql, args=None):
    '''
        执行多条SQL语句, 正常执行返回影响的行数,出错返回Flase
    '''
    ret = 0
    try:
        conn = GetConnection()
        cur = conn.cursor()

        ret = cur.executemany(sql, args)
        conn.commit()
        cur.close()
        cur.close()
    except MySQLdb.Error as e:
        logger.error("ExecuteSQLs error: %s" % str(e))
        return False

    return ret
Ejemplo n.º 20
0
        def wrapper(**kwargs):
            tries = kwargs['tries']
            delay = kwargs['delay']
            db_name = kwargs['db_name']

            for i in range(int(tries)):
                logger.info(f"It is my {i} try to connect")
                conn = None
                if os.path.isfile(db_name):
                    try:
                        conn = sqlite3.connect(db_name)
                    except Exception as e:
                        logger.error(f"The {i} attempt failed", exc_info=True)
                        time.sleep(delay)
                    else:
                        logger.info("Connection established")
                    return conn
                else:
                    logger.error(f"No database {db_name}")
Ejemplo n.º 21
0
def QueryBySQL(sql, args=None, size=None):
    '''
        通过sql查询数据库,正常返回查询结果,否则返回None
    '''
    results = []
    try:
        conn = GetConnection()
        cur = conn.cursor(cursorclass=DictCursor)
        cur.execute('SET SESSION group_concat_max_len=1024000')
        cur.execute(sql, args)
        rs = cur.fetchall()
        for row in rs:
            results.append(row)
    except MySQLdb.Error as e:
        logger.error("QueryBySQL error: %s" % str(e))
        return None
    finally:
        cur.close()
        # conn.close()

    return results
Ejemplo n.º 22
0
def GetHtml(url):
    config = my_config.GetConfig()
    cookie = config['remote_server']['cookie']
    html = ""
    request = urllib2.Request(
        url,
        headers={
            'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0 Win64 x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36'
        })
    request.add_header("Cookie", cookie)
    while (True):
        try:
            response = urllib2.urlopen(request, timeout=60)
            html = response.read()
            break
        except urllib2.HTTPError, e:
            traceback.print_stack()
            logger.info(e.code)
            logger.error(e.reason)
            logger.error(traceback)
Ejemplo n.º 23
0
 def run(self):
     while True:
         try:
             send_data = serial_out_msg_queue.get()
             data = send_data['data']
             self.client.send_data(data)
             try:
                 rep = serial_rep_msg_queue.get(timeout=2)
                 if rep == SUCCESS:
                     logger.info('rep success,data = %s' % data)
                     pass
                 if rep == FAILED:
                     logger.warning('rep failed,data =  %s' % data)
                     pass
             except Empty as e:
                 logger.warning('rep timeout,data =  %s' % data)
                 pass
         except Exception as e:
             time.sleep(5)
             logger.error(traceback.format_exc())
     pass
Ejemplo n.º 24
0
def msg_handler():
    while True:
        try:

            _msg = msg_queue.get()
            logger.debug('msg coming ')
            t = _msg.cmd_state1 & TYPE_MASK
            if t == SRSP and len(_msg.data):
                if _msg.data[0] == SUCCESS or _msg.data[0] == FAILED:
                    serial_rep_msg_queue.put(_msg.data[0])
            subsystem = _msg.cmd_state1 & SUB_SYSTEM_MASK
            if subsystem == SYS:
                mt_sys_handler(_msg)
                continue
            if subsystem == APP:
                mt_app_handler(_msg)
                continue
            logger.debug('no msg handler: %s' % _msg)
        except Exception as e:
            time.sleep(5)
            logger.error('msg handler error %s' % traceback.format_exc())
    pass
Ejemplo n.º 25
0
def add_end_device(ed: EndDevice) -> EndDevice:
    session = DBSession()
    try:

        ed.end_device_id = my_uuid()
        max_code = session.query(EndDevice).filter(
            EndDevice.code < 100).order_by(desc(EndDevice.code)).first().code
        ed.code = max_code + 1
        ed.start_time = datetime.datetime.now()
        ed.status = 0
        # ed.voltage = 0
        # ed.hum_freq = 0
        # ed.temp_freq = 0
        # ed.name = 'test'
        # ed.net_addr = '0000'
        session.add(ed)
        session.commit()
    except Exception as e:
        session.rollback()
        logger.error(e)
        logger.error('add end device fialed %s', ed)
        session.close()
    return ed
Ejemplo n.º 26
0
def SendEMail(files2mail):
    config = my_config.GetConfig()
    # 第三方 SMTP 服务
    mail_host = "smtp.qq.com"  # 设置服务器
    sender = config['email']['sender']
    mail_user = config['email']['mail_user']  # 用户名
    mail_pass = config['email']['mail_pass']  # 口令

    receivers = []
    for recv in config['email']['receivers']:
        receivers.append(recv['qq'])

    msg = MIMEMultipart()
    msg['From'] = formataddr([u'Chant', sender])
    msg['To'] = formataddr([u'Nancy', receivers])
    msg['Subject'] = Header(config['email']['topic'], 'utf-8').encode()
    msg.attach(MIMEText(config['email']['text'], 'plain', 'utf-8'))

    for file in files2mail:
        # 添加附件
        att1 = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8')
        att1["Content-Type"] = 'application/octet-stream'
        att1.add_header('Content-Disposition',
                        'attachment',
                        filename=Header(file, 'utf-8').encode())  # 防止中文附件名称乱码
        msg.attach(att1)

    try:
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
        smtpObj.login(mail_user, mail_pass)
        smtpObj.set_debuglevel(1)
        smtpObj.sendmail(sender, receivers, msg.as_string())
        smtpObj.quit()
        logger.info("Send EMail OK")
    except smtplib.SMTPException as e:
        logger.error("Send EMail Error: " + e)
Ejemplo n.º 27
0
def update_end_device(end_device: EndDevice, params):
    sess = DBSession()
    try:
        query = sess.query(EndDevice)
        _filter = query.filter_by(ext_addr=end_device.ext_addr)
        if 'end_device_id' in params:
            _filter = _filter.filter(
                EndDevice.end_device_id == params['end_device_id'])
        if 'ext_addr' in params:
            _filter = _filter.filter(EndDevice.ext_addr == params['ext_addr'])
        if 'net_addr' in params:
            _filter = _filter.filter(EndDevice.net_addr == params['net_addr'])
        if 'name' in params:
            _filter = _filter.filter(EndDevice.name == params['name'])
        if 'status' in params:
            _filter = _filter.filter(EndDevice.status == params['status'])
        if 'hum_freq' in params:
            _filter = _filter.filter(EndDevice.hum_freq == params['hum_freq'])
        if 'temp_freq' in params:
            _filter = _filter.filter(
                EndDevice.temp_freq == params['temp_freq'])
        rst = _filter.all()
        for device in rst:
            if end_device.ext_addr:
                device.ext_addr = end_device.ext_addr
            if end_device.net_addr:
                device.net_addr = end_device.net_addr
            if end_device.status:
                device.status = end_device.status
            if end_device.name:
                device.name = end_device.name
            if end_device.voltage:
                device.voltage = end_device.voltage
            if end_device.start_time:
                device.start_time = end_device.start_time
            if end_device.hum_freq:
                device.hum_freq = end_device.hum_freq
            if end_device.temp_freq:
                device.temp_freq = end_device.temp_freq
            if end_device.hum:
                device.hum = end_device.hum
            if end_device.temp:
                device.temp = end_device.temp
            if end_device.update_time:
                device.update_time = end_device.update_time
            if end_device.rssi:
                device.rssi = end_device.rssi
            if end_device.lqi:
                device.lqi = end_device.lqi
            if end_device.pv:
                device.pv = end_device.pv
            if end_device.time_window:
                device.time_window = end_device.time_window
            if end_device.parent:
                device.parent = end_device.parent
            if end_device.type:
                device.type = end_device.type
        sess.commit()
    except Exception as e:
        logger.error(e)
        sess.rollback()
        logger.warning('update end_device failed %s' % end_device)
    sess.close()
    pass
Ejemplo n.º 28
0
 def handle_error(self, request, client_address):
     logger.error(request, client_address)
     pass
Ejemplo n.º 29
0
def timeout_timer():
    global state
    state = SOP_STATE
    logger.error('time out')
Ejemplo n.º 30
0
 def run(self):
     self.timer = self.init_timer()
     while True:
         try:
             for _ in range(self.msg_len):
                 self.msg.append(serial_in_msg_queue.get())
             if self.state == SOP_STATE:
                 char = self.get_one_msg(self.idx)
                 if char == MT_UART_SOF:
                     self.state = LEN_STATE
                     self.msg_len = 1
                     # if not self.timer._is_stopped:
                     #     self.timer = self.init_timer()
                     # self.timer.start()
             elif self.state == LEN_STATE:
                 char = self.get_one_msg(self.idx)
                 self.data['len'] = char
                 self.state = CMD_STATE1
                 self.msg_len = 1
             elif self.state == CMD_STATE1:
                 char = self.get_one_msg(self.idx)
                 self.data['cmd_state1'] = char
                 self.state = CMD_STATE2
                 self.msg_len = 1
             elif self.state == CMD_STATE2:
                 char = self.get_one_msg(self.idx)
                 self.data['cmd_state2'] = char
                 if self.data['len'] == 0:
                     self.state = FCS_STATE
                     self.msg_len = 1
                 else:
                     self.state = DATA_STATE
                     self.msg_len = self.data['len']
             elif self.state == DATA_STATE:
                 chs = self.msg[self.idx - self.data['len'] + 1:self.idx +
                                1]
                 for b in chs:
                     self.data['data'] += b
                 self.state = FCS_STATE
                 self.msg_len = 1
                 pass
             elif self.state == FCS_STATE:
                 char = self.get_one_msg(self.idx)
                 fcs = self.cal_fcs()
                 if fcs == char:
                     mt_msg = Msg(self.data)
                     msg_queue.put(mt_msg)
                     logger.info('add msg : %s' % mt_msg)
                 else:
                     logger.warning('fcs failed')
                     # self.msg = self.msg[1:]
                 self.msg = self.msg[self.idx + 1:]
                 self.timer.cancel()
                 # self.timer = self.init_timer()
                 self.state = SOP_STATE
                 self.init_msg()
                 self.idx = -1
                 self.msg_len = 1
             self.idx += self.msg_len
         except Exception as e:
             time.sleep(5)
             logger.error(e)
             pass
     pass