Created
July 17, 2020 16:09
-
-
Save mrbald/c275785ffb8ccea27b8cc970a5ba5b06 to your computer and use it in GitHub Desktop.
Jolokia API request with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import with_statement | |
import argparse | |
import requests | |
from requests.auth import HTTPBasicAuth | |
from contextlib import closing | |
class PyJmx: | |
""" | |
Created class instances can/should be kept around and reused | |
as they use requests.Session, which keep-alive the TCP connection | |
""" | |
def __init__(self, url, login, password): | |
self.session = requests.Session() | |
self.url = url | |
self.auth = HTTPBasicAuth(login, password) | |
def mget(self, mbean, attrs): | |
with closing(self.session.post(self.url, auth=self.auth, json={ | |
'type': 'read', | |
'mbean': mbean, | |
'attribute': attrs | |
})) as response: | |
response.raise_for_status() | |
return response.json() | |
def close(self): | |
self.session.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment