如何从 subprocess.check_output() 修复 None return 值?

How to fix None return value from subprocess.check_output()?

我的代码使用子进程扫描路由器以查找 mac 地址。但是在启动时,它 returns “None”。我该如何解决这个问题?

import re
import os
import subprocess

# MAC address regex
macRegex = re.compile("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\1[0-9a-f]{2}){4}$")

cmd = "chcp 65001 && ipconfig | findstr /i \"Default Gateway\""
res = subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)

def GetMacByIP():
 z = subprocess.check_output('arp -a ', shell=True, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)
 a = z.decode(encoding="cp866")
 f = a.find("Physical Address")
 o = a[f:].split(' ')
 for a in o:
  if macRegex.match(a):
     return a.replace('-', ':')

这部分在这里:

def GetMacByIP():
 z = subprocess.check_output('arp -a ', shell=True, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL)
 a = z.decode(encoding="cp866")
 f = a.find("Physical Address")
 o = a[f:].split(' ')
 for a in o:
  #print(a)
  #print(macRegex.match(a))
  if macRegex.match(a): # It's possible that this if statement never meets
     return a.replace('-', ':')

您有一个 if 语句。当条件永远不满足语句时,你没有告诉 python 要 return 什么,所以,它 returns None.