如何允许 Python.app 在 Mac OS X 上使用防火墙?

How to allow Python.app to firewall on Mac OS X?

当我在 Mac 上 运行 一个 python 应用程序时,它会显示许多关于 "Python.app" 接受传入网络连接的对话框。

即使我允许了很多次,它还是一次又一次地显示。

如何允许显示一次不再显示?


编辑

我发现了这个问题: Add Python to OS X Firewall Options?

我按照接受的答案去做,但最后当我 运行 codesign -s "My Signing Identity" -f $(which python) 时,它说:

/usr/bin/python: replacing existing signature
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate: can't create output file: /usr/bin/python.cstemp (Operation not permitted)
/usr/bin/python: the codesign_allocate helper tool cannot be found or used

下一步怎么办?

显然 El Capitan 有一个叫做 System Integrity Protection 的东西可以防止任何用户(甚至 root)修改某些 OS 资源。例如本例中的 /usr/bin 目录,其中包含 python 可执行文件。要对 python 二进制文件进行自签名,您可以通过在恢复模式下重新启动 mac 来禁用 SIP(​​按住 CMD+R 重新启动),然后在终端中输入:

csrutil disable

然后重新启动到常规 OSX,并按照步骤自签名 python 并执行:

codesign -s "My Signing Identity" -f $(which python)

最后重启进入恢复模式,re-enable SIP:

csrutil enable

我从 python.org 安装了 Python 3.6,我遇到了类似的问题。我反复尝试对 python 二进制文件 /usr/local/bin/python3、/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 等进行自签名,但我会继续得到MacOS 防火墙 Deny/Allow 弹出窗口。

最后,对位于 /Library/Frameworks/Python.framework/Versions/3.6/Resources 的应用程序进行自签名:

codesign --force --sign "My Signing Identity" /Library/Frameworks/Python.framework/Versions/3.6/Resources/Python.app

我正在使用 MacOS Mojave 10.14.1,这是值得的。

对上述@mr-howdy 的回答略作补充。对于 Python 3.7,我必须使用:

codesign --force --sign "My Certificate" /Library/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python

注意添加到路径中的额外 Contents/MacOS/Python

With the OS X firewall enabled, you can remove the "Do you want the application "python" to accept incoming network connections?" message.

Create a self-signed certificate.

Open Keychain Access. Applications > Utilities > Keychain Access.
Keychain Access menu > Certificate Assistant > Create a Certificate...
Enter a Name like "My Certificate".
Select Identity Type: Self Signed Root
Select Certificate Type: Code Signing
Check the Let me override defaults box
Click Continue
Enter a unique Serial Number
Enter 7300 for Validity Period.
Click Continue
Click Continue for the rest of the dialogs
Now sign your application

  codesign -s "My Certificate" -f $(which python)

In the dialog that appears, click "Allow".

Note that when using a virtual environment, you need to activate the virtual environment before running this command.

创建自签名证书。

  • 打开钥匙串访问。应用程序 > 实用程序 > 钥匙串访问。
  • Keychain Access 菜单 > Certificate Assistant > 创建证书...
  • 输入名称,例如 "My Certificate"。
  • Select 身份类型:自签名根
  • Select 证书类型:代码签名
  • 选中让我覆盖默认值框
  • 点击继续
  • 输入唯一的序列号(从 1 到 2147483647)
  • 有效期输入 7300。
  • 点击继续
  • 单击“继续”以显示其余对话框
  • 现在通过以下任一方式签署您的申请:

    • 在终端 运行 codesign -s --deep "My Certificate" -f $(which python)
    • 在出现的对话框中,单击 "Allow"。

    • 打开钥匙串应用程序,搜索您的证书,
    • 右击'Get Info'
    • 展开信任部分,在代码签名下选择 'Always Trust'

注意使用虚拟环境时,需要在运行执行此命令前激活虚拟环境

您可以在“系统偏好设置”->“安全与隐私”中允许 python 的传入连接。单击防火墙选项卡,然后单击防火墙选项按钮(您可能需要先 authenticate/unlock)。您应该看到 python 列在某处,您可以 select 允许传入连接。这至少解决了我的问题。