-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.py
executable file
·157 lines (126 loc) · 4.45 KB
/
app.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# encoding: utf-8
import tornado.ioloop
import tornado.httpserver
import tornado.web
from tornado.util import ObjectDict
from tornado.options import options, define, parse_command_line
from tornado.gen import coroutine
import os
import json
import tornado.httpclient
define('debug', default=True, type=bool)
define('port', default=8888, type=int)
define('host', default='127.0.0.1', type=str)
define('ga', default=None, type=str)
class BaseHandler(tornado.web.RequestHandler):
def render(self, *args, **kwargs):
kwargs['ga'] = options.ga
super(BaseHandler, self).render(*args, **kwargs)
class IndexHandler(BaseHandler):
def get(self):
self.render('index.html')
def post(self):
print self.request.arguments
self.write('hi')
class MyFileHandler(BaseHandler):
def get(self):
self.render('myfile.html')
class ManageHandler(BaseHandler):
def write(self, chunk):
if isinstance(chunk, dict):
value = json.dumps(chunk)
else:
value = chunk
self.set_header('Content-Type', 'application/json; charset:utf-8')
self.set_status(200)
super(ManageHandler, self).write(value)
def success(self, data):
response = {
'meta': {
'code': 0,
'msg': 'success!'
},
'data': data
}
self.write(response)
def fail(self, code, msg):
response = {
'meta': {
'code': code,
'msg': msg
},
'data': None
}
self.write(response)
class ListRemoteFileHandler(ManageHandler):
@coroutine
def post(self):
at = self.get_argument('at')
path = self.get_argument('path')
http_client = tornado.httpclient.AsyncHTTPClient()
host = 'rsf.qbox.me'
headers = {
'Host': host,
# 'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'QBox {}'.format(at)
}
url = 'http://{host}{path}'.format(host=host,
path=path)
try:
response = yield http_client.fetch(url,
method='GET',
headers=headers)
except tornado.httpclient.HTTPError as e:
self.fail(e.code, str(e))
else:
self.success(json.loads(response.body))
class DeleteRemoteFileHandler(ManageHandler):
@coroutine
def post(self):
at = self.get_argument('at')
path = self.get_argument('path')
http_client = tornado.httpclient.AsyncHTTPClient()
host = 'rs.qbox.me'
headers = {
'Host': host,
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'QBox {}'.format(at)
}
url = 'http://{host}{path}'.format(host=host,
path=path)
try:
response = yield http_client.fetch(url,
method='POST',
headers=headers,
body='')
except tornado.httpclient.HTTPError as e:
print e.response.body
self.fail(e.code, str(e))
else:
self.success('')
class Application(tornado.web.Application):
def __init__(self):
settings = ObjectDict()
settings.debug = options.debug
settings.autoescape = None
self.base_dir = os.path.abspath(os.path.dirname(__file__))
settings.template_path = os.path.join(self.base_dir, 'templates')
settings.static_path = os.path.join(self.base_dir, 'static')
handlers = [
(r'/', IndexHandler),
(r'/upload', IndexHandler),
(r'/myfile', MyFileHandler),
(r'/myfile/list', ListRemoteFileHandler),
(r'/myfile/delete', DeleteRemoteFileHandler),
]
super(Application, self).__init__(handlers, **settings)
if __name__ == '__main__':
parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
print 'Server is running on http://{host}:{port}'.format(
host=options.host,
port=options.port
)
tornado.ioloop.IOLoop.instance().start()