python中config命令_利用python数组解析ifconfig命令输出

有时候我们需要对命令的格式化输出进行解析,解析通常依赖命令自身的格式化特征,不过这些特征都有共性:各种层面的循环列表结构

比如ifconfig命令,首先循环列出所有网络接口,然后在网络接口内,在循环列出各个预定义字段

所以对该命令解析时,首先要按list之list的结构对其输出内容进行组织,这里用python的(多维)数组是个不错的主意

#encoding=utf-8

import subprocess

tmp_file = open('/tmp/g.log','w')

subprocess.call(['ifconfig'], stdout=tmp_file)

tmp_file = open('/tmp/g.log','r')

ifaces = []

ifaces.append([]) #向数组的尾端添加一个数组!

ifaces_cnt = 0

line_num = 0

for line_raw in tmp_file:

line = line_raw.rstrip() #空行的长度为1,strip后长度才为0

if len(line) > 0:

ifaces[ifaces_cnt].append(line) #向数组的尾端添加一个字符串!

else:

ifaces_cnt = ifaces_cnt +1

ifaces.append([])

print 'there are %d section'%len(ifaces)

for face in ifaces:

print '========'

for l in face:

print l

运行输出效果:

there are 3 section

========

eth1 Link encap:以太网 硬件地址 00:1c:25:dd:44:8b

inet 地址:192.168.0.105 广播:192.168.0.255 掩码:255.255.255.0

inet6 地址: fe80::21c:25ff:fedd:448b/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1

接收数据包:569916 错误:0 丢弃:0 过载:0 帧数:0

发送数据包:389975 错误:0 丢弃:0 过载:0 载波:0

碰撞:0 发送队列长度:1000

接收字节:651139742 (651.1 MB) 发送字节:37622845 (37.6 MB)

中断:17

========

lo Link encap:本地环回

inet 地址:127.0.0.1 掩码:255.0.0.0

inet6 地址: ::1/128 Scope:Host

UP LOOPBACK RUNNING MTU:65536 跃点数:1

接收数据包:17094 错误:0 丢弃:0 过载:0 帧数:0

发送数据包:17094 错误:0 丢弃:0 过载:0 载波:0

碰撞:0 发送队列长度:0

接收字节:1578445 (1.5 MB) 发送字节:1578445 (1.5 MB)

========

多出来的1个section是边界效应,请忽视

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文:http://blog.csdn.net/happen23/article/details/48048057

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用ESP32-CAM模块和Python来进行图像识别。下面是一个简单的示例代码,使用ESP32-CAM通过WiFi连接到服务器,并发送图像进行识别: ```python import urequests import network from machine import Pin import time # 配置WiFi连接 wifi_ssid = 'your_wifi_ssid' wifi_password = 'your_wifi_password' def connect_wifi(): sta_if = network.WLAN(network.STA_IF) if not sta_if.isconnected(): print('connecting to network...') sta_if.active(True) sta_if.connect(wifi_ssid, wifi_password) while not sta_if.isconnected(): pass print('network config:', sta_if.ifconfig()) # 配置摄像头拍摄及图像上传 def capture_and_upload(): led = Pin(2, Pin.OUT) led.off() # 初始化摄像头 from machine import UART uart = UART(0, baudrate=115200) uart.init(115200, bits=8, parity=None, stop=1) # 设置图像分辨率 uart.write(b'AT+CAMSET=1,0,0,0,0') time.sleep_ms(200) # 拍摄图像 uart.write(b'AT+CAMO=1') time.sleep(5) # 读取图像数据 uart.write(b'AT+CAMA?') time.sleep(1) while uart.any(): line = uart.readline() if line.startswith(b'+CAMA: '): img_data = line.split(b', ')[1] break # 上传图像到服务器进行识别 url = 'http://your_server_url' files = {'file': ('image.jpg', img_data)} response = urequests.post(url, files=files) print(response.text) # 执行主程序 connect_wifi() capture_and_upload() ``` 你需要将`your_wifi_ssid`和`your_wifi_password`替换为你的WiFi网络的SSID和密码。同时,你需要设置一个服务器的URL,用于接收图像并进行识别。在服务器端,你可以使用Python的Flask框架来接收图像并进行处理。 希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值