#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Parsing web server accounts in JSON format


    Hi everyone, I'm new to Python and I'm trying to write a program that will read the accounts on my webserver and output a list in JSON format. This is what I have so far (code below), but I'm not really sure where to go from here. Could someone help point me in the right direction?

    #!/usr/bin/python
    lines=exec("cat /etc/userdomains")
    json_string="["
    for line in lines:
    json_string+=line
    json_string+="]"
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0
    Try this:
    Code:
    #!/usr/bin/python 
    import os 
    import simplejson  
    userdoms=os.popen("cat /etc/userdomains").read().split("\n")[0:-1] 
    json_string=simplejson.dumps(userdoms)
    print(json_string)
    Then you can enter that in your javascript code and for loop through it. Or if your using python to render the code use something like mako templates and pass json_string to your template.

    HTH
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0
    Originally Posted by nickpython
    Try this:
    Code:
    #!/usr/bin/python 
    import os 
    import simplejson  
    userdoms=os.popen("cat /etc/userdomains").read().split("\n")[0:-1] 
    json_string=simplejson.dumps(userdoms)
    print(json_string)
    Then you can enter that in your javascript code and for loop through it. Or if your using python to render the code use something like mako templates and pass json_string to your template.

    HTH
    Thanks, that worked!

IMN logo majestic logo threadwatch logo seochat tools logo