Skip to content

Instantly share code, notes, and snippets.

@mrbald
Created July 17, 2020 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrbald/c275785ffb8ccea27b8cc970a5ba5b06 to your computer and use it in GitHub Desktop.
Save mrbald/c275785ffb8ccea27b8cc970a5ba5b06 to your computer and use it in GitHub Desktop.
Jolokia API request with python
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