def __del__(self): try: if self.connok == False: raise AssertionError("HttpClient connection not ready.") self.conn.close() except Exception, e: logger.errLog("HttpClient del failed, des:", e)
def __init__(self, xmlfile): try: self.connok = False config = ConfigParse(xmlfile) isssl = config.getValue('config.http.isssl') host = config.getValue('config.http.host') port = config.getValue('config.http.port') #capath = config.getValue('config.http.') #certpath = config.getValue('config.http.') #keypath = config.getValue('config.http.') ### set header self.header = { "Content-Type": "application/json", "Accept-Type": "application/json" } if isssl == 'false': self.conn = httplib.HTTPConnection(host, int(port)) else: self.conn = httplib.HTTPSConnection(host, int(port)) #self.conn._context.check_hostname = False #self.conn._context.verify_mode = ssl.CERT_NONE self.connok = True except Exception, e: #traceback.print_exc() logger.errLog("HttpClient init failed, des:", e)
def __del__(self): try: if self.dbok == False: raise AssertionError("PgClient connection not ready.") self.conn.close() logger.debugLog('close connection to pg') except Exception,e: logger.errLog('PgClient del error, cause: ', e)
def query(self, sql): try: if self.dbok == False: raise AssertionError("PgClient connection not ready.") self.cursor.execute(sql) rows = self.cursor.fetchall() return rows except Exception, e: logger.errLog("query sql failed cause:", e, " sql:", sql) return []
def exc(self, sql, isCommit=True): try: if self.dbok == False: raise AssertionError("PgClient connection not ready.") self.cursor.execute(sql) if isCommit: self.conn.commit() return True except Exception, e: logger.errLog("execute sql failed cause:", e, " sql:", sql) return False
def cancelExe(self): """ rollback since last commit() """ try: if self.dbok == False: raise AssertionError("PgClient connection not ready.") self.conn.rollback() return True except Exception, e: logger.errLog("cancel execute sql failed cause:", e) return False
def __init__(self, xmlfile): try: self.dbok = False config = ConfigParse(xmlfile) dbuser = config.getValue('config.pg.user') dbpasswd = config.getValue('config.pg.passwd') db = config.getValue('config.pg.db') dbhost = config.getValue('config.pg.host') dbport = config.getValue('config.pg.port') self.conn = psycopg2.connect(database=db, user=dbuser, password=dbpasswd, host=dbhost, port=dbport) self.cursor = self.conn.cursor() self.dbok = True logger.debugLog('connect pg succeed') except Exception,e: #traceback.print_exc() logger.errLog('PgClient init error, cause: ', e)
def sendRequest(self, method, url, body=None, head=None): """ :param method, support POST, DELETE, PUT, GET """ try: logger.errLog("----in:", time.ctime()) if self.connok == False: raise AssertionError("HttpClient connection not ready.") logger.debugLog("httpclient.request", url) if head == None: self.conn.request(method, url, body, self.header) else: newheader = dict(self.header, **head) self.conn.request(method, url, body, newheader) except Exception, e: logger.errLog("send request excption cause: ", e, " url:", url) return None
logger.errLog("HttpClient del failed, des:", e) def sendRequest(self, method, url, body=None, head=None): """ :param method, support POST, DELETE, PUT, GET """ try: logger.errLog("----in:", time.ctime()) if self.connok == False: raise AssertionError("HttpClient connection not ready.") logger.debugLog("httpclient.request", url) if head == None: self.conn.request(method, url, body, self.header) else: newheader = dict(self.header, **head) self.conn.request(method, url, body, newheader) except Exception, e: logger.errLog("send request excption cause: ", e, " url:", url) return None else: res = self.conn.getresponse() if res.status == 200: #res is HTTPResponse obj logger.errLog("----outok:", time.ctime()) return res else: logger.errLog('send request failed, cause recv: ', res.status, res.read()) return None