Using Redis in iPython

sbalasa

Santhosh Balasa

Posted on May 31, 2021

Using Redis in iPython
In [1]: import redis

In [2]: r = redis.Redis(host='localhost', port=6379, db=0)

In [3]: re = [
   ...:         {
   ...:             "cpu_util": "18",
   ...:             "mem_size": 4194303.9990234375,
   ...:             "mem_util": 0.0,
   ...:             "model": None,
   ...:             "re_temp": None,
   ...:             "reboot_reason": None,
   ...:             "role": "master",
   ...:             "serial":None,
   ...:             "slot": "0",
   ...:             "state": "OK",
   ...:             "status": 0,
   ...:             "id": "306f2fd7-229f-4aa8-b2c0-e420c85dcc3a",
   ...:             "cpu_temp": "",
   ...:             "cpu_temp_dgr": "",
   ...:             "load": 0,
   ...:             "re_temp_dgr": "",
   ...:             "role_priority": "",
   ...:             "up_datetime": "",
   ...:             "up_datetime_sec": "",
   ...:             "uptime": ""
   ...:         }
   ...:     ]

In [4]: re
Out[4]:
[{'cpu_util': '18',
  'mem_size': 4194303.9990234375,
  'mem_util': 0.0,
  'model': None,
  're_temp': None,
  'reboot_reason': None,
  'role': 'master',
  'serial': None,
  'slot': '0',
  'state': 'OK',
  'status': 0,
  'id': '306f2fd7-229f-4aa8-b2c0-e420c85dcc3a',
  'cpu_temp': '',
  'cpu_temp_dgr': '',
  'load': 0,
  're_temp_dgr': '',
  'role_priority': '',
  'up_datetime': '',
  'up_datetime_sec': '',
  'uptime': ''}]

In [5]: hostname = 'at-vie05d-rt1'

In [6]: hostname
Out[6]: 'at-vie05d-rt1'

In [7]: r.hset(f'router:{hostname}', 're', str(re))
Out[7]: 1

In [8]: v = r.hget(f'router:{hostname}', 're')

In [9]: import yaml

In [10]: re_from_redis = yaml.load(v)

In [11]: re_from_redis
Out[11]:
[{'cpu_util': '18',
  'mem_size': 4194303.9990234375,
  'mem_util': 0.0,
  'model': 'None',
  're_temp': 'None',
  'reboot_reason': 'None',
  'role': 'master',
  'serial': 'None',
  'slot': '0',
  'state': 'OK',
  'status': 0,
  'id': '306f2fd7-229f-4aa8-b2c0-e420c85dcc3a',
  'cpu_temp': '',
  'cpu_temp_dgr': '',
  'load': 0,
  're_temp_dgr': '',
  'role_priority': '',
  'up_datetime': '',
  'up_datetime_sec': '',
  'uptime': ''}]
Enter fullscreen mode Exit fullscreen mode
In [43]: redis_client.hmset(f'router:{hostname}', {'data': str(data)})

Enter fullscreen mode Exit fullscreen mode

-> Redis on windows:

winpty docker exec -it 8e76eea66688 redis-cli GET cnt:iface:72e76f51-1d54-4742-9462-1e29d0e88edb
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
sbalasa
Santhosh Balasa

Posted on May 31, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related