当前位置: 首页> 财经> 金融 > 建筑工程网上报建流程_网页传奇游戏怎么彻底卸载_怎样在百度上免费做广告_西安核心关键词排名

建筑工程网上报建流程_网页传奇游戏怎么彻底卸载_怎样在百度上免费做广告_西安核心关键词排名

时间:2025/7/9 1:13:01来源:https://blog.csdn.net/pasaxiaorui/article/details/143278757 浏览次数:0次
建筑工程网上报建流程_网页传奇游戏怎么彻底卸载_怎样在百度上免费做广告_西安核心关键词排名

目录

案例一:系统监控工具

案例二: 假设 多台电脑  在局域网中 只有一台电脑可以连接外网, 内网的数据必须要传递到外网电脑

内网:

外网:

程序打包


案例一:系统监控工具

  • 加载配置:从 config.json 文件中读取要监控的端口列表。

  • 端口检查:检查指定的端口是否正在被使用。

  • 系统信息收集

    • CPU 信息:获取 CPU 使用率和核心数。
    • 内存信息:获取虚拟内存和交换内存的状态。
    • 磁盘信息:列出所有磁盘分区及其使用情况,包括总容量、已用容量、剩余容量和使用率。
    • 网络信息:获取网络 I/O 统计和网络接口的地址信息。
    • 进程数量:统计当前运行的进程数量。
  • 循环监控:每五秒钟重复获取并打印上述系统信息,同时监控指定端口的状态,输出每个端口的运行状态。

  • 异常处理:在数据收集和打印过程中捕获并输出可能发生的错误。

import sys
import psutil
import time
import json
import osdef load_config():# 获取 .exe 所在目录的路径base_path = os.path.dirname(os.path.abspath(sys.argv[0]))config_path = os.path.join(base_path, 'config.json')# 检查配置文件是否存在if not os.path.exists(config_path):print(f"配置文件 {config_path} 未找到!")return []# 读取配置文件内容with open(config_path, 'r') as f:config = json.load(f)return config.get("monitor_ports", [])def is_port_in_use(port):# 检查指定端口是否在使用for conn in psutil.net_connections(kind='inet'):if conn.laddr.port == port and conn.status == psutil.CONN_LISTEN:return Truereturn Falsedef get_system_info():monitor_ports = load_config()  # 加载要监控的端口while True:try:# 获取 CPU 信息cpu_percent = psutil.cpu_percent(interval=1)cpu_count = psutil.cpu_count(logical=True)# 获取内存信息virtual_memory = psutil.virtual_memory()swap_memory = psutil.swap_memory()# 获取磁盘信息disk_partitions = psutil.disk_partitions()disk_info = []for partition in disk_partitions:partition_usage = psutil.disk_usage(partition.mountpoint)disk_info.append({'device': partition.device,'mountpoint': partition.mountpoint,'fstype': partition.fstype,'total': partition_usage.total,'used': partition_usage.used,'free': partition_usage.free,'percent': partition_usage.percent,})disk_io = psutil.disk_io_counters()# 获取网络信息net_io = psutil.net_io_counters()net_if_addrs = psutil.net_if_addrs()# 获取进程数量process_count = len(psutil.pids())# 打印系统信息print("\n=== 系统信息 ===")print(f"CPU 使用率: {cpu_percent}%")print(f"CPU 核心数: {cpu_count}")print(f"虚拟内存: {virtual_memory}")print(f"交换内存: {swap_memory}")# 打印磁盘信息print("\n=== 磁盘信息 ===")for info in disk_info:print(f"设备: {info['device']}")print(f"挂载点: {info['mountpoint']}")print(f"文件系统类型: {info['fstype']}")print(f"总容量: {info['total'] / (1024 ** 3):.2f} GB")print(f"已使用: {info['used'] / (1024 ** 3):.2f} GB")print(f"剩余: {info['free'] / (1024 ** 3):.2f} GB")print(f"使用率: {info['percent']}%")print("----------")print(f"磁盘 I/O: 读 {disk_io.read_count}, 写 {disk_io.write_count}, 读字节 {disk_io.read_bytes}, 写字节 {disk_io.write_bytes}")# 打印网络信息print(f"网络 I/O: {net_io}")print("网络接口地址:")for interface, addrs in net_if_addrs.items():print(f"  {interface}: {[addr.address for addr in addrs if addr.family == psutil.AF_LINK]}")print(f"当前进程数量: {process_count}")# 监控指定端口状态print("\n=== 端口监控 ===")for port in monitor_ports:status = "运行中" if is_port_in_use(port) else "未运行"print(f"端口 {port}: {status}")except Exception as e:print(f"发生错误: {e}")# 每五秒钟获取一次time.sleep(5)# 调用函数
get_system_info()
{"monitor_ports": [80, 443, 8080]
}

案例二: 假设 多台电脑  在局域网中 只有一台电脑可以连接外网, 内网的数据必须要传递到外网电脑

1. 主程序(服务器)

  • 监听连接:在本地的5000端口上创建一个TCP服务器,等待客户端的连接请求。

  • 接受客户端连接:当接收到来自客户端的连接时,打印出连接的客户端地址,并关闭连接。

2. 从程序(客户端)

  • 网络扫描:在指定的子网(如192.168.1.0/24)中,逐个IP地址尝试连接主程序的5000端口。

  • 连接检测:如果成功连接到主程序,打印出主程序的IP地址;如果没有找到主程序,则输出相应的信息。

内网:

import socket
import ipaddressdef find_master():# 假设在 192.168.1.0/24 网段subnet = "192.168.1.0/24"port = 5000for ip in ipaddress.IPv4Network(subnet):print(f"正在寻找:{ip}")with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:s.settimeout(0.5)  # 设置超时时间,避免长时间等待try:s.connect((str(ip), port))print(f"找到主程序,地址:{ip}")return str(ip)  # 返回主程序的 IPexcept (socket.timeout, ConnectionRefusedError):pass  # 尝试下一个 IPprint("未找到主程序")return None
find_master()

外网:

import socketdef start_master():# 主程序监听的端口host = '0.0.0.0'  # 接收来自任何地址的连接port = 5000with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:s.bind((host, port))s.listen()print(f"主程序在端口 {port} 上等待从程序连接...")while True:conn, addr = s.accept()print(f"接收到从程序连接,地址:{addr}")conn.close()start_master()

程序打包

安装pyinstaller

pip install pyinstaller

打开终端  到你程序所在的目录

执行

pyinstaller --onefile main_script.py

就可以看见执行文件

关键字:建筑工程网上报建流程_网页传奇游戏怎么彻底卸载_怎样在百度上免费做广告_西安核心关键词排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: