Skip to content

Instantly share code, notes, and snippets.

@najeira
Created October 3, 2011 08:03
Show Gist options
  • Save najeira/1258672 to your computer and use it in GitHub Desktop.
Save najeira/1258672 to your computer and use it in GitHub Desktop.
Check mediawiki responses for Tuningathon#2
# -*- coding: utf-8 -*-
import urllib
import urllib2
import time
MASTER_SERVER = '127.0.0.1'
PAGE_FORMAT = 'http://%s/mediawiki/index.php/%s'
TARGETS = [
u'ロシア',
u'スタジオジブリ',
]
IPS = """
192.168.0.1
192.168.0.2
"""
def get_page(server, word):
url = PAGE_FORMAT % (server, urllib.quote_plus(word.encode('utf-8')))
req = urllib2.Request(url)
req.add_header('User-Agent', 'http_load 12mar2006')
start = time.time()
for _ in range(3):
try:
f = urllib2.urlopen(req)
ret = f.read()
elapsed = time.time() - start
return ret.decode('utf-8'), elapsed, f.info()
except urllib2.URLError:
time.sleep(1)
raise
masters = {}
def check(server):
for word in TARGETS:
if word not in masters:
master, _, _ = get_page(MASTER_SERVER, word)
masters[word] = master
else:
master = masters[word]
user, user_elapsed, user_info = get_page(server, word)
master_len = len(master)
user_len = len(user)
diff_len = abs(master_len - user_len)
diff_per = float(diff_len) / float(master_len) * 100
print u'%s: %s => %s sec.' % (server, word, user_elapsed)
if 1 < diff_per:
print u'NG: %.03f%%' % diff_per
else:
print u'OK: %.03f%%' % diff_per
if word not in user:
print u'NG: the word was not found in page.'
else:
print u'OK: the word was found in page.'
if '/mediawiki/skins/common/images/wiki.png' not in user:
print u'NG: the page may not be original.'
else:
print u'OK: the page may be original.'
print '----------------'
print user_info
print '================'
def main():
from optparse import OptionParser
parser = OptionParser()
options, args = parser.parse_args()
if not args:
print 'Argument server IP address required.'
return
server = args[0]
check(server)
def main2():
for ip in IPS.splitlines():
if ip and 5 <= len(ip):
try:
check(ip)
except Exception, ex:
print 'Error: %s: %s' % (ip, ex)
if __name__ == "__main__":
#main()
main2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment