I want make a simple program to get the web server active on my local. I have try to create some code, this code i working well in windows 7 64bit. but when I run my code on windows XP32 bit, this code make a windows XP hang.
can you help me to explain why this code make the windows XP hang ?
def get_web_server():
import win32api
import subprocess
try:
cmd = 'for /f "usebackq tokens=5" %i in (`"netstat -aon | findstr "0.0:80""`) do @wmic PROCESS get Name,ProcessId,ExecutablePath | findstr "%i"'
output = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True).communicate()
try:
windows_exe = (output[0].split('\r\n')[0].strip().split()[0])
except:
windows_exe = None
try:
language, codepage = win32api.GetFileVersionInfo(windows_exe, '\\VarFileInfo\\Translation')[0]
stringFileInfo = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, "FileDescription")
description = win32api.GetFileVersionInfo(windows_exe, stringFileInfo)
except:
description = None
except:
description = None
return description
print get_web_server()
sample output in win7 64bit when I'm using XAMPP
Apache HTTP Server
Thanks
import os
import subprocess
import re
import win32api
pid=subprocess.check_output('netstat.exe -abno | find /i "listening" |find ":80"', shell=True).split('\n', 1)[0].split()[-1]
webserver= subprocess.check_output('tasklist /FI "PID eq '+pid+'" /v /fo List', shell=True).splitlines()[-1].split("Window Title:", 1)[1].rsplit("Window Title:", 1)[0].strip()
def getFileDescription(windows_exe):
try:
language, codepage = win32api.GetFileVersionInfo(windows_exe, '\\VarFileInfo\\Translation')[0]
stringFileInfo = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, "FileDescription")
description = win32api.GetFileVersionInfo(windows_exe, stringFileInfo)
except:
description = "unknown"
return description
print getFileDescription(webserver)
Output for above is "Apache HTTP Server"
I don't know why your's doesn't work in XP, but try the code above (Python 2.7)
Basically: executes command> gets relevant line> gets last word(PID) in output> passes pid to tasklist> goes to relevant line in tasklist> prints first word.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.