Here's the code:
inputDomain = subprocess.Popen("cat /etc/localdomains", shell=True, stdout=subprocess.PIPE)
domains = inputDomain.stdout.read().splitlines()
for domain in domains:
cmd = "whmapi1 domainuserdata domain " + domain
output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
jsonS = json.dumps(output.communicate())
print json.loads(jsonS)['data']
here's there error
root@server [~/testP]# python copie.py Traceback (most recent call last): File "copie.py", line 18, in print json.loads(jsonS)['data'] TypeError: list indices must be integers, not str
and this is an example of the json i need to parse:
{
"data":{
"userdata":{
"phpopenbasedirprotect":1,
"options":"ExecCGI Includes",
"ip":"10.0.0.1",
"hascgi":"1",
"group":"user",
"usecanonicalname":"Off",
"scriptalias":[
{
"url":"/cgi-bin/",
"path":"/home/user/public_html/cgi-bin"
},
{
"url":"/cgi-bin/",
"path":"/home/user/public_html/cgi-bin/"
}
],
"user":"user",
"ifmodulemodsuphpc":{
"group":"user"
},
"owner":"root",
"documentroot":"/home/user/public_html",
"userdirprotect":"",
"serveralias":"parkeddomain.com www.parkeddomain.com www.example.com",
"port":"80",
"homedir":"/home/user",
"ifmoduleconcurrentphpc":{
},
"customlog":[
{
"target":"/usr/local/apache/domlogs/example.com",
"format":"combined"
},
{
"target":"/usr/local/apache/domlogs/example.com-bytes_log",
"format":"\"%{%s}t %I .\\n%{%s}t %O .\""
}
],
"servername":"example.com",
"serveradmin":"[email protected]"
}
}
So i need the user and the domaine, but python always answer that i need a int. Thanks for the help guys.
jsonS
is not a list? in which case you'd have to dojson.loads(jsonS)[0]['data']
– UrbanizeinputDomain = subprocess.Popen("cat /etc/localdomains", shell=True, stdout=subprocess.PIPE)
? python can read text files without the need of callingcat
!! – Urbanizeprint(type(jsonS))
just to see the type – Urbanize