1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#!/usr/bin/python
# This script is part of the piwik project
# author : Johan Mathe <johan.mathe@tremplin-utc.net>
# Licence : GPL
# works with the following apache log config :
# LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\"" combinedcookie-stressme
#TODO
#cookie
#headers IP;browser etc
#+ expliquer quels params changer pour duree visite
import getopt
import sys
import os
import time
import urllib2
class player:
def __init__(self, logFile, hostName, delay = 0, verbose = False):
self.logFile = logFile;
self.hostName = hostName;
self.delay = delay
self.verbose = verbose
def play(self):
fd = open(self.logFile)
for line in fd.readlines():
fields = line.split(' ')
uri = fields[6]
url = "http://%(w)s/%(u)s" % {'w' : self.hostName, 'u' : uri};
if self.verbose:
print url
try:
req = urllib2.Request(url)
handle = urllib2.urlopen(req)
the_page = handle.read()
except IOError, e:
if e.code == 404:
print "404 error : %s" % url
else:
print "Unknown error while opening URL %s" % url
except KeyboardInterrupt:
print "Keyboard interrupt"
sys.exit()
time.sleep(self.delay)
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def usage():
print "--help : show this help\n--log : logfile\n--host : host name\n--delay : delay in sec"
def main(argv=None):
hostName = ""
logFile = ""
verbose = False
delay = 0
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "vhf:h:d:", ["help","log=","host=","delay="])
except getopt.error, msg:
raise Usage(msg)
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("f", "--log"):
logFile = a
elif o in ("h", "--host"):
hostName = a
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-d", "--delay"):
delay = float(a)
else:
assert False, "unhandled option"
if not hostName or not logFile:
raise Usage("Plase specify the host name and the log file.")
p = player(logFile, hostName, delay, verbose)
p.play()
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())
|