def get_config(firstLine=None, secondLine=None): config = configparser.ConfigParser() # 读取配置文件信息 config.read('./config.conf', encoding="utf-8") try: return config[firstLine][secondLine] except Exception as e: Pylog.error(e)
def get_connect(self, db): try: Pylog.info("开始连接数据库...") conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.pwd, db=db) cur = conn.cursor() return [cur, conn] except Exception as err: Pylog.error('mysql连接错误:' + str(err))
def inquire_data(self, sql, db): datas = [] acon = self.get_connect(db) cur = acon[0] conn = acon[1] try: # 执行SQL语句 cur.execute(sql) # 获取所有记录列表 results = cur.fetchall() for row in results: datas.append(str(row[0])) cur.close() conn.close() Pylog.info("关闭数据库连接...") return datas except Exception as e: Pylog.error("查询错误 " + str(e))
def upload(self, filename=None): Pylog.info(filename + ": 开始上载") try: src = config.get_config("upload", "src") savefile = config.get_config("upload", "savefile") savename = re.findall('E:/OtherFile/(.*)', src, re.S)[0] idfile = savefile + savename + '.csv' url = "http://img.will888.cn/photo/upload" files = {'pic': open(filename, 'rb')} self.reps = self.httpRequest.upload(url=url, files=files) Pylog.debug("Response:" + self.reps.text) pic = json.loads(self.reps.content) #CSV写入 csvfile = open(idfile, 'a', encoding='utf8', newline='') writer = csv.writer(csvfile) picId = pic["picid"] picname = re.findall(src + '/(.*)', filename, re.S) writer.writerow([picname[0], picId]) csvfile.close() except Exception as e: Pylog.error(e)
def upload(self, url, files): try: resp = self.session.post(url=url, files=files) return resp except Exception as e: Pylog.error(e)
def post(self, url, data): try: resp = self.session.post(url=url, data=data, headers=self.headers) return resp except Exception as e: Pylog.error(e)
def get(self, url, data=None): try: resp = self.session.get(url=url, params=data, headers=self.headers) return resp except Exception as e: Pylog.error(e)