main_netrate.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free Software
  8. # Foundation, either version 3 of the License, or (at your option) any later
  9. # version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU General Public License along with
  17. # this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmnode-netrate: Bitcoin daemon network rate monitor
  20. """
  21. import sys,time
  22. from mmgen.cfg import Config
  23. from mmgen.util import async_run
  24. opts_data = {
  25. 'text': {
  26. 'desc': 'Bitcoin daemon network rate monitor',
  27. 'usage': '[opts]',
  28. 'options': """
  29. -h, --help Print this help message
  30. --, --longhelp Print help message for long options (common options)
  31. """
  32. }
  33. }
  34. cfg = Config(opts_data=opts_data)
  35. ERASE_LINE,CUR_UP = '\033[K','\033[1A'
  36. async def main():
  37. from mmgen.rpc import rpc_init
  38. c = await rpc_init(cfg,ignore_wallet=True)
  39. async def get_data():
  40. d = await c.call('getnettotals')
  41. return [float(e) for e in (d['totalbytesrecv'],d['totalbytessent'],d['timemillis'])]
  42. rs,ss,ts = (None,None,None)
  43. while True:
  44. r,s,t = await get_data()
  45. if rs is not None:
  46. sys.stderr.write(
  47. '\rrcvd: {:9.2f} kB/s\nsent: {:9.2f} kB/s '.format(
  48. (r-rs)/(t-ts),
  49. (s-ss)/(t-ts) ))
  50. time.sleep(2)
  51. if rs is not None:
  52. sys.stderr.write('{}{}{}'.format(ERASE_LINE,CUR_UP,ERASE_LINE))
  53. rs,ss,ts = (r,s,t)
  54. try:
  55. async_run(main())
  56. except KeyboardInterrupt:
  57. sys.stderr.write('\n')