User:Eleanorg/Thematic1.1/Make hops into %

From XPUB & Lens-Based wiki
< User:Eleanorg
Revision as of 00:59, 3 December 2011 by Eleanorg (talk | contribs)

Converts hop count to %. 1 hop is around 2%, 30 or more is 75%.

traceroute default max hop count is 30; left unchanged here as tracing over 30 is sloooow, but 30 hops will be converted to 75% only - sites in far east etc often have over 50 hops from Europe!

  • Question - is there a way of getting a more accurate hopcount for sites with 30 or more hops, without having to go thru very slow traceroute count?
#!/usr/bin/python
#-*- coding:utf-8 -*-

import os				      # lets you use system commands inside the python script


###---------- GET NO. OF HOPS--------------

site = "mysite.org"
command = "traceroute " + site + " | wc -l"   # concatenates your site variable inside command to be sent to system
traceroute = os.popen(command,'r')            # popen function opens system command with argument 'r' for 'read'
hopcount = float(traceroute.read()) - 1
print(hopcount)

###--------- CONVERT TO %------------------

percent = hopcount / 40                       
print(percent)