python: xp firewall

Posted by tjwei on 星期六, 3月 03, 2007 with No comments
網路上面找到的
開xp 防火牆 port

import win32com.client
fw=win32com.client.gencache.EnsureDispatch('HNetCfg.FwMgr',0)
apps=fw.LocalPolicy.CurrentProfile.AuthorizedApplications
for app in apps:
print app.Name, app.ProcessImageFileName
newapp=win32com.client.Dispatch('HNetCfg.FwAuthorizedApplication')
newapp.Name='python_d.exe'
newapp.ProcessImageFileName='j:\\python24\\python_d.exe'
newapp.Enabled=True
apps.Add(newapp)

另外

import win32com.client
import pythoncom

# Using Python to detect if XP Firewall is enabled.
def is_WinXP_FW_Enabled():
try:
XPFW = win32com.client.gencache.EnsureDispatch('HNetCfg.FwMgr',0)
XPFW_Policy = XPFW.LocalPolicy.CurrentProfile
except pythoncom.com_error:
# Can't dispatch or access the FW COM instance.
return False
return XPFW_policy.FirewallEnabled
# Using Python to detect if XP SP 2 is installed.
def is_WinXP_SP2():
try:
objWMIService = win32com.client.GetObject("winmgmts:")
objOS = objWMIService.ExecQuery ("Select * from
Win32_OperatingSystem WHERE Caption LIKE 'Microsoft Windows XP%' AND
ServicePackMajorVersion >= 2")
if objOS.Count != 0:
# objOS.Count on Win2000 is invalid (raises com_error)
return True
except pythoncom.com_error:
# Can't access WMI so default to False.
# Maybe add some fancier error handling later.
return False
# No COM errors but not Win XP
return False

Categories: ,