app.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import time
  3. import paramiko
  4. from flask import Flask, render_template, request, url_for, redirect
  5. # coding=utf-8
  6. import paramiko
  7. import pymysql
  8. import logging
  9. from logging.handlers import RotatingFileHandler
  10. def make_dir(make_dir_path):
  11. path = make_dir_path.strip()
  12. if not os.path.exists(path):
  13. os.makedirs(path)
  14. def getLogHandler():
  15. # 日志地址
  16. log_dir_name = "/.log"
  17. # 文件名,以日期作为文件名
  18. log_file_name = 'logger-' + time.strftime('%Y-%m-%d', time.localtime(time.time())) + '.log'
  19. # 创建日志文件
  20. log_file_folder = os.path.abspath(
  21. os.path.join(os.path.dirname(__file__), os.pardir)) + os.sep + log_dir_name
  22. make_dir(log_file_folder)
  23. log_file_str = log_file_folder + os.sep + log_file_name
  24. # 默认日志等级的设置
  25. logging.basicConfig(level=logging.INFO)
  26. # 创建日志记录器,指明日志保存路径,每个日志的大小,保存日志的上限
  27. file_log_handler = RotatingFileHandler(log_file_str, maxBytes=1024 * 1024, backupCount=10, encoding='UTF-8')
  28. # 设置日志的格式 发生时间 日志等级 日志信息文件名 函数名 行数 日志信息
  29. formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
  30. # 将日志记录器指定日志的格式
  31. file_log_handler.setFormatter(formatter)
  32. # 日志等级的设置
  33. # file_log_handler.setLevel(logging.WARNING)
  34. return file_log_handler
  35. app = Flask(__name__)
  36. # 添加日志配置
  37. app.logger.addHandler(getLogHandler())
  38. # 激活上下文
  39. ctx = app.app_context()
  40. ctx.push()
  41. # conn = pymysql.connect(
  42. # host='127.0.0.1',
  43. # user='root',
  44. # password='g53743001',
  45. # db='ip',
  46. # charset='utf8'
  47. # )
  48. conn = pymysql.connect(
  49. host='52.69.200.169',
  50. user='coffee',
  51. password='skyeye',
  52. db='Coffee',
  53. charset='utf8'
  54. )
  55. port = '22'
  56. @app.route("/")
  57. def index():
  58. return render_template("index.html", title='ERP WEB')
  59. @app.route("/check_Peel_Output_status",methods=['POST', 'GET'])
  60. def check_Peel_Output_status():
  61. if request.method == 'POST':
  62. cur = conn.cursor()
  63. #sql = "SELECT * FROM `ferment_container_status` ORDER BY `sn` DESC LIMIT 1 "
  64. sql = "SELECT `Peel_Output_1`,`Peel_Output_2` FROM `clean_container_status` ORDER BY `sn` DESC LIMIT 1"
  65. #sql = ferment_container_status.query.order_by(text('datetime desc')).first()
  66. cur.execute(sql)
  67. get_Peel_Output_status = cur.fetchone()
  68. cur1 = conn.cursor()
  69. sql1 = "SELECT `Ferment_Input_1`,`Ferment_Input_2` FROM `ferment_container_status` ORDER BY `sn` DESC LIMIT 1 "
  70. cur1.execute(sql1)
  71. get_Ferment_Input = cur1.fetchone()
  72. return render_template('test.html',**locals())
  73. return render_template("index.html")
  74. @app.before_request
  75. def log_each_request():
  76. app.logger.info('【請求方法】{}【請求路徑】{}【請求IP】{}'.format(request.method, request.path, request.remote_addr))
  77. if __name__ == '__main__':
  78. app.run(debug=True,host = '0.0.0.0' , port=5566)