def __init__(self, host, user, pwd, dbname=None, port=3306, charset='utf8'): ''' 初始化函数,初始化连接列表 :param host:数据库服务器地址 :param port数据库服务器端口号 :param user:数据库用户名 :param pwd:用户的密码 :param dbname:要操作的库名称 ''' self.host = host self.user = user self.passwd = pwd self.db = dbname self.port = port self.charset = charset try: # 建立数据库连接 self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db, charset=self.charset) except ConnectionError as e: logger.debug("Mysql Error %d: %s" % (e.args[0], e.args[1])) # 创建游标对象:用于执行查询和获取结果 self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
async def watch(): await watcher.setup(loop) while True: event = await watcher.get_event() logger.debug(f"Captured event {event}") with open(os.path.join(event.alias, event.name)) as f: user_code = f.read() incoming = new_in_msg("1:python-1", user_code) checker = checkers[incoming["task_id"]] checker.delay(incoming)
def send(self, params=None, data=None, **kwargs): response = self.session.request(method=self.method, url=self.url, params=params, data=data, **kwargs) response.encoding = 'utf-8' logger.debug('{0} {1}'.format(self.method, self.url)) logger.debug('请求成功: {0}\n{1}'.format(response, response.text)) return response
def click_btn_by_class(id_class=None): """ Recebe "classe" por parametro. Clica em um botão com "class" recebida por parametro. """ try: if id_class is None: logger.critical("id_class não informado") else: logger.debug("Clicando no botão by classe: {}".format(id_class)) time.sleep(1) driver.find_element_by_class_name(id_class).click() except Exception as error: logger.error("Erro ao clicar no botão by classe: {}".format(id_class)) logger.error(error)
def click_btn_by_id(id=None): """ Recebe "id" por parametro. Clica em um botão com "id" recebido por parametro. """ try: if id is None: logger.critical("id não informado") else: logger.debug("Clicando no botao by id: {}".format(id)) time.sleep(1) driver.find_element_by_id(id).click() except Exception as error: logger.error("Erro ao clicar no botão by id: {}".format(id)) logger.error(error)
def write_ipt_by_id(id=None, text=None): """ Recebe "id" por parametro e o texto a ser escrito no campo. Escreve o "text" em um input com "id" recebido por parametro. """ try: if id is None or text is None: logger.critical("id ou text não informado") else: logger.debug("Escrevendo no campo by id: {}".format(id)) time.sleep(1) driver.find_element_by_id(id).send_keys(text) except Exception as error: logger.error("Erro ao Escrevendo no campo by xpath: {}".format(id)) logger.error(error)
def create_driver(driver_path,driver_link): """ Recebe o path do driver e o link por parametro Alem de criar o driver já acessa o link enviado retornando um driver pronto para uso """ try: logger.debug("Criando driver...") driver = webdriver.Chrome(driver_path) driver.get(driver_link) driver.set_window_size(720,680) driver.set_window_position(600,150) return driver except Exception as error: logger.critical("Erro ao criar driver") logger.critical(error)
def login(): """ Responsavel por realizar login na app """ try: init() logger.debug("Music: Realizando login") fw.click_btn_by_xpath(INITIAL_BT_XPATH) fw.write_ipt_by_xpath(LG_USERNAME_IPT_XPATH, USERNAME) fw.click_btn_by_id(LG_USERNAME_BTN_ID) fw.write_ipt_by_xpath(LG_PASSWORD_IPT_XPATH, PASSWORD) fw.click_btn_by_id(LG_PASSWORD_BTN_ID) except Exception as error: logger.error("Music - Erro ao realizar login") logger.error(error)
def click_btn_by_xpath(xpath=None): """ Recebe "xpath" por parametro. Clica em um botão com "xpath" recebido por parametro. """ try: if xpath is None: logger.critical("id não informado") else: logger.debug("Clicando no botão by xpath: {}".format(xpath)) time.sleep(1) driver.find_element_by_xpath(xpath).click() except Exception as error: print(error) logger.error("Erro ao clicar no botão by xpath: {}".format(xpath)) logger.error(error)
def write_ipt_by_xpath(xpath=None, text=None): """ Recebe "xpath" por parametro e o "texto" a ser escrito no campo. Escreve o "text" em um input com "xpath" recebido por parametro. """ try: if xpath is None or text is None: logger.critical("Xpath ou text não informado") return else: logger.debug("Escrevendo no campo by id: {}".format(xpath)) time.sleep(1) driver.find_element_by_xpath(xpath).send_keys(text) except Exception as error: logger.error("Erro ao Escrevendo no campo by xpath: {}".format(xpath)) logger.error(error)
def check_exist_by_xpath(xpath=None): """ Checa se um elemento existe a partir do "xpath" recebido por parametro """ try: if xpath is None: logger.critical("xpath não informado") return else: logger.debug("Procurando elemento by xpath") if (driver.find_element_by_xpath(xpath).size() != 0): return True else: logger.error("Elemento não existe ou não está visivel") return False except Exception as error: logger.error("Erro ao procurar elemento by path") logger.error(error)
def set_vars(): """ Armazena os dados de configuração em um dict. Após armazenar retorna o dict. """ logger.debug( 'Armazenando as o conteudo do arquivo de configuração em variaveis.') open_file() data_json = open_file() data = { "driver_path": os.path.join(home, data_json['driver']['driver_path']), "driver_link": data_json['driver']['driver_link'], "user_username": data_json['user']['user_username'], "user_password": data_json['user']['user_password'], "conf_path_before": data_json['conf_path']['conf_path_before'], "conf_path_after": data_json['conf_path']['conf_path_after'], "conf_path_erro": data_json['conf_path']['conf_path_erro'] } return data
def open_file(): """ Abre o arquivo de propriedades """ logger.debug('Abrindo arquivo de configuração.') try: arquivo = open( home + "\\Projetos\\auto_music\\config\\properties_driver.json").read() if (os.path.exists(os.path.abspath(arquivo))): logger.critical( 'Arquivo de configuração não existe ou está com nome "diferente de properties_driver.json".' ) sys.exit(1) except Exception as error: logger.critical('Erro ao abrir arquivo de configuração') logger.error(error) sys.exit(1) data_json = json.loads(arquivo) return data_json
def send(self, params=None, data=None, **kwargs): response = self.session.request(method=self.method, url=self.url, params=params, data=data, **kwargs) response.encoding = 'utf-8' logger.debug('params:{0}, data:{1}'.format(params, data)) logger.debug('{0} {1}'.format(self.method, self.url)) if response.status_code == requests.codes.ok: logger.debug('请求成功: {0}\n{1}'.format(response, response.text)) else: logger.debug('请求失败:{0}\n{1}'.format(response, response.text)) response.raise_for_status() return response
def sendbyurl(self, params): """get请求时将请求参数拼接成请求地址的一部分""" url = "%s%s" % (self.url, params) response = self.session.get(url) logger.debug('请求方式:GET,请求地址: {0}'.format(url)) if response.status_code == requests.codes.ok: logger.debug('请求成功: {0}\n{1}'.format(response, response.text)) else: logger.debug('请求失败:{0}\n{1}'.format(response, response.text)) response.raise_for_status() return response
def test_baidu_http(self): res = self.client.send() logger.debug(res.text) assertHTTPCode(res, [400]) self.assertIn('百度一下,你就知道', res.text)
def generate_check_result_file(file): logger.debug(f"writing file {file}") f = open(file, "w") f.write(json.dumps(check_result)) f.close()
def check_code(incoming): logger.debug(incoming) try: return check_script(incoming) except SoftTimeLimitExceeded as e: return new_out_msg(incoming["task_id"], 1, str(e))
def login(): init() logger.debug('teste') print("login discord")