I have a URL which gives me the below JSON String if I hit them on the browser -
Below is my URL, let's say it is URL-A and I have around three URL's -
http://hostnameA:1234/Service/statistics?%24format=json
And below is my JSON String -
{
"description": "",
"statistics": {
"dataCount": 0,
}
}
Now I have written a Python script which is scanning all my 3 URL's and then parse then JSON String to extract the value of dataCount from it. And it should keep on running every few seconds to scan the URL and then parse it.
Below are my URL's
hostnameA http://hostnameA:1234/Service/statistics?%24format=json
hostnameB http://hostnameB:1234/Service/statistics?%24format=json
hostnameC http://hostnameC:1234/Service/statistics?%24format=json
And the data I want to see is like this on the console, here dataCount will be actual number
hostnameA - dataCount
hostnameB - dataCount
hostnameC - dataCount
And I have the below python script which works fine locally on my cygwin but if I am running it on my company's production ubuntu machine it gives an error -
import requests
from time import sleep
def get_data_count(url):
try:
req = requests.get(url)
except requests.ConnectionError:
return 'could not get page'
try:
# this line is giving an error
return int(req.json['statistics']['dataCount'])
except TypeError:
return 'field not found'
except ValueError:
return 'not an integer'
def main():
urls = [
('hostnameA', 'http://hostnameA:1234/Service/statistics?%24format=json'),
('hostnameB', 'http://hostnameB:1234/Service/statistics?%24format=json'),
('hostnameC', 'http://hostnameC:1234/Service/statistics?%24format=json')
]
while True:
print('')
for name, url in urls:
res = get_data_count(url)
print('{name} - {res}'.format(name=name, res=res))
sleep(10.)
if __name__=="__main__":
main()
Below is the error I am getting -
AttributeError: 'Response' object has no attribute 'json'
I am using Python 2.7.3 and Ubuntu 12.04 and the version of requests I am running is 0.8.2 (I guess this is the problem).
In any case, is there any way I can rewrite the above script using some other library apart from requests meaning just the portion of getting the data from server, we can use other libraries right?
Since I guess, I cannot update this package since it's our production ubuntu servers so I need to find some other way to do this.
requests0.8.2 is ancient and does not support.json. You can install a newer version in a virtualenv, which will then be used instead.virutualenv? I have never use it before. If you can guide me then it will be of great help. Not a python expert :(requests).