-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
91 lines (75 loc) · 2.05 KB
/
server.py
File metadata and controls
91 lines (75 loc) · 2.05 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
import time
from datetime import datetime
from flask import Flask, request
app = Flask(__name__)
messages = [
{"username": "Jack", "text": "Hello!", "time": time.time()},
{"username": "Mary", "text": "Hi, Jack!", "time": time.time()},
]
users = {
'Jack': '12345',
'Mary': '54321'
}
@app.route("/")
def hello():
return "Hello, <h1>Welcom to Python meessenger!</h1>!"
@app.route("/status")
def status():
return {
"status": True,
'time': datetime.now().strftime("%H:%M:%S %d.%m.%Y")
}
@app.route("/messages")
def messages_view():
"""
Получение сообщений после отметки after
input: after - отметка времени
:return: {
"messages": [
{"username": str, "text": str, "time": float},
...
]
}
"""
after = float(request.args['after'])
new_messages = [message for message in messages if message['time'] > after]
return {"messages": new_messages}
@app.route("/send", methods=['POST'])
def send_view():
"""
Отправка сообщений
input: {
"username": str,
"text": str
}
:return: {"ok": bool}
"""
data = request.json
username = data["username"]
password = data["password"]
if username not in users or users[username] != password:
return {"ok": False}
text = data["text"]
messages.append({"username": username, "text": text, "time": time.time()})
return {'ok': True}
@app.route("/auth", methods=['POST'])
def sauth_view():
"""
Авторизовать пользователя или сообщить, что пароль неверный.
input: {
"username": str,
"password": str
}
:return: {"ok": bool}
"""
data = request.json
username = data["username"]
password = data["password"]
if username not in users:
users[username] = password
return {"ok": True}
elif users[username] != password:
return {"ok": False}
else:
return {"ok": True}
app.run()