起因:公司有多台东芝打印机,为了方便查看各打印机墨粉量,才促使写成这个脚本
打印机型号:TOSHIBA e-STUDIO 3505等
※不保证所有型号适用,我反正已经可以使用了
[Python] 纯文本查看 复制代码 import requests
import socket
from xml.dom import minidom
# 获取本机IP
def extract_ip():
st = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
st.connect(('10.255.255.255', 1))
IP = st.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
st.close()
return IP
# 获取打印机信息
def getXMLStrToXml(ip):
# 获取到cookie
cookie = requests.get('http://'+ip+'/?MAIN=TOPACCESS').cookies
# 获取session
for key, value in cookie.items():
if key == 'Session':
session = value
# 请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33',
'csrfpId': session
}
# 请求数据
data = "<DeviceInformationModel><GetValue><Controller><Information/><Settings><AdminSystemSettings><EFiling><eFilingEnabled/></EFiling><Copy/></AdminSystemSettings><WebDataRetentionPeriod/></Settings></Controller></GetValue><GetValue><MFP><DeviceState></DeviceState><ErrorState></ErrorState><Printer></Printer><Fax></Fax><ModelName></ModelName><System><PageMemory></PageMemory><MainMemory></MainMemory><HDD></HDD></System></MFP></GetValue><GetValue><FileStorages><FileStorage selected='1'><name>SaveAsFile</name></FileStorage><FileStorage selected='1'><name>FaxStorage</name></FileStorage></FileStorages></GetValue><GetValue><Network><Adapters><Wire/><Wireless/></Adapters><Protocols><TCP-IP><hostName></hostName></TCP-IP></Protocols></Network></GetValue><GetValue><DiagnosticMode><Category><Mode>08</Mode><MainCode><Code>8625</Code></MainCode></Category></DiagnosticMode></GetValue><SetValue><FileStorages><FileStorage selected='1'><name>SaveAsFile</name></FileStorage><FileStorage selected='1'><name>FaxStorage</name></FileStorage></FileStorages></SetValue><SetValue><DiagnosticMode><Category><Mode>08</Mode><MainCode><Code>8625</Code></MainCode></Category></DiagnosticMode></SetValue><Command><GetPhysicalSpaceInfo><commandNode>FileStorages</commandNode></GetPhysicalSpaceInfo></Command><Command><GetDiagnosticMode><commandNode>DiagnosticMode</commandNode></GetDiagnosticMode></Command></DeviceInformationModel>"
url = 'http://' + ip + '/contentwebserver'
# 获得到字符串类型的xml
xml_contents = requests.post(url, headers=headers, data=data, cookies=cookie).content.decode('utf-8')
# 判断是否获取到正确的xml数据
if(len(xml_contents) < 150 ):
if(xml_contents.index('STATUS_AL_INVALID_SESSION_ID') != -1):
print('获取打印机信息失败,无效的sessionId')
return False
# 使用minidom将xml字符串转换成xml对象 # dom = minidom.parse("temp.xml")
dom = minidom.parseString(xml_contents)
#获取所有墨粉余量
names = dom.getElementsByTagName("RemainingQuantityDetails")
return names
# 程序入口
if __name__ == "__main__":
ip_list = ['10.12.22.2','10.12.22.3','10.12.22.4','10.12.22.5']
place_list = ['10层','11层','12层左侧','12层右侧']
for i in range(len(ip_list)):
names = getXMLStrToXml(ip_list[i])
print("位置:" + place_list[i] ,)
print("IP: " + ip_list[i])
print("黄色:",names[0].firstChild.data+"%")
print("品红:",names[1].firstChild.data+"%")
print("青色:",names[2].firstChild.data+"%")
print("黑色:",names[3].firstChild.data+"%")
print("\n") |