-3

How do i extract the required data from the output and save it to a variable

this is my program:

import commands

cmd = curl -v -k -H "Content-Type: application/json" -X GET -u hitman:hitman <https://test.com:8181/v1/config/sipregistrar

result = commands.getoutput(cmd)
print result

now run the programm :

python test25.py

output :

About to connect() to 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 to 50.50.50.201 (50.50.50.201) port 8181 (#0)
successfully 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 this output , the params i 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 do i extract the above and save it to a variable ?

4

2 回答 2

2

If you just need a string of those params and all your output always follows that pattern and nothing in your [data not shown] sabotages this, you could try ...

params = results[results.find('{"'):]

That takes the portion of results that starts at {"sip_domains" If you end up having another {" before hand, though, this will break.

于 2012-06-05T14:04:44.553 回答
0

I have cleaned this up, removed eval, and converted the result using JSON, to make it safe:

import json
from 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 now contains this 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 is simply an alternative, and @jcfollower has a +1 from me for a succinct and effective solution.

于 2012-06-05T14:30:45.073 回答