-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
executable file
·61 lines (52 loc) · 1.56 KB
/
runner.py
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
#!/usr/bin/env python3
"""
Hit the target endpoints with GET requests
"""
import sys
from argparse import ArgumentParser, RawDescriptionHelpFormatter as RDHF
from urllib.request import urlopen
from threading import Thread
def read_url(url):
"""
Get request targeting the provided endpoint
:param url: A URL to submit a GET request to
:return: None
"""
try:
urlopen(url)
except:
excp = sys.exc_info()[1]
print("Failed to open {u}: {e}".format(u=url, e=excp))
def get_it(base_url, n=100):
"""
Send n GET requests to the provided url.
This involves ranging between 0 and 100 (exclusive)
and hitting /sleep if i is even.
:param base_url: The base url of the web server (landing page)
:param n: The number of requests to send
:return: List of Thread objects to be joined
"""
threads = []
for i in range(n):
if i % 2 == 0:
t = Thread(target=read_url, args=("{}/sleep".format(base_url),))
else:
t = Thread(target=read_url, args=(base_url,))
t.start()
threads.append(t)
return threads
if __name__ == '__main__':
parser = ArgumentParser(description=__doc__, formatter_class=RDHF)
parser.add_argument(
'--url', '-u',
help='The base of the target url',
default='http://127.0.0.1:7878'
)
parser.add_argument(
'--number', '-n',
help='Number of GET requests to send',
default=100,
type=int
)
args = parser.parse_args()
[tr.join() for tr in get_it(args.url, args.number)]