按照我的预想,我要做一个发包工具。发包的话就需要知道包从电脑的哪个接口发出去,所以在初始化工具的时候需要知道当前电脑的网卡状态及网卡信息。
这次学习主要是读取PC的网卡及设备信息。
通过本次学习可以了解到如下知识点:
通过Python的netifaces模块,可以很容易的获取ip,路由、网关等信息
使用pip install netifaces 安装或者去官网下载,netifaces · PyPI
使用如下代码可以查看netifaces的属性和方法。
- import netifaces
- print(help(netifaces))
- Help on module netifaces:
-
- NAME
- netifaces
- FUNCTIONS
- gateways(...)
- Obtain a list of the gateways on this machine.
- Returns a dict whose keys are equal to the address family constants,
- e.g. netifaces.AF_INET, and whose values are a list of tuples of the
- format (, <interface>,
). - There is also a special entry with the key 'default', which you can use
- to quickly obtain the default gateway for a particular address family.
- There may in general be multiple gateways; different address
- families may have different gateway settings (e.g. AF_INET vs AF_INET6)
- and on some systems it's also possible to have interface-specific
- default gateways.
- ifaddresses(...)
- Obtain information about the specified network interface.
- Returns a dict whose keys are equal to the address family constants,
- e.g. netifaces.AF_INET, and whose values are a list of addresses in
- that family that are attached to the network interface.
- interfaces(...)
- Obtain a list of the interfaces available on this machine.
- DATA
- AF_12844 = 25
- AF_APPLETALK = 16
- AF_ATM = 22
- AF_BAN = 21
- AF_CCITT = 10
- AF_CHAOS = 5
- AF_CLUSTER = 24
- AF_DATAKIT = 9
- AF_DECnet = 12
- AF_DLI = 13
- AF_ECMA = 8
- AF_FIREFOX = 19
- AF_HYLINK = 15
- AF_IMPLINK = 3
- AF_INET = 2
- AF_INET6 = 23
- AF_IPX = 6
- AF_IRDA = 26
- AF_ISO = 7
- AF_LAT = 14
- AF_LINK = -1000
- AF_NETBIOS = 17
- AF_NETDES = 28
- AF_NS = 6
- AF_PUP = 4
- AF_SNA = 11
- AF_UNIX = 1
- AF_UNKNOWN1 = 20
- AF_UNSPEC = 0
- AF_VOICEVIEW = 18
- address_families = {-1000: 'AF_LINK', 0: 'AF_UNSPEC', 1: 'AF_UNIX', 2:...
- version = '0.11.0'
-
- FILE
- c:\program files\python37\lib\site-packages\netifaces.cp37-win_amd64.pyd
subprocess 模块允许我们启动一个新进程,并连接到它们的输入/输出/错误管道,从而获取返回值。
可以使用上节给的方法查看他的属性和方法。
常用的方法是run和popen。
下面是个在windows下执行ipconfig的代码。其中需要注意的是:
- import subprocess
-
- q = subprocess.run('ipconfig', stdout=subprocess.PIPE, universal_newlines=True)
- print(q.stdout)
-
- print('-------------------------------------------------------')
- p = subprocess.Popen('ipconfig', shell=True, stdout=subprocess.PIPE, encoding='CP936')
- p.wait()
- res = "".join(p.stdout.readlines())
- print(p.stdout)
- print(res)
结果:
- Windows IP 配置
-
-
- 以太网适配器 本地连接 3:
-
- 连接特定的 DNS 后缀 . . . . . . . :
- 本地链接 IPv6 地址. . . . . . . . : fe80::757d:c87c:6656:f0cf%17
- IPv4 地址 . . . . . . . . . . . . : 192.168.10.28
- 子网掩码 . . . . . . . . . . . . : 255.255.255.0
- 默认网关. . . . . . . . . . . . . :
-
- 以太网适配器 本地连接:
-
- 连接特定的 DNS 后缀 . . . . . . . :
- IPv4 地址 . . . . . . . . . . . . : 192.168.206.28
- 子网掩码 . . . . . . . . . . . . : 255.255.255.0
- 默认网关. . . . . . . . . . . . . : 192.168.206.1
-
- 隧道适配器 Teredo Tunneling Pseudo-Interface:
-
- 媒体状态 . . . . . . . . . . . . : 媒体已断开
- 连接特定的 DNS 后缀 . . . . . . . :
-
- -------------------------------------------------------
- <_io.TextIOWrapper name=3 encoding='CP936'>
-
- Windows IP 配置
-
-
- 以太网适配器 本地连接 3:
-
- 连接特定的 DNS 后缀 . . . . . . . :
- 本地链接 IPv6 地址. . . . . . . . : fe80::757d:c87c:6656:f0cf%17
- IPv4 地址 . . . . . . . . . . . . : 192.168.10.28
- 子网掩码 . . . . . . . . . . . . : 255.255.255.0
- 默认网关. . . . . . . . . . . . . :
-
- 以太网适配器 本地连接:
-
- 连接特定的 DNS 后缀 . . . . . . . :
- IPv4 地址 . . . . . . . . . . . . : 192.168.206.28
- 子网掩码 . . . . . . . . . . . . : 255.255.255.0
- 默认网关. . . . . . . . . . . . . : 192.168.206.1
-
- 隧道适配器 Teredo Tunneling Pseudo-Interface:
-
- 媒体状态 . . . . . . . . . . . . : 媒体已断开
- 连接特定的 DNS 后缀 . . . . . . . :
python winreg是python的一个标准库,用来对windows注册表的操作
我这里用它主要是因为windows下使用netiface模块的interfaces()函数不能拿到网卡名称(真正的名称),只能拿到一串奇怪的数字{95896FC1-9EC4-407E-89D7-A7007AE80D98}。需要将这串数字变成真正的网卡名称,就需要用到windows注册表
python re模块称为正则表达式;
基本和常用正则符号的含义如下:
| 正则符号 | 正则含义 |
| \d | 匹配所有的十进制数字 0-9 |
| \D | 匹配所有的非数字,包含下划线 |
| \s | 匹配所有空白字符(空格、TAB等) |
| \S | 匹配所有非空白字符,包含下划线 |
| \w | 匹配所有字母、汉字、数字 a-z A-Z 0- |
| \W | 匹配所有非字母、汉字、数字,包含下划线 |
| $ | 匹配一行的结尾(必须放在正则表达式最后面) |
| ^ | 匹配一行的开头(必须放在正则表达式最前面) |
| * | 前面的字符可以出现0次或多次(0~无限) |
| + | 前面的字符可以出现1次或多次(1~无限) |
| ? | 变"贪婪模式"为"勉强模式",前面的字符可以出现0次或1次 |
| . | 匹配除了换行符"\n"之外的任意单个字符 |
| | | 两项都进行匹配 |
| [ ] | 代表一个集合,有如下三种情况 |
| [abc] | 能匹配其中的单个字符 |
| [a-z0-9] | 能匹配指定范围的字符,可取反(在最前面加入^) |
| [2-9] [1-3] | 能够做组合匹配 |
| { } | 用于标记前面的字符出现的频率,有如下情况: |
| {n,m} | 代表前面字符最少出现n次,最多出现m次 |
| {n,} | 代表前面字符最少出现n次,最多不受限制 |
| {,m} | 代表前面字符最多出现n次,最少不受限制 |
| {n} | 前面的字符必须出现n次 |