extracting the output using python -


how extract required data output , save variable

this program:

import commands  cmd = curl -v -k -h "content-type: application/json" -x -u hitman:hitman <https://test.com:8181/v1/config/sipregistrar  result = commands.getoutput(cmd) print result 

now run programm :

python test25.py

output :

about connect() test.com port 8181 (#0) trying test.com...   % total    % received % xferd  average speed   time    time     time  current dload  upload   total   spent    left  speed 0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0connected connected 50.50.50.201 (50.50.50.201) port 8181 (#0) set certificate verify locations: http/1.1 200 ok date: tue, 05 jun 2012 18:27:11 gmt server: jetty(6.1.22) content-type: application/json;charset=utf-8 expires: thu, 01 jan 1970 00:00:00 gmt set-cookie: jsessionid=12buplms5odzt;path=/config transfer-encoding: chunked closing connection #0  sslv3, tls alert, client hello (1): } [data not shown] {"sip_domains":{"prefix":[{"name":""}],"domain":[{"name":"k200.com"},{"name":"zinga.com"},{"name":"rambo.com"}]},"sip_security":{"level":2},"sip_trusted_hosts":{"host":[]},"sip_proxy_mode":{"handle_requests":1}} 

from output , params require :

{"sip_domains":{"prefix":[{"name":""}],"domain":[{"name":"k200.com"},{"name":"zinga.com"},{"name":"rambo.com"}]},"sip_security":{"level":2},"sip_trusted_hosts":{"host":[]},"sip_proxy_mode":{"handle_requests":1}}

how extract above , save variable ?

i have cleaned up, removed eval, , converted result using json, make safe:

import json pprint import pprint  output = '''[data extract]set-cookie: jsessionid=12buplms5odzt;path=/config transfer-encoding: chunked closing connection #0  sslv3, tls alert, client hello (1): } [data not shown] {"sip_domains":{"prefix":[{"name":""}],"domain":[{"name":"k200.com"},{"name":"zinga.com"},{"name":"rambo.com"}]},"sip_security":{"level":2},"sip_trusted_hosts":{"host":[]},"sip_proxy_mode":{"handle_requests":1}}'''  output = json.loads( output.split("\n")[-1] )  pprint( output ) 

the variable output contains dictionary:

{'sip_domains': {'domain': [{'name': 'k200.com'},                             {'name': 'zinga.com'},                             {'name': 'rambo.com'}],                  'prefix': [{'name': ''}]},  'sip_proxy_mode': {'handle_requests': 1},  'sip_security': {'level': 2},  'sip_trusted_hosts': {'host': []}} 

this alternative, , @jcfollower has +1 me succinct , effective solution.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -