linux - 将 Internet 访问脚本列入白名单
问题描述
我们有一个 windows 服务器和一个 linux 服务器。两台服务器上都没有互联网。现在,我们需要在这些服务器上部署一个 python 脚本,它将向外部网络 url 发出 http get 请求。所以,我需要互联网。但是我们不能为所有应用程序启用互联网。有没有办法只为这个特定的脚本启用互联网?
解决方案
我不太了解 Linux 平台,但是您可以允许程序使用命令行从 python 发出传出请求,如下所示:
import sys
import os
def allow_outbound_connections(program_path):
"""
Allow program outbound connections.
"""
if "win" in sys.platform:
command = f'netsh advfirewall firewall add rule name="{program_path}" '\
'dir=out action=block program= "{os.path.basename(program_path}"'\
' enable=yes profile=any'
if "linux" in sys.platform:
# *Add a similar command in Linux*
with os.popen(command) as stream:
return stream.read()
def main():
# First allow this program to make outbound connections.
output = allow_outbound_connections(__file__)
# (Eventually you could handle the output)
# Now make request to an outside network url.
推荐阅读
- javascript - 对象作为 React 子对象无效(找到:带有键 {_id, name} 的对象)
- python - Bokeh output_file 未在 html 中显示
- javascript - 错误:未处理的拒绝(TypeError):无法读取未定义、ReactJS、搜索的属性“livres”
- php - SQL ORDER BY date ASC 省略要显示的记录
- c++ - C++ 变量意外被覆盖
- python - 将 .txt 上传到 FTP 服务器会添加新行
- vagrant - 当我通过 vagrant 创建集群时,我遇到了 minidcos 的问题
- java - 错误:无法找到或加载主类 JavaFundamentals.GUIOutput
- asp.net - ASP.NET:即使页面未刷新/重新加载,cookie 也会在过期后被删除
- pip - 如何为所有用户在 ubuntu 上安装 scrapyd 1.2.1?