-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.py
61 lines (40 loc) · 1.47 KB
/
cookies.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
# coding=utf-8\
import os.path
from tornado import web, httpserver, ioloop
from tornado.options import define, options
define('port', default=8000, help='run on the given port', type=int)
class Application(web.Application):
def __init__(self):
handlers = [
(r'/', WelcomeHandler),
(r'/login', LoginHandler),
(r'/logout', LogoutHandler)]
settings = {
'template_path': os.path.join(os.path.dirname(__file__), 'templates'),
'cookie_secret': 'f0008374-f3ec-455d-a57e-73e49a82e106',
'xrsf_cookies': True,
'login_url': "/login",
'debug': True}
web.Application.__init__(self, handlers, **settings)
class BaseHandler(web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie('username')
class LoginHandler(BaseHandler):
def get(self):
self.render('login.html')
def post(self):
self.set_secure_cookie('username', self.get_argument('username'))
self.redirect('/')
class WelcomeHandler(BaseHandler):
@web.authenticated
def get(self):
self.render('index.html', user=self.current_user)
class LogoutHandler(BaseHandler):
def get(self):
self.clear_cookie('username')
self.redirect('/')
if __name__ == '__main__':
options.parse_command_line()
http_server = httpserver.HTTPServer(Application())
http_server.listen(options.port)
ioloop.IOLoop.instance().start()