欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tornadao—请求方法

程序员文章站 2022-06-13 13:22:39
...

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()

 

相关标签: PythonWeb框架