章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • python

    • python基础

      • python基础知识
      • python基础较难的15个知识点
    • FastAPI

    • python每日练习脚本

    • python3给防火墙添加放行
    • python生成部署脚本
    • python将多个文件内容输出到一个文件中
    • 使用 Aligo 定时备份服务器文件
    • python监控日志文件并发送钉钉告警
    • python监控数据库脚本并发送钉钉告警
    • 使用python编写自动化发布脚本
    • 查询redis列表某个元素
    • centos7安装python3
    • python环境管理工具介绍
    • conda安装和镜像源配置
    • pip更换国内源
    • python爬虫
    • python环境启动服务报错缺少glibc库版本
    • 监控目录或文件变化
    • 批量更改文件
    • python引用数据库
    • python模块集合
  • shell

  • go

  • 编程
  • python
章工运维
2025-05-14

python模块集合

# 装饰器

def performance_monitor(func):
    def wrapper(*args, **kwargs):
        import time
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f"函数 {func.__name__} 执行耗时: {end_time - start_time}秒")
        return result
    return wrapper

@performance_monitor
def backup_database():
    # 模拟数据库备份
    import time
    #下面填需要执行的操作
    time.sleep(2) 
    print("数据库备份完成")

backup_database()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 并发编程

# 使用多线程处理复杂任务
from concurrent.futures import ThreadPoolExecutor

def process_server_log(log_file):
    # 处理单个日志文件的复杂逻辑
    pass

def batch_log_processing(log_files):
    with ThreadPoolExecutor(max_workers=10) as executor:
        executor.map(process_server_log, log_files)

1
2
3
4
5
6
7
8
9
10
11

# 网络编程

import socket
import requests

# socket编程
def port_scan(host, port):
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((host, port))
        if result == 0:
            print(f"Port {port} is open")
        sock.close()
    except Exception as e:
        print(f"Error scanning port {port}: {e}")

# 发起HTTP请求监控服务
def check_service_health(url):
    try:
        response = requests.get(url, timeout=5)
        if response.status_code == 200:
            print(f"Service {url} is healthy")
        else:
            print(f"Service {url} returned status code {response.status_code}")
    except requests.RequestException as e:
        print(f"Service {url} is down: {e}")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# 系统交互与自动化

import os
import subprocess
import shutil

# 文件和目录操作
def backup_directory(source, destination):
    try:
        shutil.copytree(source, destination)
        print(f"Backup of {source} completed successfully")
    except Exception as e:
        print(f"Backup failed: {e}")

# 执行系统命令
def run_system_command(command):
    try:
        result = subprocess.run(command, shell=True, capture_output=True, text=True)
        print("Command output:", result.stdout)
        return result.stdout
    except Exception as e:
        print(f"Command execution failed: {e}")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
微信 支付宝
上次更新: 2025/05/14, 15:46:36

← python引用数据库 进程pid判断脚本→

最近更新
01
个人开源项目推介平台汇总整理
06-27
02
英语学习技巧网站
06-27
03
5个开源流程图制作软件
06-27
更多文章>
Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式