-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
129 lines (106 loc) · 3.84 KB
/
Copy pathclient.py
File metadata and controls
129 lines (106 loc) · 3.84 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
#------------------------------IMPORT------------------------------#
from flask import Flask, render_template, request, url_for, redirect, session
import requests
import queue
import json
import ast
from requests.exceptions import ConnectionError
#------------------------------------------------------------------#
#-------------------------------INIT-------------------------------#
#--------------ENV--------------#
env = open('.env', 'r')
env = ast.literal_eval(env.read())
#-------------------------------#
#-------------FLASK-------------#
app = Flask(__name__)
app.secret_key = env['SESSION_SECRET']
#-------------------------------#
#--------------MAP--------------#
mapa_cola = {}
semaforo = {}
#-------------------------------#
#------------------------------------------------------------------#
#------------------------------RUTAS-----------------------------#
#------------DEFAULT------------#
@app.route('/')
def index():
return render_template('index.html')
#-------------------------------#
#------------MENSAJE------------#
@app.route('/mensajes', methods=['POST', 'GET'])
def mensajes():
headers = {"Authorization":session['token']}
if request.method == 'POST':
try:
if not (session['token'] in semaforo):
semaforo[session['token']] = True
payload = {'contenido':request.form['contenido']}
url = env['SERVER_HOST']+':'+env['SERVER_PORT']+'/mensajes'
if not (session['token'] in mapa_cola):
mapa_cola[session['token']] = queue.Queue()
mapa_cola[session['token']].put(payload)
if semaforo[session['token']]:
return redirect(url_for('mensajes'))
else:
semaforo[session['token']] = True
while not mapa_cola[session['token']].empty():
requests.post(url, data=list(mapa_cola[session['token']].queue)[0], headers=headers)
mapa_cola[session['token']].get()
semaforo[session['token']] = False
return redirect(url_for('mensajes'))
except ConnectionError:
return redirect(url_for('mensajes'))
else:
error = ""
mensajes={}
try:
url = env['SERVER_HOST']+':'+env['SERVER_PORT']+'/mensajes'
response = requests.get(url, headers=headers)
mensajes = response.json()
except ConnectionError:
error = "Error de conexion"
return render_template('mensajes.html', error = error, mensajes = mensajes)
#-------------------------------#
#-------------LOGIN-------------#
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
try:
url = env['SERVER_HOST']+':'+env['SERVER_PORT']+'/usuario/'+request.form['nombre']
response = requests.get(url)
usuario_id = str(response.json()['id'])
url = env['SERVER_HOST']+':'+env['SERVER_PORT']+'/usuario/'+usuario_id+'/'+request.form['password']
response = requests.get(url)
session.clear()
session['nombre'] = request.form['nombre']
session['token'] = response.json()['token']
return redirect(url_for('index'))
except ConnectionError:
redirect(url_for('login'))
else:
return render_template('login.html')
#-------------------------------#
#-----------REGISTRAR-----------#
@app.route('/registrar', methods=['POST', 'GET'])
def registrar():
if request.method == 'POST':
try:
payload = {'nombre':request.form['nombre'], 'password':request.form['password']}
url = env['SERVER_HOST']+':'+env['SERVER_PORT']+'/usuario'
requests.post(url, data=payload)
return redirect(url_for('login'))
except ConnectionError:
redirect(url_for('registrar'))
else:
return render_template('registrar.html')
#-------------------------------#
#------------LOGOUT-------------#
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('index'))
#-------------------------------#
#------------------------------MAIN------------------------------#
if __name__ == '__main__':
app.run(host=env['CLIENT_HOST'], port=env['CLIENT_PORT'])
#----------------------------------------------------------------#