Tornadao—请求方法
程序员文章站
2022-06-13 13:22:39
...
# 来实现⼀个上传⽂件并保存在服务器本地的⼩程序upload.py
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.options import options, define
from tornado.web import RequestHandler
define("port", default=8000, type=int, help="run server on the given
port.")
class IndexHandler(RequestHandler):
def get(self):
self.write("hello itcast.")
class UploadHandler(RequestHandler):
def post(self):
files = self.request.files
img_files = files.get('img')
if img_files:
img_file = img_files[0]["body"]
file = open("./itcast", 'w+')
file.write(img_file)
file.close()
self.write("OK")
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application([
(r"/", IndexHandler),
(r"/upload", UploadHandler),
])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
上一篇: pythonweb day10
下一篇: 硕士阶段总结《科苑行》之科研论文