工作正常:它搜索JIRA并将结果导出为JSON文件
curl -XN -u user:Pass -X GET -H "Content-Type: application/json" https://jira.company.com/rest/api/2/search?jql=project='"Technology"+AND+summary~"Remove%20User*"+AND+issuetype="Task"+AND+status!="DONE"' | python -m json.tool > /var/lib/rundeck/1.json
如何从python
运行相同的命令试过这个:
#!/usr/bin/python
import sys
import re
import requests
import subprocess
import os
import urllib2
def bash_command(cmd):
subprocess.Popen(cmd, shell=True, executable='/bin/bash')
bash_command("curl -XN -u user:Pass -X GET -H \"Content-Type:application/json\" https://jira.company.com/rest/api/2/search?jql=project='\"Technology\"+AND+summary~\"Remove%20User*\"+AND+issuetype=\"Task\"+AND+status!=\"DONE\"' | python -m json.tool > /var/lib/rundeck/1.json")
我得到了输出,与BASH相同,但是脚本被“卡住”,它不会退出,文件被创建,只有当我按下ENTER时才会退出
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9204 0 9204 0 0 47480 0 --:--:-- --:--:-- --:--:-- 47443
添加sys.exit()
但相同
也试过这个,但没有结果:
process = subprocess.Popen(cmd, shell=False, ,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
exit()
答案 0 :(得分:0)
我认为你在转义时缺少引号
curlCmd = <your curl command, use escape for quotes like -H \"Content-Type: application/json\" >
curlRes = subprocess.check_output(curlCmd, shell=True).strip()
答案 1 :(得分:0)
最后让它工作,它没有问题退出,谢谢大家!1:
#!/usr/bin/python
import sys
import re
import requests
import subprocess
import os
import urllib2
#def bash_command(cmd):
# subprocess.Popen(cmd, shell=True, executable='/bin/bash')
command=("curl -XN -u user:pass -X GET -H \"Content-Type:application/json\" https://jira.company.com/rest/api/2/search?jql=project='\"Technology\"+AND+summary~\"%20User*\"+AND+issuetype=\"Task\"+AND+status!=\"DONE\"' | python -m json.tool > /var/lib/rundeck/1.json")
p = subprocess.Popen(command, shell=True, executable='/bin/bash' ,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
答案 2 :(得分:-1)
我认为您正在从Jira页面读取一些数据并将其写入本地文件......是吗?
如果是这样,最好采用Urllib的GET请求以正常方式获取数据,然后将该数据写入所需文件
为此
output = urllib.request.urlopen('http://www.somewebsite.com').read()
#write to file
如果您仍想使用curl,请正确检查输出数据并将数据转储到文件
我尝试了这个简单的步骤并且有效:
import os
cmd = 'curl https://randomuser.me/api/ -o myoutput.json'
os.system(cmd)