如何用Python编写脚本一键开启或关闭系统防火墙?

2026-05-21 17:103阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计132个文字,预计阅读时间需要1分钟。

如何用Python编写脚本一键开启或关闭系统防火墙?

使用Python一键打开或关闭防火墙:运行以下代码即可实现。


利用python一键打开或关闭防火墙

#!/usr/bin/python
# -*- coding: GBK -*-
"""
@author: Roc-xb
"""
import os


# 启用防火墙
def open_firewall():
os.system("netsh advfirewall set currentprofile state on")
os.system("netsh advfirewall set domainprofile state on")
os.system("netsh advfirewall set privateprofile state on")
print("防火墙已开启")


# 关闭防火墙
def close_firewall():
os.system("netsh advfirewall set currentprofile state off")
os.system("netsh advfirewall set domainprofile state off")
os.system("netsh advfirewall set privateprofile state off")
print("防火墙已关闭")

如何用Python编写脚本一键开启或关闭系统防火墙?

本文共计132个文字,预计阅读时间需要1分钟。

如何用Python编写脚本一键开启或关闭系统防火墙?

使用Python一键打开或关闭防火墙:运行以下代码即可实现。


利用python一键打开或关闭防火墙

#!/usr/bin/python
# -*- coding: GBK -*-
"""
@author: Roc-xb
"""
import os


# 启用防火墙
def open_firewall():
os.system("netsh advfirewall set currentprofile state on")
os.system("netsh advfirewall set domainprofile state on")
os.system("netsh advfirewall set privateprofile state on")
print("防火墙已开启")


# 关闭防火墙
def close_firewall():
os.system("netsh advfirewall set currentprofile state off")
os.system("netsh advfirewall set domainprofile state off")
os.system("netsh advfirewall set privateprofile state off")
print("防火墙已关闭")

如何用Python编写脚本一键开启或关闭系统防火墙?