Wednesday, February 10, 2010

Python script to time HTTP requests via a proxy server

Like the title says - I just want to time HTTP requests via different proxy servers to compare response times.

I hadn't really used Python decorators before, but once you get your head around them they're an extremely useful design pattern.
#!/usr/bin/env python

# Script to time HTTP requets via a proxy server.

# Sample output:
# proxy01 [ http://www.news.com.au ] took 222.687 ms
# proxy01 [ http://www.google.com ] took 134.649 ms
# proxy02 [ http://www.news.com.au ] took 1387.021 ms
# proxy02 [ http://www.google.com ] took 4197.468 ms

import urllib, time

proxies = ('proxy01', 'proxy02')
urls = ('http://www.news.com.au', 'http://www.google.com')

# Decorator for timing functions
def timer(f):
def wrapper(*arg):
t_start = time.time()
r = f(*arg)
t_finish = time.time()
print '%s [ %s ] took %0.3f ms' % (proxy, url, (t_finish-t_start)*1000.0)
return r
return wrapper

@timer
def testProxy(proxy, url):
proxy = 'http://username:password@%s.localdomain:8080' % (proxy)
proxy_list = {'http': proxy}
try:
f = urllib.urlopen(url, proxies=proxy_list)
html = f.read()
except URLError, e:
print e.reason

# Main
if __name__ == "__main__":
for proxy in proxies:
for url in urls:
testProxy(proxy, url)

1 comments:

  1. Jason :) How you been, can you give me a call , 0405160448 - or an email eoccy@yahoo.com

    Your old mate Grant :P

    ReplyDelete