-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvsearch4web.py
More file actions
104 lines (79 loc) · 3.1 KB
/
vsearch4web.py
File metadata and controls
104 lines (79 loc) · 3.1 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
from flask import Flask, render_template, request, escape, session
from vsearch import search4letters
from DBcm import UseDatabase, ConnectionError, CredentialsError, SQLError
from time import sleep
from threading import Thread
from flask import copy_current_request_context
from checker import check_logged_in
app = Flask(__name__)
app.config['dbconfig'] = {'host': '127.0.0.1',
'user': 'root',
'password': '4553',
'database': 'vsearchlogDB', }
@app.route('/login')
def do_login() -> str:
session['logged_in'] = True
return "You are now logged in!"
@app.route('/logout')
def logout() -> str:
session.pop('logged_in')
return "You are now logged out!"
@app.route('/search4', methods=['POST'])
def do_search() -> 'html':
@copy_current_request_context
def log_request(req: 'flask_request', res: str) -> None:
sleep(5)
with UseDatabase(app.config['dbconfig']) as cursor:
_SQL = """insert into log
(phrase, letters, ip, browser_string, results)
values
(%s, %s, %s, %s, %s)"""
cursor.execute(_SQL, (req.form['phrase'],
req.form['letters'],
req.remote_addr,
str(req.user_agent),
res, ))
phrase = request.form['phrase']
letters = request.form['letters']
title = 'Here are your results:'
results = str(search4letters(phrase, letters))
try:
t = Thread(target=log_request,args=(request,results))
t.start()
except Exception as err:
print("***** Logging failed with this error:", err)
return render_template('results.html',
the_title=title,
the_phrase=phrase,
the_letters=letters,
the_results=results,)
@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
return render_template('entry.html',
the_title='Welcome to search4letters on the web!')
@app.route('/viewlog')
@check_logged_in
def view_the_log() -> 'html':
try:
with UseDatabase(app.config['dbconfig']) as cursor:
_SQL = """select ip, browser_string, phrase, letters, results from log;"""
cursor.execute(_SQL)
contents = cursor.fetchall()
titles = ("IP", "User_agent", "Remote_addr", "Letters", "Results")
return render_template('viewlog.html',
the_title = 'View Log',
the_row_titles = titles,
the_data=contents,)
except ConnectionError as err:
print("Ошибка 1")
except CredentialsError as err:
print("Ошибка 2")
except SQLError as err:
print("Ошибка 3")
except Exception as err:
print("Неизвестная ошибка")
return "Error"
app.secret_key = "AkbarLox"
if __name__ == '__main__':
app.run(debug=True)