Closed majurgens closed 2 years ago
This is not part of the library but it is easy to create something like this:
import asyncio
import json
from datetime import datetime, timedelta
from aiowmi.connection import Connection
from aiowmi.query import Query
from aiowmi.exceptions import WbemFalse
def encode_value(value):
if isinstance(value, datetime):
# you need to choose some JSON compatible datetime conversion
return value.isoformat()
if isinstance(value, timedelta):
# you need to choose some JSON compatible timedelta conversion
return value.microseconds #
return value
async def get_json_output(query, conn, service):
await query.start(conn, service)
result = []
while True:
try:
res = await query.next()
except WbemFalse:
break
props = res.get_properties()
item = {name: encode_value(prop.value) for name, prop in props.items()}
result.append(item)
return json.dumps(result)
async def main():
host = IP_ADDRESS_OR_HOSTNAME
username = MY_USERNAME
password = MY_SECRET_PASSWORD
domain = OPTIONAL_DOMAIN
# Just some query...
query = Query('SELECT * FROM Win32_NetworkAdapter')
service = None
conn = Connection(host, username, password, domain=domain)
await conn.connect()
try:
service = await conn.negotiate_ntlm()
out = await get_json_output(query, conn, service)
print(out) # print the JSON output
except Exception:
raise
finally:
if service:
service.close()
conn.close()
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
Thanks for the great response
Can we get JSON output from the library? Maybe the output is already convertible to JSON output? I'm not very good with python and so maybe this is already available