class SendHttpRequest: def __init__(self): reload(sys) sys.setdefaultencoding('utf-8') self.version_name = apiset().getConfigValues("version", "version_name") self.header = { "Accept": "application/json", "versionName": self.version_name } def postByparam(self, url, value, headers=None, timeout=3): if headers == None: headers = self.header params = urllib.urlencode(value) try: req = requests.post(url + "?%s" % params, headers=headers, timeout=timeout) except Exception, err: log().error("接口" + url + "请求失败:" + err.message) print err.message if req.status_code == 200: log().info(u"发送post请求: %s 服务器返回: %s" % (req.url, req.status_code)) else: log().error(u"发送post请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text)) return req.json()
def get(self, url, value=None, headers=None, timeout=3): if headers == None: headers = self.header try: req = requests.get(url, params=value, headers=headers, timeout=timeout) except Exception, err: log().error("接口" + url + "请求失败:" + err.message) print err.message
def postByData(self, url, value=None, headers=None, timeout=3): if headers == None: headers = self.header try: req = requests.post(url, data=value, headers=headers, timeout=timeout) print req.url except Exception, err: log().error("接口" + url + "请求失败:" + err.message) print err.message
def postByparam(self, url, value, headers=None, timeout=3): if headers == None: headers = self.header params = urllib.urlencode(value) try: req = requests.post(url + "?%s" % params, headers=headers, timeout=timeout) except Exception, err: log().error("接口" + url + "请求失败:" + err.message) print err.message
def sortReportFile(report_path): new_report_list = [] try: for path in report_path: lists = os.listdir(path) lists.sort(key=lambda fn: os.path.getmtime(path + "/" + fn) if not os.path.isdir(path + "/" + fn) else 0) new_report = os.path.join(path, lists[-1]) new_report_list.append(new_report) return new_report_list except IndexError, e: log().info("sort_report_file - There are no reports to send:") log().error(e)
def execMysql(self, database, sql): self.database = database try: conn = pymysql.connect( host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.database, ) cur = conn.cursor() print cur if cur: log().info(u"执行SQL语句|%s|" % sql) cur.execute(sql) result = cur.fetchall() # return result return self.convertToList(result) except Exception, e: log().error(e)
def execMssql(self, database, sql): self.database = database try: conn = pymssql.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.database, charset="utf8") cur = conn.cursor() if cur: log().info(u"执行SQL语句|%s|" % sql) cur.execute(sql) rows = cur.fetchall() if len(rows) == 0: log().warning(u"没有查询到数据") return rows else: log().error(u"数据库连接不成功") conn.close() except Exception, e: log().error(e.message)
def json_to_xml(self): try: root = {"root": self.json_value} xml = xmltodict.unparse(root, pretty=True) except ArithmeticError, e: log().error(e)
def __init__(self, json_value): try: self.json_value = json.loads(json_value) except ValueError, e: log().error(e)
log().info(u"发送post请求: %s 服务器返回: %s" % (req.url, req.status_code)) else: log().error(u"发送post请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text)) return req.json() def postByJson(self, url, value=None, headers=None, timeout=3): if headers == None: headers = self.header try: req = requests.post(url, json=value, headers=headers, timeout=timeout) print req.url except Exception, err: log().error("接口" + url + "请求失败:" + err.message) print err.message if req.status_code == 200: log().info(u"发送post请求: %s 服务器返回: %s" % (req.url, req.status_code)) return req.json() else: log().error(u"发送post请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text)) print req.text def postByData(self, url, value=None, headers=None, timeout=3): if headers == None: headers = self.header try: req = requests.post(url, data=value, headers=headers, timeout=timeout) print req.url except Exception, err: log().error("接口" + url + "请求失败:" + err.message) print err.message if req.status_code == 200: