class SendHttpRequest(object): def __init__(self, path, domain=request_address): ''' :param path: 接口请求路径 :param domain: 接口服务器地址 ''' self.url = urljoin(request_address, path) def post(self, *args, **kwargs): ''' :param args: 追加的路径 :param kwargs: 以多的参数的形式传入 :return: http请求内容 ''' params = urllib.urlencode(kwargs) up_url = urljoin(self.url, '/'.join(args)) req_url = up_url + '?' + params try: req = requests.post(req_url, timeout=10) except Exception, err: pyapilog().error(err) if req.status_code == 200: pyapilog().info(u"发送post请求: %s 服务器返回: %s\n" % (req.url, req.status_code)) try: result = JSONEncoder().encode(req.json()) except ValueError: result = req.text return result else: pyapilog().error(u"发送post请求: %s 服务器返回: %s\n" % (req.url, req.status_code))
def json_to_xml(self): try: root = {'xml': self.json_value} xml = xmltodict.unparse(root, pretty=True) except ArithmeticError as e: pyapilog().error(e) return xml
def get(self, value=None): try: req = requests.get(self.url, params=value) except Exception as err: print(err) if req.status_code == 200: pyapilog().info(u"发送get请求: %s 服务器返回: %s" % (req.url, req.status_code)) else: pyapilog().error(u"发送get请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text)) return req.text
def post(self, value=None): params = urllib.urlencode(value) try: req = requests.post(self.url + "?%s" % params) except Exception as err: print(err) if req.status_code == 200: pyapilog().info(u"发送post请求: %s 服务器返回: %s" % (req.url, req.status_code)) else: pyapilog().error(u"发送post请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text)) return req.text
def exec_mysql(self, sql): try: conn = MySQLdb.connect( host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.database ) cur = conn.cursor() if cur: pyapilog().info(u"执行SQL语句|%s|" % sql) resList = cur.execute(sql) return resList except Exception, e: pyapilog().error(e)
def post_json(self, value): #head = {'content-type': 'application/json'} try: #req = requests.post(self.url, data=json.dumps(value), headers=head) req = requests.post(self.url, data=value) #print(req.url) except Exception as err: print(err) if req.status_code == 200: pyapilog().info(u"发送post请求: %s 服务器返回: %s" % (req.url, req.status_code)) return req.text else: pyapilog().error(u"发送post请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text))
def post(self, *args, **kwargs): ''' :param args: 追加的路径 :param kwargs: 以多的参数的形式传入 :return: http请求内容 ''' params = urllib.urlencode(kwargs) up_url = urljoin(self.url, '/'.join(args)) req_url = up_url + '?' + params try: req = requests.post(req_url, timeout=10) except Exception, err: pyapilog().error(err)
def exec_mysql(self, sql): try: conn = MySQLdb.connect(host=self.host, port=self.port, user=self.user, passwd=self.password, db=self.database, ) cur = conn.cursor() if cur: pyapilog().info(u"执行SQL语句|%s|" % sql) resList = cur.execute(sql) return resList except Exception, e: pyapilog().error(e)
def execsql(sql): config = settings.DATABASE driver = config.get("ENGINE") host = config.get("HOST") port = config.get("PORT") user = config.get("USER") password = config.get("PWD") database = config.get("DATABASE") if driver == "MSSQL": try: sql_result = sqldriver(host=host, port=port, user=user, password=password, database=database).exec_mysql( sql ) return sql_result except Exception, e: pyapilog().error(e)
class SendHttpRequest(object): def __init__(self, url): self.url = url # post request def post(self, value=None): params = urllib.urlencode(value) try: req = requests.post(self.url + "?%s" % params) except Exception, err: print err if req.status_code == 200: pyapilog().info(u"发送post请求: %s 服务器返回: %s" % (req.url, req.status_code)) else: pyapilog().error(u"发送post请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text)) return req.text
def execsql(sql): config = settings.DATABASE driver = config.get("ENGINE") host = config.get("HOST") port = config.get("PORT") user = config.get("USER") password = config.get("PWD") database = config.get("DATABASE") if driver == "MSSQL": try: sql_result = sqldriver(host=host, port=port, user=user, password=password, database=database).exec_mysql(sql) return sql_result except Exception, e: pyapilog().error(e)
def sitechSend(self, subject, content, files=[]): try: fro = self.login_user msg = MIMEMultipart() msg['From'] = fro msg['Subject'] = subject msg['To'] = COMMASPACE.join(self.to_list) msg['Date'] = formatdate(localtime=True) msg.attach(MIMEText(content, _charset='utf-8')) for f in files: part = MIMEBase('application', 'octet-stream') part.set_payload(open(f, 'rb').read()) encoders.encode_base64(part) part.add_header( 'Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f).encode("utf-8")) msg.attach(part) server = new_smtplib.SMTP(self.mail_host, self.mail_port) server.set_debuglevel(0) server.login(self.login_user, self.login_pwd) server.sendmail(fro, self.to_list, msg.as_string()) server.quit() pyapilog().info(u'哦了') except Exception, e: pyapilog().error(u'这有毛病') pyapilog().exception("Exception Logged")
def exec_mssql(self, sql): 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: pyapilog().info(u"执行SQL语句|%s|" % sql) cur.execute(sql) rows = cur.fetchall() if len(rows) == 0: pyapilog().warning(u"没有查询到数据") return rows else: pyapilog().error(u"数据库连接不成功") conn.close() except Exception, e: pyapilog().error(e)
def exec_mssql(self, sql): 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: pyapilog().info(u"执行SQL语句|%s|" % sql) cur.execute(sql) rows = cur.fetchall() if len(rows) == 0: pyapilog().warning(u"没有查询到数据") return rows else: pyapilog().error(u"数据库连接不成功") conn.close() except Exception, e: pyapilog().error(e)
def moblieSend(self, subject, content, files=[]): try: fro = self.login_user server = { 'name': self.mail_host, 'user': self.login_user, 'passwd': self.login_pwd, 'port': self.mail_port } msg = MIMEMultipart() msg['From'] = fro msg['Subject'] = subject msg['To'] = COMMASPACE.join(self.to_list) msg['Date'] = formatdate(localtime=True) msg.attach(MIMEText(content, 'html', _charset='utf8')) #msg.attach(MIMEText(content, _charset='utf-8')) for f in files: if f == None: pass else: part = MIMEBase('application', 'octet-stream') part.set_payload(open(f, 'rb').read()) encoders.encode_base64(part) part.add_header( 'Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f).encode("utf8")) msg.attach(part) if self.encrypt == 1: smtp = smtplib.SMTP(server['name'], server['port']) smtp.starttls() smtp.ehlo() smtp.login(server['user'], server['passwd']) smtp.sendmail(fro, self.to_list, msg.as_string()) smtp.close() else: smtp = smtplib.SMTP(server['name'], server['port']) smtp.login(server['user'], server['passwd']) smtp.sendmail(fro, self.to_list, msg.as_string()) smtp.close() pyapilog().info( u'------------------------------Óʼþ·¢Ëͳɹ¦------------------------------' ) except Exception, e: pyapilog().error( u'------------------------------Óʼþ·¢ËÍʧ°Ü------------------------------' ) pyapilog().exception("Exception Logged")
else: pyapilog().error(u"发送post请求: %s 服务器返回: %s\n" % (req.url, req.status_code)) def post_json(self, value): ''' :param value: json值 :return: 请求内容 ''' head = {'content-type': 'application/json'} try: req = requests.post(self.url, data=json.dumps(value), headers=head, timeout=10) except Exception, err: print err if req.status_code == 200: pyapilog().info(u"发送post请求: %s 服务器返回: %s\n" % (req.url, req.status_code)) return req.text else: pyapilog().error(u"发送post请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text)) def get(self, *args, **kwargs): ''' :param args: 追加的路径 :param kwargs: 以多的参数的形式传入 :return: http请求内容 ''' params = urllib.urlencode(kwargs) up_url = urljoin(self.url, '/'.join(args)) req_url = up_url + '?' + params try: req = requests.get(req_url, timeout=10)
driver = config.get("ENGINE") host = config.get("HOST") port = config.get("PORT") user = config.get("USER") password = config.get("PWD") database = config.get("DATABASE") if driver == "MSSQL": try: sql_result = sqldriver(host=host, port=port, user=user, password=password, database=database).exec_mysql( sql ) return sql_result except Exception, e: pyapilog().error(e) else: pyapilog().error(u"[%s]数据库配置支持MYSQL、MSSQL、ORACLE" % driver) """ elif driver == "MYSQL": try: sql_result = sqldriver( host=host, port=port, user=user, password=password, database=database ).exec_mssql(sql) return sql_result except Exception, e: pyapilog().error(e) elif driver == "ORACLE": try:
def post_json(self, value): ''' :param value: json值 :return: 请求内容 ''' head = {'content-type': 'application/json'} try: req = requests.post(self.url, data=json.dumps(value), headers=head, timeout=10) except Exception, err: print err if req.status_code == 200: pyapilog().info(u"发送post请求: %s 服务器返回: %s\n" % (req.url, req.status_code)) return req.text else: pyapilog().error(u"发送post请求: %s 服务器返回: %s\n error info: %s " % (req.url, req.status_code, req.text)) def get(self, *args, **kwargs): ''' :param args: 追加的路径 :param kwargs: 以多的参数的形式传入 :return: http请求内容 ''' params = urllib.urlencode(kwargs) up_url = urljoin(self.url, '/'.join(args)) req_url = up_url + '?' + params try:
def __init__(self, json_value): try: self.json_value = json.loads(json_value) except ValueError as e: pyapilog().error(e)
user = config.get("USER") password = config.get("PWD") database = config.get("DATABASE") if driver == "MYSQL": try: sql_result = sqldriver( host=host, port=port, user=user, password=password, database=database ).exec_mysql(sql) return sql_result except Exception, e: pyapilog().error(e) elif driver == "MSSQL": try: sql_result = sqldriver( host=host, port=port, user=user, password=password, database=database ).exec_mssql(sql) return sql_result except Exception, e: pyapilog().error(e) else: pyapilog().error(u"[%s]数据库配置支持MYSQL、MSSQL、ORACLE" % driver)
def __init__(self, json_value): try: self.json_value = json.loads(json_value) except TypeError, e: pyapilog().error(u'%s不是json字符串' % json_value)