Tornadao—请求方法


# 来实现⼀个上传⽂件并保存在服务器本地的⼩程序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()
发布了259 篇原创文章 · 获赞 6 · 访问量 3541

猜你喜欢

转载自blog.csdn.net/piduocheng0577/article/details/105059356