My code:
import os
from xml.dom import minidom
doc = minidom.parse("jobs.xml")
job = doc.getElementsByTagName("job")[0]
id = job.getElementsByTagName("id")[0]
name = job.getElementsByTagName("name")[0]
id_data = id.firstChild.data
name_data = name.firstChild.data
os.system('curl --compressed -H "Accept: application/xml" -X GET "http://localhost:19888/ws/v1/history/mapreduce/jobs/"')
>>> /home/ankit/rrd-xml/task.xml
I want to get the commands like
os.system('curl --compressed -H "Accept: application/xml" -X GET "http://localhost:19888/ws/v1/history/mapreduce/jobs/< variable value of name_data >"')
>>> /home/ankit/rrd-xml/task.xml
How can i do it in python itself?
Use single quotes around the command, or even triple quotes, so that the quotes that form part of the command are not confused with the Python string:
os.system('curl --compressed -H "Accept: application/xml" -X GET "http://localhost:19888/ws/v1/history/mapreduce/jobs/{}" >> /home/ankit/rrd-xml/task.xml'.format(name_data))
That will work, but it's not the best. It would be better for you to use Python to make the HTTP request. requests is a good module for this:
import requests
url = 'http://localhost:19888/ws/v1/history/mapreduce/jobs/{}'.format(name_data)
headers = {'Accept': 'application/xml'}
response = requests.get(url, headers=headers)
with open('/home/ankit/rrd-xml/task.xml', 'a') as outfile:
outfile.write(response.content)
This code will GET the URL, setting the Accept header to application/xml and append the response to the file. Compression is requested by default.
What you want is a simple string formatting.
import os
from xml.dom import minidom
doc = minidom.parse("jobs.xml")
job = doc.getElementsByTagName("job")[0]
id = job.getElementsByTagName("id")[0]
name = job.getElementsByTagName("name")[0]
id_data = id.firstChild.data
name_data = name.firstChild.data
url = 'http://localhost:19888/ws/v1/history/mapreduce/jobs/{name}'.format(name=name_data)
cmd = 'curl --compressed -H "Accept: application/xml" -X GET "{url}"'.format(url=url)
os.system(cmd)
Use subprocess. The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using the os.system function.
import subprocess
import shlex
command = shlex.split('curl --compressed -H "Accept: application/xml" -X GET http://localhost:19888/ws/v1/history/mapreduce/jobs/')
with open('/home/ankit/rrd-xml/task.xml', 'a') as f:
subprocess.Popen(command, stdout=f)