Last active
April 6, 2016 06:12
-
-
Save svalleru/fde95314c61ac22d8dae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| __author__ = 'svalleru' | |
| # simple script for network latency debugging | |
| import time | |
| from optparse import OptionParser | |
| from urllib2 import Request, urlopen | |
| def url_fetcher(url, count): | |
| print "trying url:", url, "with retry count:", count | |
| for attempt in range(1, count): | |
| if attempt > 1: | |
| print 'Making another attempt (%d)' % attempt | |
| try: | |
| return urlopen(Request(url)) | |
| except Exception, ex: | |
| print str(ex) | |
| time.sleep(2) | |
| else: | |
| print "timeout. exiting.." | |
| exit() | |
| if __name__ == '__main__': | |
| parser = OptionParser() | |
| parser.add_option("-u", dest="url", help="url of ks file", type=str) | |
| parser.add_option("-c", dest="count", help="retry count", type=int) | |
| (options, args) = parser.parse_args() | |
| start_time = time.time() * 1000 | |
| rfp = url_fetcher(options.url or "http://www.linuxdevcenter.com/2004/08/19/examples/sample-ks.cfg", | |
| options.count or 16) | |
| print "took", (time.time() * 1000) - start_time, "ms to fetch remote file handle" | |
| start_time = time.time() * 1000 | |
| with open('ks.cfg', 'wb') as f: | |
| f.write(rfp.read()) | |
| print "took", (time.time() * 1000) - start_time, "ms to write to local file" | |
| # -bash-4.1$ weasel svalleru$ python fetcher.py -h | |
| # Usage: fetcher.py [options] | |
| # | |
| # Options: | |
| # -h, --help show this help message and exit | |
| # -u URL url of ks file | |
| # -c COUNT retry count | |
| # -bash-4.1$ python fetcher.py -u http://usgcb.nist.gov/usgcb/content/configuration/workstation-ks.cfg -c 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment