-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiapp.py
More file actions
182 lines (153 loc) · 6.22 KB
/
miniapp.py
File metadata and controls
182 lines (153 loc) · 6.22 KB
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
#!flask/bin/python
import os
import re
import time
import werkzeug
from colorama import Fore, Style
# TODO: Переделать на фаст апи
from flask import Flask, abort, json, jsonify, make_response, request
app = Flask(__name__)
# задаём параметры для работы приложения
success_input_port = False
test_port = 0
my_path = '/test/foo'
# TODO: Вынести все функции в отдельный модуль
# Функция для вывода красного цвета
def out_red(text):
print(Fore.RED + format(text))
print(Style.RESET_ALL)
# Функция для прочтения json файла
def get_setting(name):
with open(name, 'r', encoding='utf-8') as f: # открыли файл с данными
setting = json.load(f) # загнали все, что получилось в переменную
# print(setting)
return (setting)
# Настройка приложения через ввод параметров в консоли
# TODO: Добавить выбор сохранения результатов. Формат сохранения в хэдер
# TODO: Вынести в отдельный модуль\файл
int_port = 80 # Порт по умолчанию
while success_input_port is False and test_port < 10:
str_port = (input("Введите порт\nport = "))
try:
int_port = int(str_port)
if 0 < int_port < 65535:
success_input_port = True
else:
test_port = test_port + 1
out_red("Порт должен быть от 0 до 65535 !!!")
except:
test_port = test_port + 1
out_red("Порт должен быть числом !!!")
else:
if test_port == 10:
out_red(
"Не получилось ввести порт 10 раз подряд !!!\nСервер прекратит работу через 5 секунд.")
time.sleep(5)
else:
pass
# Попытка сделать унифированый вызов путей из json файла
@app.route(my_path, methods=['GET', 'POST', 'PUT', 'DELETE'])
def test_response():
return make_response(jsonify({'test': 'ok'}), 200)
# функция которая выводит информацию о поступившем запросе
@app.route('/', methods=['GET', 'POST', 'PUT', 'DELETE'], defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def request_info(path):
if path is None:
path = "null"
args = request.args
if len(args) == 0:
str_args = "null"
else:
args_m = []
for i, j in args.to_dict().items():
args_m.append('\n ' + ''.join(i) + ' = ' + ''.join(j))
str_args = ''.join(args_m)
if request.url is None:
url = "null"
else:
url = request.url
if request.method is None:
method = "null"
else:
method = request.method
if request.headers.items() is None:
header = "null"
else:
header = werkzeug.datastructures.Headers(request.headers.items())
len_hed = header.__len__()
# счётчик
i = 0
# пустой массив
a = []
while i < len_hed:
a.append('\n ' + ''.join(header[i][0]) +
' = ' + ''.join(header[i][1:len(header[i])]))
i += 1
str_headers = ''.join(a)
# работа с телом запроса
date_dict = request.form.to_dict()
if len(date_dict) != 0:
str_date = json.dumps(date_dict)
else:
try:
str_date = request.get_data().decode()
except:
save_body = open(
"save_" + time.strftime('%Y-%m-%d.%H.%M.%S') + "_.txt", 'wb')
save_body.write(request.get_data())
save_body.close()
str_date = "Не получилось прочитать тело"
# собираем всю инфу в строку
str_info = '''Получен запрос в ''' + time.strftime('%Y-%m-%d.%H.%M.%S') + '''
url: ''' + url + '''
path: ''' + path + '''
query params: ''' + str_args + '''
method: ''' + method + '''
headers: ''' + str_headers + '''
data: \n''' + str_date
# сохраняем в файл в отдельной папке
# выводим результат в консоль
print(str_info)
return make_response(jsonify({'Info': 'Ok'}), 200)
# функция для проверки задержки
@app.route('/delay', methods=['GET', 'POST', 'PUT', 'DELETE'])
def delay_response():
str_wait = request.args.to_dict().get("wait")
if str_wait is None:
# abort(400, 'Not found query prams "wait"')
return make_response(
jsonify({"error": "Not found query params wait"}), 400
)
else:
try:
int_wait = int(str_wait)
time.sleep(int_wait)
return make_response(jsonify({'delay': str_wait + ' second'}), 200)
except:
return make_response(jsonify({"error": "Params wait must be integer"}), 400)
# функция для получения файла
# TODO: Добавить работу с XML и текстом
@app.route('/return-file', methods=['GET'])
def return_file_response():
str_name = request.args.to_dict().get("name")
if str_name is None:
return make_response(jsonify({"error": "Not found query params name"}), 400)
result = re.split(r'\.', str_name)
if len(result) < 2:
response_str = "You need to specify the file extension."
abort(400, response_str)
if result[1] != "json":
response_str = "Only json files are supported. You have sent: " + \
str(result[1])
abort(400, response_str)
try:
with open(str_name) as json_file:
json_data = json.load(json_file)
return make_response(json_data, 200)
except:
cwd = os.getcwd()
response_str = "Failed to open the file. Check it path: " + cwd
abort(400, response_str)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=int_port)