class base_thread(threading.Thread):
def __init__(self, func):
threading.Thread.__init__(self)
self.func = func
#print(type(self.func)) def run(self):
self.func
def multi_thread():
threads = []
for i in range(0, int(br["count"])):
threads.append(base_thread(sample_request(index=i)))
for j in range(0, int(br["count"])):
threads[j].start()
for k in range(0, int(br["count"])):
threads[k].join()
def sample_request(index=0):
h = http_request(base_url=br['baseurl'], http_api=br['httpapi'], method=br['method'], http_params=br['postparams'])
res = h.request()
http_params['request_num'].append(index+1)
if res:
res = '%.2f'%res
#print(res) http_params['response_time'].append(res)
else:
http_params['response_time'].append("0")
if index == int(br['count']) - 1: #如果循环总数循环结束,进行统计操作
class http_request:
def __init__(self, base_url='XXXX', http_port=None, http_api='XXXXX method='post', http_params=None):
self.banse_url = base_url
self.http_port = http_port
self.http_api = http_api
self.http_method = method
self.http_params = http_params
if http_port == "0":
self.http_port = None
else:
self.http_port = http_port
def request(self):
print(HTTP + self.banse_url + self.http_api)
hc.list_arg.append((HTTP + self.banse_url + self.http_api))
try:
http_headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Connection': 'Keep-Alive',
'Referer': HTTP + self.banse_url, 'Accept': 'text/plain'}
# http_params = urllib.parse.urlencode(params)
http_conn = http.client.HTTPConnection(self.banse_url, self.http_port)
start_time = time.time()
http_conn.request(method=self.http_method, url=self.http_api, body=self.http_params, headers=http_headers)
http_response = http_conn.getresponse()
if http_response.status == 200:
print(u"响应时间" + str(time.time() - start_time))
hc.response_time.append("%.2f" %(time.time() - start_time))
return time.time() - start_time
#return http_response.read().decode('utf-8')
else:
print(u"请求失败")
print(http_response.status)
hc.response_time.append(0)
return False
except socket.error:
print(u"请求超时")
hc.response_time.append(0)
print(socket.error)
return False
finally:
http_conn.close()