-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfile_uploader.py
43 lines (32 loc) · 1.03 KB
/
file_uploader.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
import tornado.web
import tornado.ioloop
MB = 1024 * 1024
GB = 1024 * MB
TB = 1024 * GB
MAX_STREAMED_SIZE = 1 * GB
# curl -X PUT -H "Content-Type: application/jpg" -H "x-username: cherkavi" -d @uploader-local.png http://127.0.0.1:7777
@tornado.web.stream_request_body
class MainHandler(tornado.web.RequestHandler):
def initialize(self):
print("start upload")
def prepare(self):
self.f = open("/tmp/test.png", "wb")
# dict(self.request.headers)['X-Username']
self.request.connection.set_max_body_size(MAX_STREAMED_SIZE)
def post(self):
print("upload completed")
self.f.flush()
self.f.close()
def put(self):
print("upload completed")
self.f.flush()
self.f.close()
def data_received(self, data):
print("write: %d " % len(data))
self.f.write(data)
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
])
application.listen(7777)
tornado.ioloop.IOLoop.instance().start()