Category - Python相关

2024-06-20 10:46:05    59    0    0

python主程序和两个静态页面,实现ansible批量执行更新脚本

app.py

  1. import os
  2. import logging
  3. from flask import Flask, request, render_template, redirect, url_for, session, render_template_string
  4. import hashlib
  5. import subprocess
  6. import datetime
  7. from flask_sqlalchemy import SQLAlchemy
  8. app = Flask(__name__)
  9. app.config['SECRET_KEY'] = '自定义32位字符串'
  10. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:passwd@10.0.0.1/python_db'
  11. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  12. db = SQLAlchemy(app)
  13. class User(db.Model):
  14. __tablename__ = 'users' # 指定表名为 'users'
  15. id = db.Column(db.Integer, primary_key=True)
  16. username = db.Column(db.String(100), unique=True, nullable=False)
  17. password = db.Column(db.String(255), nullable=False)
  18. class LoginLog(db.Model):
  19. __tablename__ = 'login_logs' # 指定表名为 'login_logs'
  20. id = db.Column(db.Integer, primary_key=True)
  21. username = db.Column(db.String(100), nullable=False)
  22. login_time = db.Column(db.DateTime, nullabl
2024-06-20 10:14:25    55    0    0
  1. import requests
  2. from fake_useragent import UserAgent
  3. import csv
  4. from datetime import datetime
  5. def convert_created_at(created_at):
  6. dt = datetime.strptime(created_at, '%a %b %d %H:%M:%S +0800 %Y')
  7. formatted_created_at = dt.strftime('%Y-%m-%d %H:%M:%S')
  8. return formatted_created_at
  9. def get_weibo_mid(weibo_id):
  10. url = f'https://weibo.com/ajax/statuses/show?id={weibo_id}'
  11. header = {
  12. 'user-agent': UserAgent().random
  13. }
  14. response = requests.get(url=url, headers=header)
  15. if response.status_code == 200:
  16. try:
  17. json_data = response.json()
  18. weibo_mid = json_data.get('mid')
  19. if weibo_mid:
  20. return weibo_mid
  21. else:
  22. print("未找到微博 MID。")
  23. except Exception as e:
  24. print(f"解析微博 MID 时发生异常:{e}")
  25. else:
  26. print(f"无法获取微博 MID。状态码:{response.status_code}")
  27. return None
  28. def get_weibo_comments(weibo_id, csv_filename):
  29. header = {
2024-06-20 10:10:18    27    0    0
  1. import openpyxl
  2. import os
  3. from copy import copy
  4. def get_cell(sheet, cell):
  5. # 如果是合并单元格,找到合并区域的起始单元格
  6. for merged_cell_range in sheet.merged_cells.ranges:
  7. if cell.coordinate in merged_cell_range:
  8. return sheet[merged_cell_range.coord.split(':')[0]]
  9. return cell
  10. def copy_cell_format(src_cell, dest_cell):
  11. if src_cell.has_style:
  12. dest_cell.font = copy(src_cell.font)
  13. dest_cell.border = copy(src_cell.border)
  14. dest_cell.fill = copy(src_cell.fill)
  15. dest_cell.number_format = src_cell.number_format
  16. dest_cell.protection = copy(src_cell.protection)
  17. dest_cell.alignment = copy(src_cell.alignment)
  18. def copy_row(sheet_src, sheet_dest, src_row_num, dest_row_num):
  19. src_row = sheet_src[src_row_num]
  20. for cell in src_row:
  21. dest_cell = sheet_dest.cell(row=dest_row_num, column=cell.column, value=cell.value)
  22. copy_cell_format(cell, dest_cell)
  23. # 复制行高
  24. sheet_dest.row_dimensi
Footer