I'm trying to run bash commands from python3 script and I get an error. Command:
#!/usr/bin/python3
import os
os.system('curl -k --header "Authorization: 3NKNRNNUrFQtu4YsER6" --header "Accept: application/json" --header "Content-Type: application/json" https://192.168.1.1/alerts/index/limit:1/page:1/sort:id/direction:desc.json | jq -r ''{"request": {"alert": {"alert": .[0].alert, "new": "test"}}}'' > 1.json')
Error response:
jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at
<top-level>, line 1:
{request:
(23) Failed writing body
There's no need to use curl
and jq
; Python has libraries to handle both HTTP requests and JSON data. ( requests
is a 3rd-party library; json
is part of the standard library.)
import json
import requests
with open("1.json", "w") as fh:
response = requests.get("https://192.168.1.1/alerts/index/limit:1/page:1/sort:id/direction:desc.json",
headers={"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "3NKNRNNUrFQtu4YsER6"
}
).json()
json.dump(fh, {'request': {'alert': {'alert': response[0]['alert'], 'new': 'test'}}})
If you insist on using curl
and jq
, use the subprocess
module instead of os.system
.
p = subprocess.Popen(["curl", "-k",
"--header", "Authorization: 3NKNRNNUrFQtu4YsER6",
"--header", "Accept: application/json",
"--header", "Content-Type: application/json",
"https://192.168.1.1/alerts/index/limit:1/page:1/sort:id/direction:desc.json"
], stdout=subprocess.PIPE)
with open("1.json", "w") as fh:
subprocess.call(["jq", "-r", '{request: {alert: {alert: .[0].alert, new: "test"}}}'],
stdin=p.stdout,
stdout=fh)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.