问题描述
我目前正在基于python创建软件,并且正在其中通过 selenium的Webdriver (Chromedriver)使用一些自动化操作,然后我使用 cx_Freeze 。我可以在机器上使用程序,没有任何问题,但是当我将程序分发给其他人时,会发生以下两种情况之一:
2。用户什么也没得到,函数调用selenium后几秒钟,我的程序崩溃了。
问题
我有很多问题,如果有人能回答我,我会很乐意:
- 是否有一种自动方式允许我的程序通过防火墙?
- 如果没有办法,使使用该软件时第一次显示Windows安全警报?
- 如果没有,则可以通过Windows设置来实现“手动”操作吗?
- 我的程序是否知道我使用Selenium的Webdriver与某些网站进行交互并剪贴数据,是否同时需要专用网络和公用网络选项?
- 是否有办法知道用户是否不允许该应用程序并检查两个选项(如果都需要)或至少检查了一个重要的选项(需要一个)以提示他在之后再次进行操作? / li>
我真的很感谢您花时间阅读我的问题,希望您能为我提供帮助,我对python还是很陌生(1年),我找不到在线搜索的答案。
解决方法
实际上,我通过做更多的研究并尝试了一些事情来回答自己的问题:
Q1>是的,我编写了一个python脚本来搜索规则是否已经存在,如果存在则删除它并创建它。效果很好:
import subprocess
import ctypes
import sys
from subprocess import DEVNULL
def check_admin():
""" Force to start application with admin rights """
try:
isAdmin = ctypes.windll.shell32.IsUserAnAdmin()
except AttributeError:
isAdmin = False
if not isAdmin:
ctypes.windll.shell32.ShellExecuteW(
None,"runas",sys.executable,__file__,None,1)
def checkIfRuleExists(ruleName):
"""Check if the rule exists
Args:
ruleName (str): the name of the rule
Returns:
bool: true if the rule exists false if not
"""
try:
command = 'netsh advfirewall firewall show rule name="{}"'.format(
ruleName)
output = subprocess.check_output(["powershell.exe",command])
except:
return False
else:
return True
def deleteRule(ruleName):
"""deletes the rule
Args:
ruleName (str): the name of the rule
"""
command = 'netsh advfirewall firewall delete rule profile=domain,private name="{}"'.format(
ruleName)
subprocess.run(command,shell=False,stdout=DEVNULL,stderr=DEVNULL)
def addRule(ruleName,programPath):
"""adds the rule
Args:
ruleName (str): the name of the rule
programPath (str) : the path of the program
"""
command = 'netsh advfirewall firewall add rule name="{0}" profile=domain,private protocol=any enable=yes DIR=In program="{1}" Action=Allow'.format(
ruleName,programPath)
subprocess.run(command,stderr=DEVNULL)
if __name__ == "__main__":
check_admin()
Rule = "YOUR RULE NAME"
path = "YOUR EXECUTABLE PATH"
if checkIfRuleExists(Rule):
deleteRule(Rule)
addRule(Rule,path)
Q2>也许有,但是直到现在我什么都没发现,由于Q1的解决方案,我认为它没有意义。
Q3>可以通过转到Windows设置>更新和安全>防火墙>高级设置来实现。
Q4>经过测试后,两者都需要。
Q5>不需要,因为规则创建过程现在是自动的。
就是这样,我希望这会帮助任何有需要的人。