Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Доп. задача #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
10 changes: 7 additions & 3 deletions client/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def __init__(self, settings: dict):
self.__settings = settings

self.__HOST = self.__settings.get("url")
iffomko marked this conversation as resolved.
Show resolved Hide resolved
self.__PORT = int(self.__settings.get("port"))
self.__PORT = self.__settings.get("port")

def get_data(self) -> str:
"""
Expand All @@ -21,7 +21,11 @@ def get_data(self) -> str:
timeout = self.__settings.get("timeout")

s.settimeout(timeout)
s.connect((self.__HOST, self.__PORT))

try:
s.connect((self.__HOST, int(self.__PORT)))
except ValueError:
return 'Неправильный url адрес или port'

request = self.create_http_request(self.__settings).encode()

Expand Down Expand Up @@ -75,7 +79,7 @@ def get_headers(settings: dict) -> str:
cookies.append(f'{key}={value};')

if len(cookies) != 0:
cookies = 'Cookie: ' + ' '.join(cookies)
cookies = 'Cookie: ' + ' '.join(cookies)
cookies = cookies[:-1]
cookies += '\r\n'
headers.append(cookies)
Expand Down
140 changes: 75 additions & 65 deletions resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@
</head>

<body>
<div class="form_wrapper">
<h1 class="form-title">HTTP-Client</h1>
<p class="error-message">Ошибка! Вы не ввели либо url, либо port, либо тип запроса!</p>
<div class="container form-container">
<form class="form">
<div class="wrapper url_wrapper">
<label id="url_label" for="url">Url-адрес: </label>
<input id="url" class="url" type="text" name="url" placeholder="Введите url...">
</div>
<div class="wrapper port_wrapper">
<label id="port_label" for="port">Порт: </label>
<input id="port" class="port" type="text" name="port" placeholder="Введите port...">
</div>
<div class="wrapper timeout_wrapper">
<label id="timeout_label" for="timeout">Таймаут: </label>
<input id="timeout" class="timeout" type="text" name="timeout" placeholder="Введите таймаут ожидания запроса...">
</div>
<div class="wrapper request_wrapper">
<div class="request-title">Тип запроса: </div>
<div class="form_wrapper">
<h1 class="form-title">HTTP-Client</h1>
<p class="error-message">Ошибка! Вы не ввели либо url, либо port, либо тип запроса!</p>
<div class="container form-container">
<form class="form">
<div class="wrapper url_wrapper">
<label id="url_label" for="url">Url-адрес: </label>
<input id="url" class="url" type="text" name="url" placeholder="Введите url...">
</div>
<div class="wrapper port_wrapper">
<label id="port_label" for="port">Порт: </label>
<input id="port" class="port" type="text" name="port" placeholder="Введите port...">
</div>
<div class="wrapper timeout_wrapper">
<label id="timeout_label" for="timeout">Таймаут: </label>
<input id="timeout" class="timeout" type="text" name="timeout" placeholder="Введите таймаут ожидания запроса...">
</div>
<div class="wrapper request_wrapper">
<div class="request-title">Тип запроса: </div>
<div class="request_btns">
<div class="request-btn_container">
<div class="request-container">
<div class="request-btn_wrapper">
Expand Down Expand Up @@ -67,59 +68,68 @@ <h1 class="form-title">HTTP-Client</h1>
</div>
</div>
</div>
</div>
<div class="wrapper headers_wrapper">
<div class="label-container">
<label for="headers" id="headers_label">Заголовки:</label>
<p class="warning_wrapper">
<span class="warning_important">*</span>
Заголовки должны быть записаны в формате:<br>
<span class="warning_ex">Заголовок: значение;</span>
<span class="warning_note">И обязательно должны быть разделены точкой с запятой</span>
</p>
</div>
<textarea
name="headers"
id="headers"
class="headers-textarea"
placeholder="Впишите суда заголовки..."
name="get_form"
id="get_form"
cols="30"
rows="10"
class="get_form"
placeholder="Введите строку, вхождения которой вы хотите найти..."
></textarea>
</div>
<div class="wrapper cookie_wrapper">
<div class="label-container">
<label for="cookie" id="cookie_label">Cookie:</label>
<p class="warning_wrapper">
<span class="warning_important">*</span>
Cookie должны быть записаны в формате:<br>
<span class="warning_ex">Название: значение;</span>
<span class="warning_note">И обязательно должны быть разделены точкой с запятой</span>
</p>
</div>
<textarea
name="cookie"
id="cookie"
class="cookie-textarea"
placeholder="Впишите суда Cookie..."
></textarea>
</div>
<div class="wrapper headers_wrapper">
<div class="label-container">
<label for="headers" id="headers_label">Заголовки:</label>
<p class="warning_wrapper">
<span class="warning_important">*</span>
Заголовки должны быть записаны в формате:<br>
<span class="warning_ex">Заголовок: значение;</span>
<span class="warning_note">И обязательно должны быть разделены точкой с запятой</span>
</p>
</div>
<div class="wrapper body_wrapper">
<label for="body" id="body_label">Тело запроса:</label>
<textarea
name="body"
id="body"
class="body-textarea"
placeholder="Впишите суда тело запроса..."
></textarea>
<textarea
name="headers"
id="headers"
class="headers-textarea"
placeholder="Впишите суда заголовки..."
></textarea>
</div>
<div class="wrapper cookie_wrapper">
<div class="label-container">
<label for="cookie" id="cookie_label">Cookie:</label>
<p class="warning_wrapper">
<span class="warning_important">*</span>
Cookie должны быть записаны в формате:<br>
<span class="warning_ex">Название: значение;</span>
<span class="warning_note">И обязательно должны быть разделены точкой с запятой</span>
</p>
</div>
<button class="submit-btn" type="submit">Сделать запрос</button>
</form>
</div>
<div class="result">
<div class="container result-container">
<h1 class="result__title">Результат запроса:</h1>
<p class="result__text"></p>
<textarea
name="cookie"
id="cookie"
class="cookie-textarea"
placeholder="Впишите суда Cookie..."
></textarea>
</div>
<div class="wrapper body_wrapper">
<label for="body" id="body_label">Тело запроса:</label>
<textarea
name="body"
id="body"
class="body-textarea"
placeholder="Впишите суда тело запроса..."
></textarea>
</div>
<button class="submit-btn" type="submit">Сделать запрос</button>
</form>
</div>
<div class="result">
<div class="container result-container">
<h1 class="result__title">Результат запроса:</h1>
<p class="result__text"></p>
</div>
</div>
</div>
</body>
</html>
37 changes: 27 additions & 10 deletions resources/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ textarea::placeholder {
width: 35%;
}

.request_btns {
display: flex;
align-items: flex-start;
justify-content: space-between;

width: 100%;
}

.get_form {
display: none;

margin-left: 15px;
}

.get_form__active {
display: block;
}

/* Wrappers styles */

.form_wrapper {
Expand Down Expand Up @@ -255,13 +273,7 @@ textarea::placeholder {
/* Result styles */

.result-container {
flex-direction: column;
}

.result__text {
font-size: 15px;
color: #333;
font-weight: 400;
flex-direction: column;
}

.result {
Expand All @@ -281,8 +293,13 @@ textarea::placeholder {
}

.result__text {
width: 100%;
font-size: 15px;
color: #333;
font-weight: 400;

width: 100%;

hyphens: auto;
white-space: pre-wrap;
hyphens: auto;
white-space: pre-wrap;
word-wrap: break-word;
}
39 changes: 30 additions & 9 deletions resources/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const onSubmit = (event) => {

const body = JSON.stringify(fields)

if (fields.request !== 'get') {
fields.get_form = ''
}

fetch('http://127.0.0.1:8080/', {
method: 'post',
headers: {
Expand All @@ -58,16 +62,21 @@ const onSubmit = (event) => {
})
.then(response => response.text())
.then(data => {
const result_text = document.querySelector('.result__text');
result_text.textContent = data;
const result_text = document.querySelector('.result__text');

console.log(data)

const result = document.querySelector('.result');
const jsonResponse = JSON.parse(data)

if (!result.classList.contains('result-active')) {
result.classList.add('result-active')
result_text.textContent = JSON.stringify(jsonResponse)

const result = document.querySelector('.result');

if (!result.classList.contains('result-active')) {
result.classList.add('result-active')
}
}
}
);
);

return true
}
Expand Down Expand Up @@ -117,6 +126,18 @@ const parse = (headers) => {
return response.length === 0 ? {} : response
}

window.addEventListener('load', () => {
const radioBtnChecker = e => {
if (e.target.tagName.toLowerCase() === 'input' && e.target.id.toLowerCase() === 'get-request') {
document.querySelector('#get_form').classList.add('get_form__active');
return null
}

document.querySelector('#get_form').classList.remove('get_form__active');
}

const main = () => {
document.querySelector('.form').addEventListener('submit', onSubmit)
})
document.querySelector('.request-btn_container').addEventListener('click', radioBtnChecker)
}

window.addEventListener('load', main)
Loading