by doyousketch2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.5 KiB

  1. #!/usr/bin/env python2.7
  2. # -*- coding: utf-8 -*-
  3. ##=========================================================
  4. ## pingmt.py 7 Mar 2018
  5. ##
  6. ## Eli Innis @Doyousketch2
  7. ## Doyousketch2 @ yahoo.com
  8. ##
  9. ## GNU GPLv3 gnu.org/licenses/gpl-3.0.html
  10. ##=========================================================
  11. ## required ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. ## (linux)
  13. ## sudo apt-get install fping
  14. ##=========================================================
  15. ## libs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  16. import json
  17. import urllib
  18. import operator
  19. import time
  20. from subprocess import Popen, PIPE
  21. begin = time.time()
  22. ##=========================================================
  23. ## get json ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. ## how many top servers printed on commandline?
  25. ## read 'pinglist.txt' for the rest
  26. howmany = 30
  27. url = 'http://servers.minetest.net/list'
  28. servers = []
  29. ports = []
  30. response = urllib .urlopen(url)
  31. data = json .loads( response .read() )
  32. for key, val in enumerate( data['list'] ):
  33. addy = val['address']
  34. port = str(val['port'])
  35. if addy in servers:
  36. i = 1
  37. while servers[i] < addy:
  38. i += 1
  39. ports[i] = ports[i] + ', ' + port
  40. else:
  41. servers .append( addy )
  42. ports .append( port )
  43. ## for testing purposes...
  44. # servers = ['192.168.1.1', '192,168.1.255', '8,8,8,8', 'github.com']
  45. # ports = ['30000', '30001', '30002', '30003']
  46. ##=========================================================
  47. ## ping 'em ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. print('\nPinging {} servers...\n'.format( len(servers) ))
  49. responses = []
  50. for server in servers:
  51. try:
  52. ping = Popen( ['fping', '-e', '--timeout=333', server], stdout=PIPE, stderr=PIPE)
  53. stdout, stderr = ping .communicate()
  54. if stderr is not None and stdout[-12:-1] != 'unreachable' and len(stdout) > 8:
  55. responses .append( stdout )
  56. if stdout[-7] == '.' or stdout[-8] == '.':
  57. print(stdout)
  58. except:
  59. pass
  60. ##=========================================================
  61. ## sort 'em ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. i = 0
  63. for addr in responses:
  64. if addr[-8] == '.':
  65. text = '00' + addr[-10:-4] + 'ms ' + addr[:-21]
  66. if addr[-7] == '.':
  67. text = '0' + addr[-9:-4] + 'ms ' + addr[:-20]
  68. else:
  69. text = addr[-8:-4] + 'ms ' + addr[:-19]
  70. responses[i] = text
  71. i += 1
  72. combined = zip(responses, ports)
  73. pingsorted = sorted( combined, key=lambda x: x[0] )
  74. outputtext = []
  75. for i in range( len(pingsorted) ):
  76. text = pingsorted[i][0] .lstrip('0') .lstrip('0') + ' : ' + pingsorted[i][1]
  77. outputtext .append( text )
  78. ##=========================================================
  79. ## save 'em ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80. output = open('./pinglist.txt', 'w')
  81. date = time .strftime( '%d %b %Y' )
  82. output .write('{} https://github.com/doyousketch2/Minetest-ping-sort\n\n'.format( date ) )
  83. output .write(' {} Minetest servers, sorted by ping speed.\n'.format( len(pingsorted) ))
  84. print( '\n\n Your top {} servers are:\n'.format(howmany) )
  85. top = 0
  86. for addr in outputtext:
  87. output .write( '{}\n'.format( addr ))
  88. if top < howmany :
  89. print( addr )
  90. top += 1
  91. output .write('\n')
  92. output .close()
  93. ##=========================================================
  94. ## done ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. elapsed = time.time() -begin
  96. print( '\nCompleted in {:.3f} seconds.'.format(elapsed) )
  97. print( '\nSee pinglist.txt for complete listing.' )