flask 封装
#!/bin/env python
# _*_coding:utf-8_*_
#!!!!!!!!!!!!describe:this script shoud install python-devel and pip install MySQL-python
import subprocess
import traceback
import sys
from flask import Flask
from flask import request
import threading
import MySQLdb
from datetime import datetime
import time
app = Flask(__name__)
def mysqlCmd(version,subversion,sid,event_type,extra_info):
if subversion:
subversion = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(subversion) / 1000))
event_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
event_time = str(event_time).replace("\"", "").replace("\'","")
conn=MySQLdb.connect(host='fof2.0.mysql.com',port=3306,db='py_crontab',user='py_fof',passwd='8Lakw26x',charset='utf8')
cs1=conn.cursor()
cs1.execute( '''insert into py_script_result_log(version,subversion,sid,event_type,extra_info,event_time)
values(%s,%s,%s,%s,%s,%s)''',(version,subversion,sid,event_type,extra_info,event_time))
conn.commit()
cs1.close()
conn.close()
def runCmd(cmd):
res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sout,serr = res.communicate()
#print (sout,serr)
#return res.returncode, sout, serr, res.pid
return sout, serr
def main(script,version,subversion,sid,event_type,extra_info=None):
mysqlCmd(version, subversion, sid, event_type, extra_info)
sout, serr = runCmd(script)
if len(serr.strip()) != 0:
event_type = 3
serr = str(serr).replace("\"", "").replace("\'","")
extra_info = "%s"%(serr)
mysqlCmd(version,subversion,sid,event_type,extra_info)
else:
event_type = 2
mysqlCmd(version,subversion,sid,event_type,extra_info)
@app.route('/py_fof_2_1.py_fof_fund_company_detail_daily_2_1.sh',methods={"POST","GET"})
def index1():
script = 'sh py_fof_2_1.py_fof_fund_company_detail_daily_2_1.sh'
#获取变量
version = request.values.get("version")
try:
subversion = request.values.get("subversion")
except:
subversion = None
sid = request.values.get("sid")
res = program_start(script, version, subversion, sid)
return res
@app.route('/py_fof_fund_manager_detail_daily_2_1.sh',methods={"POST","GET"})
def index2():
script = 'sh py_fof_fund_manager_detail_daily_2_1.sh'
version = request.values.get("version")
try:
subversion = request.values.get("subversion")
except:
subversion = None
sid = request.values.get("sid")
res = program_start(script, version, subversion, sid)
return res
@app.route('/py_fof_monetary_funds_details_daily_2_1.sh',methods={"POST","GET"})
def index3():
script = 'sh py_fof_monetary_funds_details_daily_2_1.sh'
version = request.values.get("version")
try:
subversion = request.values.get("subversion")
except:
subversion = None
sid = request.values.get("sid")
res = program_start(script, version, subversion, sid)
return res
@app.route('/py_fof_non_monetary_funds_details_daily_2_1.sh',methods={"POST","GET"})
def index4():
script = 'sh py_fof_non_monetary_funds_details_daily_2_1.sh'
version = request.values.get("version")
try:
subversion = request.values.get("subversion")
except:
subversion = None
sid = request.values.get("sid")
res = program_start(script, version, subversion, sid)
return res
def program_start(script, version, subversion, sid ):
event_type = 1
t = threading.Thread(target=main, args=(script, version, subversion, sid, event_type))
t.start()
return '<h3> execute success </h3>'
if __name__ == '__main__':
try:
host = sys.argv[1]
port = int(sys.argv[2])
except BaseException:
print 'argv error,exit - {0}'.format(traceback.format_exc())
sys.exit(1)
app.run(host=host,port=port,debug=True)