-
Notifications
You must be signed in to change notification settings - Fork 2
/
chattle.py
85 lines (59 loc) · 1.62 KB
/
chattle.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
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
# -*- coding: utf-8 -*-
import bottle
from bottle import route, redirect, static_file, response, request
from gevent import monkey; monkey.patch_all()
from gevent.event import Event
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
import json
import logging
logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig(filename='chattle.log',level=logging.DEBUG)
def json_loads(string):
return json.loads(string, object_pairs_hook=OrderedDict);
def json_dumps(data):
return json.dumps(data, indent=4)
def printj(data):
print json_dumps(data)
msgs = [
{
'from': 'server',
'content': 'Yori!'
},
{
'from': 'server',
'content': 'whzz uup?'
}
]
event_new_msg = Event()
@route('/')
def index(name='World'):
return open('_res/chattle.html')
@route('/_res/:path#.+#')
def server_static(path):
return static_file(path, root='_res/')
@route('/msgs', method='GET')
def get_items():
start = int(request.params.get('start')) or 0
while True:
new_msgs = msgs[start:]
if len(new_msgs) == 0:
event_new_msg.wait()
else:
event_new_msg.clear()
break
response.content_type = 'application/json;'
return json.dumps(new_msgs, indent=4)
@route('/msgs', method='POST')
def get_items():
data = json_loads(request.body.getvalue())
msgs.append(data)
event_new_msg.set()
return
if __name__ == "__main__":
bottle.debug(True)
bottle.run(host='0.0.0.0', port=8080, server='gevent', reloader=True)
else:
application = bottle.default_app()