python 判断某IP是否ping通 成功则返回1 失败返回0
利用ping命令的返回信息差异,以及re正则采集的策略 计算是否ping通

代码如下
import subprocess
import re
def ping_server(server_ip):
try:
output = subprocess.check_output(['ping', '-i', '4', server_ip])
#return output.decode('ansi')
result =output.decode('ansi')
#return result
match =re.findall(r'Average = \d',result)
if match:
return 1
else:
return 0
except subprocess.CalledProcessError as e:
#return e.output.decode('ansi')
return 0
server_ip = '192.168.0.17'
result = ping_server(server_ip)
print(result)
python 检测IP ping检测 查物理地址
import subprocess
import re
import socket
from scapy.all import sr1, IP, ICMP
def get_mac_address(ip):
try:
# 执行arp命令来查找IP对应的MAC地址
result = subprocess.run(['arp', '-a'], capture_output=True, text=True, check=True)
output = result.stdout
# 解析输出,注意Windows上的输出格式可能与Linux不同
match = re.search(rf'{ip}\s+([0-9a-fA-F-]+)', output)
if match:
return match.group(1)
else:
return None
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
return None
def ping(host):
# 创建一个ICMP请求数据包
packet = IP(dst=host)/ICMP()
# 发送数据包并接收响应
reply = sr1(packet, timeout=1, verbose=False)
# 检查是否收到回复
if reply is not None and reply.haslayer(ICMP):
return True
else:
return False
ip_address = "192.168.0.254"
mac_address = get_mac_address(ip_address)
print(f"MAC Address: {mac_address}")
print(f"IP: {ip_address}")
if ping(ip_address):
print(f"{ip_address} is active.")
else:
print(f"{ip_address} is die.") PyQt5 菜单栏打开新的窗口
import sys
import sqlite3
from dotenv import load_dotenv
from os import getenv
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
#读取配置信息
load_dotenv()
#主窗口
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initializeUI()
def initializeUI(self):
# 设置窗口标题和初始大小
self.setWindowTitle(getenv("APP_NAME"))
self.setGeometry(100, 100, 600, 400)
# 创建菜单栏对象
menubar = QMenuBar(self)
self.setMenuBar(menubar) # 将菜单栏设置到窗口中
# 创建菜单对象
fileMenu = menubar.addMenu('文件(&F)')
editMenu = menubar.addMenu('编辑(&E)')
helpMenu = menubar.addMenu('帮助(&H)')
# 创建动作(Action)并添加到菜单中
exitAction = QAction(QIcon('exit.png'), '退出', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('退出应用程序')
#exitAction.triggered.connect(qApp.quit)
exitAction.triggered.connect(self.opensecond)
fileMenu.addAction(exitAction) # 将动作添加到文件菜单中
# 添加其他动作到编辑和帮助菜单中(示例)
newAction = QAction(QIcon('new.png'), '新建', self) # 假设你有一个new.png图标文件
newAction.setShortcut('Ctrl+N')
newAction.setStatusTip('新建文件')
editMenu.addAction(newAction)
aboutAction = QAction('关于', self)
aboutAction.setStatusTip('关于本应用')
helpMenu.addAction(aboutAction)
windowList = []
def opensecond(self):
the_window =SecondWindow()
self.windowList.append(the_window)
self.close()
the_window.show()
#第二个窗口
class SecondWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('第二主界面')
self.label = QLabel()
self.label.setText("第二个主界面")
self.label.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
self.label.setAlignment(Qt.AlignCenter)
self.label.setFont(QFont("Roman times", 50, QFont.Bold))
self.setCentralWidget(self.label)
self.statusBar().showMessage("当前用户:一心狮")
self.showMaximized()
windowList = []
#重写关闭返回当前窗口
def closeEvent(self, event):
the_window = MainWindow()
self.windowList.append(the_window)
the_window.show()
event.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_()) python 读取xslx第一列数据并返回
import pandas as pd
import numpy as np
# 读取Excel文件
excel_data = pd.read_excel('order.xlsx', sheet_name='Sheet')
# 提取第一列的数值数据并转换为字符串数组
string_array = excel_data.iloc[:,0].astype(str).tolist()
#存储硬盘
np.save('order.npy',string_array)
#打印结果
print(string_array)
要实现前要安装python3 并安装相应的依赖库
pip install openpyxl python 识别图片中的二维码
pip install pyzbar
#pip install Pillow
from pyzbar.pyzbar import decode
from PIL import Image
def decode_qr_code(image_path):
# 打开图片
image = Image.open(image_path)
# 使用pyzbar的decode函数解码图片中的二维码
decoded_objects = decode(image)
# 输出解码结果
for obj in decoded_objects:
print('Type:', obj.type)
print('Data:', obj.data.decode('utf-8')) # 解码数据为utf-8格式
print('Location:', obj.rect) # 输出二维码在图片中的位置
# 调用函数,传入图片路径
decode_qr_code('I:/winpython/erweima.jpg')
识别结果如下:
Could not load file or assembly System.Core, Version=2.0.5.0 ...报错
当前环境:
win7
network4.0
原因:该应用程序使用的版本2.0.5 缺少更新文件。
解决:
更新network4.0补丁
微软补丁地址
https://www.microsoft.com/en-us/download/details.aspx?id=3556
Microsoft .NET Framework 4 KB2468871
NDP40-KB2468871-v2-x64.rar
NDP40-KB2468871-v2-IA64.rar
NDP40-KB2468871-v2-x86.rar