index.html
html
<!DOCTYPE html>
<html>
<head>
<title>Upload a zip file</title>
</head>
<body>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Upload</button>
</form>
</body>
</html>
python 代码
python
import tornado.web
import zipfile
import os
class UploadHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
def post(self):
file_name = self.request.files['file'][0]['filename']
body = self.request.files['file'][0]['body']
with open(file_name, 'wb') as f:
f.write(body)
# 解压文件
with zipfile.ZipFile(file_name, 'r') as zip_ref:
zip_ref.extractall()
self.write('File uploaded and unzipped successfully')
def make_app():
return tornado.web.Application([
(r"/upload", UploadHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(5000)
tornado.ioloop.IOLoop.current().start()