-
Notifications
You must be signed in to change notification settings - Fork 1
/
learn.py
51 lines (39 loc) · 1.23 KB
/
learn.py
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
# coding = utf-8
from gevent.pywsgi import WSGIServer
from gevent.queue import Queue, Empty
from flask import Flask, render_template, redirect, url_for, request
from forms import LoginForm
from flask_bootstrap import Bootstrap
import simplejson
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config['SECRET_KEY'] = 'HARD'
messages = []
user_dict = {}
@app.route('/put_post/<user>', methods=["POST"])
def put_post(user):
post = request.form['message']
for u in user_dict:
user_dict[u].put_nowait(post)
return ""
@app.route('/get_post/<user>', methods=["GET", "POST"])
def get_post(user):
q = user_dict[user]
try:
p = q.get(timeout=10)
except Empty:
p = ''
return simplejson.dumps(p)
@app.route('/chatroom/<user>/')
def chatroom(user):
user_con = user_dict.setdefault(user, Queue())
return render_template("chatroom.html", user=user)
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
return redirect(url_for("chatroom", user=form.name.data))
return render_template("login.html", form=form)
if __name__ == '__main__':
http_server = WSGIServer(('127.0.0.1', 5000), app)
http_server.serve_forever()