python - 如何将json内容传递给python子进程或json文件
问题描述
我正在使用 python subprocess.popen 运行 openshift 命令。
我的 subprocess.popen 看起来像:
subprocess.Popen(['oc','create','-n',project,'-f','resource.json'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
我正在尝试像这样传递 json 文件。但这不是我要创建的资源。有谁知道我可以将json内容传递给它的任何其他方式。
解决方案
由于 OpenShift 是 Kubernetes,如果您想使用 Python Kubernetes 客户端与 OpenShift 交互,可能值得一试:https ://github.com/kubernetes-client/python
您的代码使用subprocess
看起来正确,因此请检查打印的内容stdout
和stderr
:
import subprocess
project = 'simon'
process = subprocess.Popen(['oc','create','-n',project,'-f','resource.json'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
for line in process.stderr:
print(line.strip())
for line in process.stdout:
print(line.strip())
例如:
$ python3 test.py
b'pod/test created'
$ python3 test.py
b'Error from server (AlreadyExists): error when creating "resource.json": pods "test" already exists'
推荐阅读
- javascript - Flexbox - 基于特定列高度的流体网格
- python - Python Pandas 将列值更改为 NULL 并返回其原始值
- jquery - 检查是否未设置输入字段和选择字段
- html - 如何使用 JSTL 动态插入 html 代码?
- webmethods - 为什么 Java 服务在 WebMethods 集成服务器中是静态的和最终的
- javascript - 添加 onClick 功能以选择元素
- javascript - 在 Flutter 中映射为 webview
- php - 如何在 php 中从表单中搜索多个输入?
- android - 非 UI 线程的领域如何从 UI 线程调用 changelistener?
- javascript - JavaScript 正则表达式开始字符串澄清 + str.replace()