speedtest 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # author: Dean Do
  4. import re
  5. import subprocess
  6. import psutil
  7. import time
  8. import redis
  9. import json
  10. import multiprocessing
  11. import datetime
  12. import copy
  13. import xlwt
  14. hosts = 'serverlist'
  15. domain = "https://www.youtube.com"
  16. def sshproxy(action, port, *server):
  17. if action:
  18. ip, sshport, user, password = server
  19. cmd = '''sshpass -p "%s" ssh -qTfnN -D %s %s@%s -p %s''' % (
  20. password, port, user, ip, sshport)
  21. p = subprocess.call(cmd, shell=True)
  22. if not p:
  23. return True
  24. else:
  25. return False
  26. else:
  27. for netstat in psutil.net_connections():
  28. if netstat.laddr[1] == port:
  29. pid = netstat.pid
  30. p = psutil.Process(pid)
  31. p.kill()
  32. return True
  33. return
  34. def runtest(ip, sshport, user, password, port, q):
  35. username = "root"
  36. sshproxy(0, port)
  37. proxy = sshproxy('proxy', port, ip, sshport, username, password)
  38. if proxy:
  39. proxy = "127.0.0.1:%s" % port
  40. cmd = "curl -I -o /dev/null -s -w %%{time_total} --socks5-hostname %s %s" % (
  41. proxy, domain)
  42. l = []
  43. for i in range(6):
  44. p = subprocess.Popen(
  45. cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  46. stdout, err = p.communicate()
  47. if stdout:
  48. request_time = float(stdout.strip())
  49. l.append(request_time)
  50. if err:
  51. print err.strip()
  52. raw_data = copy.copy(l)
  53. l.pop(l.index(max(l)))
  54. l.pop(l.index(min(l)))
  55. avg = '%.2f' % (sum(l) / len(l))
  56. cmd = "curl --socks5-hostname %s ipinfo.io" % proxy
  57. p = subprocess.Popen(
  58. cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  59. stdout, err = p.communicate()
  60. if stdout:
  61. out = eval(stdout.strip())
  62. country = out.get("country")
  63. city = out.get("city")
  64. q.put([country, city, ip, avg, raw_data])
  65. sshproxy(0, port)
  66. def iplist(hosts):
  67. l = []
  68. port = 7070
  69. with open(hosts, 'r') as f:
  70. for line in f:
  71. if re.match('^#.*', line):
  72. continue
  73. line = line.strip()
  74. regex = re.search(
  75. '^(?P<ip>(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s+(?P<sshport>\d+?)\s+(?P<user>.*?)\s+(?P<sshpass>.*?)$',
  76. line)
  77. ip = regex.group('ip')
  78. sshport = regex.group('sshport')
  79. password = regex.group('sshpass')
  80. user = regex.group('user')
  81. l.append([ip, sshport, user, password, port])
  82. port += 1
  83. return l
  84. def xlsprepare():
  85. titles = ["IP", "COUNTRY", "CITY", "REQUEST TIME TEST 1", "REQUEST TIME TEST 2", "REQUEST TIME TEST 3",
  86. "REQUEST TIME TEST 4", "REQUEST TIME TEST 5", "REQUEST TIME TEST 6", "REQUEST TIME TEST AVG"]
  87. workbook = xlwt.Workbook(encoding='ascii')
  88. worksheet = workbook.add_sheet('Test Result')
  89. for index, item in enumerate(titles):
  90. worksheet.write(0, index, label=item)
  91. width = len(item) + 3
  92. worksheet.col(index).width = 256 * width
  93. worksheet.col(0).width = 256 * 15
  94. worksheet.col(2).width = 256 * 15
  95. return workbook, worksheet
  96. if __name__ == "__main__":
  97. tasks = []
  98. data = []
  99. q = multiprocessing.Queue()
  100. for server_info in iplist(hosts):
  101. ip, sshport, user, password, port = server_info
  102. tasks.append(multiprocessing.Process(target=runtest,
  103. name=ip, args=(ip, sshport, user, password, port, q)))
  104. for task in tasks:
  105. task.start()
  106. print task.name, "test start..."
  107. time.sleep(1)
  108. for task in tasks:
  109. task.join()
  110. n = 0
  111. workbook, worksheet = xlsprepare()
  112. while True:
  113. if not q.empty():
  114. value = q.get(True)
  115. data.append(value)
  116. print "\033[1;32;40mIP: %s\033[0m" % value[2]
  117. print "COUNTRY:", value[0]
  118. print "CITY:", value[1]
  119. print "REQUST TIME AVG:", value[3]
  120. print "=" * 22
  121. if 1:
  122. n += 1
  123. worksheet.write(n, 0, label=value[2])
  124. worksheet.write(n, 1, label=value[0])
  125. worksheet.write(n, 2, label=value[1])
  126. worksheet.write(n, 3, label=value[4][0])
  127. worksheet.write(n, 4, label=value[4][1])
  128. worksheet.write(n, 5, label=value[4][2])
  129. worksheet.write(n, 6, label=value[4][3])
  130. worksheet.write(n, 7, label=value[4][4])
  131. worksheet.write(n, 8, label=value[4][5])
  132. worksheet.write(n, 9, label=float(value[3]))
  133. else:
  134. workbook.save('result.xls')
  135. break