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
asked Oct 10, 2018 at 11:50
bugnet17
1811 gold badge2 silver badges12 bronze badges
1 Answer 1
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)
answered Oct 10, 2018 at 12:03
chepner
538k77 gold badges595 silver badges747 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
\'requestsinstead of runningcurl.