User:Eleanorg/Thematic1.1/Get no. of hops: Difference between revisions
(Created page with "Uses traceroute system command to return the number of hops for a specified webpage, in variable 'hopcount'. <script lang=python> #!/usr/bin/python #-*- coding:utf-8 -*- impo...") |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
Uses traceroute system command to return the number of hops for a specified webpage, in variable 'hopcount'. | Uses traceroute system command to return the number of hops for a specified webpage, in variable 'hopcount'. | ||
< | <source lang=python> | ||
#!/usr/bin/python | #!/usr/bin/python | ||
Line 8: | Line 8: | ||
import os # lets you use system commands inside the python script | import os # lets you use system commands inside the python script | ||
site = " | site = "mysite.org" | ||
command = "traceroute " + site + " | wc -l" # concatenates your site variable inside command to be sent to system | 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' | traceroute = os.popen(command,'r') # popen() function opens system command with argument 'r' for 'read' | ||
Line 14: | Line 14: | ||
print(hopcount) | print(hopcount) | ||
</ | </source> |
Latest revision as of 23:59, 2 December 2011
Uses traceroute system command to return the number of hops for a specified webpage, in variable 'hopcount'.
#!/usr/bin/python
#-*- coding:utf-8 -*-
import os # lets you use system commands inside the python script
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 = int(traceroute.read()) - 1 # turns no. of lines returned by traceroute into an int, subtracting 1 (the summary line)
print(hopcount)