-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttp-tester.py
executable file
·208 lines (171 loc) · 6.11 KB
/
http-tester.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
from threading import Thread
from httplib import HTTPConnection
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from datetime import datetime, timedelta
from bcolor import bcolors
import sys
import time
class TestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/basic":
cdata = open("./basic", "r").read()
if self.path == "/basic2":
cdata = open("./basic2", "r").read()
if self.path == "/basic3":
cdata = open("./basic3", "r").read()
time.sleep(3)
if self.path == "/cacheTest":
cdata = str(time.time())
size = len(cdata)
expireDate=(datetime.now()+timedelta(days=1)).strftime("%a, %d %b %Y %H:%M:%S GMT")
lastModify=(datetime.now()+timedelta(days=-1)).strftime("%a, %d %b %Y %H:%M:%S GMT")
self.send_response(200)
self.send_header('Content-Type','text/html')
self.send_header('Content-Length', str(size))
self.send_header('Expires',expireDate)
self.send_header('Last-Modified', lastModify)
if self.close_connection == True:
self.send_header('Connection', 'close')
self.end_headers()
self.wfile.write(cdata)
return
class ServerThread (Thread):
def __init__(self, port):
Thread.__init__(self)
self.port = port
def run(self):
try:
TestHandler.protocol_version = "HTTP/1.1"
self.server = HTTPServer(('', self.port), TestHandler)
self.server.serve_forever()
except KeyboardInterrupt:
self.server.socket.close()
class ClientThread (Thread):
def __init__(self, proxy, url, file):
Thread.__init__(self)
self.proxy = proxy
self.url = url
self.file = file
self.result = False
self.data = ""
def run(self):
if self.file:
dataFile = open(self.file, "r")
cdata = dataFile.read()
conn = HTTPConnection(self.proxy)
conn.request("GET", self.url)
resp = conn.getresponse()
rdata = resp.read()
if rdata == cdata:
self.result = True
self.data = rdata
conn.close()
dataFile.close()
else:
conn = HTTPConnection(self.proxy)
conn.request("GET", self.url)
resp = conn.getresponse()
rdata = resp.read()
if resp.status == httplib.OK:
self.result = True
conn.close()
class ClientPersistThread(Thread):
def __init__(self, proxy, url, file, url2, file2):
Thread.__init__(self)
self.proxy = proxy
self.url = url
self.file = file
self.url2 = url2
self.file2 = file2
self.result = False
def run(self):
conn = HTTPConnection(self.proxy)
tmpFlag = True
dataFile = open(self.file, "r")
cdata = dataFile.read()
dataFile = open(self.file2, "r")
cdata2 = dataFile.read()
conn.request("GET", self.url)
resp = conn.getresponse()
rdata = resp.read()
if rdata != cdata:
tmpFlag = False
if resp.will_close == True:
tmpFlag = False
connHdrs = {"Connection": "close"}
conn.request("GET", self.url2, headers=connHdrs)
resp = conn.getresponse()
rdata2 = resp.read()
if rdata2 != cdata2:
tmpFlag = False
if resp.will_close == False:
tmpFlag = False
if tmpFlag == True:
self.result = True
conn.close()
dataFile.close()
conf = open("./portconf", "r")
pport = conf.readline().rstrip().split(':')[1]
sport1 = conf.readline().rstrip().split(':')[1]
sport2 = conf.readline().rstrip().split(':')[1]
b1 = open("./basic", "w")
b1.write("basic\n")
b2 = open("./basic2", "w")
b2.write("aloha\n")
b3 = open("./basic3", "w")
b3.write("cat\n")
b1.close()
b2.close()
b3.close()
server1 = ServerThread(int(sport1))
server2 = ServerThread(int(sport2))
server1.start()
server2.start()
client1 = ClientThread("127.0.0.1:" + pport, "http://127.0.0.1:" + sport1 + "/basic", "./basic")
client1.start()
client1.join()
if client1.result:
print "Basic object fetching: [" + bcolors.PASS + "PASSED" + bcolors.ENDC + "]"
else:
print "Basic object fetching: [" + bcolors.FAIL + "FAILED" + bcolors.ENDC + "]"
client2 = ClientPersistThread("127.0.0.1:" + pport, "http://127.0.0.1:" + sport1 + "/basic", "./basic", "http://127.0.0.1:" + sport1 + "/basic2", "./basic2")
client2.start()
client2.join()
if client2.result:
print "Persistent Connection: [" + bcolors.PASS + "PASSED" + bcolors.ENDC + "]"
else:
print "Persistent Connection: [" + bcolors.FAIL + "FAILED" + bcolors.ENDC + "]"
client3 = ClientThread("127.0.0.1:" + pport, "http://127.0.0.1:"+ sport1 +"/basic3", "./basic3")
client4 = ClientThread("127.0.0.1:" + pport, "http://127.0.0.1:"+ sport2 +"/basic3", "./basic3")
start = time.time()
client3.start()
client4.start()
client3.join()
client4.join()
end = time.time()
r = False
datafile = open("./basic3", "r")
cdata = datafile.read()
if(end - start) < 4 and client3.data == cdata and client4.data == cdata:
r = True
if r:
print "Concurrent Connection: [" + bcolors.PASS + "PASSED" + bcolors.ENDC + "]"
else:
print "Concurrent Connection: [" + bcolors.FAIL + "FAILED" + bcolors.ENDC + "]"
client5 = ClientThread("127.0.0.1:" + pport, "http://127.0.0.1:"+sport1+"/cacheTest", "./basic")
client5.start()
client5.join()
time.sleep(2)
client6 = ClientThread("127.0.0.1:" + pport, "http://127.0.0.1:"+sport1+"/cacheTest", "./basic")
client6.start()
client6.join()
r = False
if client5.data == client6.data and client5.data != "":
r = True
if r:
print "Caching: [" + bcolors.PASS + "PASSED" + bcolors.ENDC + "]"
else:
print "Caching: [" + bcolors.FAIL + "FAILED" + bcolors.ENDC + "]"
server1.server.shutdown()
server2.server.shutdown()