属性 | 说明 |
---|---|
self.request.method | http的请求头;get, post等 |
self.reuqest.uri | 客户端请求完整的uri |
self.request.path | uri的路径名,不包含查询的字符串 |
self.request.query | uri中的查询字符串 |
self.request.version | http/1.1 |
self.request.headers | 请求头 |
self.requset.body | 字符串的消息 |
self.request.remote_ip | 客户端请求的ip |
self.request.protocol | http的协议http or https |
self.request.host | 请求消息的主机名 |
self.request.files | 以字典的方式表达客户端上传的文件; |
self.request.cookies | 客户端的cookies字典 |
self.request.arguments | 客户端提交的参数 |
示例:
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options # 新导入的options模块
tornado.options.define("port", default=8000, type=int, help="服务器监听端口号")
tornado.options.define("content", default=[], type=str, multiple=True, help="控制台输出内容")
class IndexHandler(tornado.web.RequestHandler):
"""主路由处理类"""
def get(self):
self.write("Hello World!")
self.write("\n-------method:\n")
self.write(self.request.method)
self.write("\n-------uri:\n")
self.write(self.request.uri)
self.write("\n-------path:\n")
self.write(self.request.path)
self.write("\n-------query:\n")
self.write(self.request.query)
self.write("\n-------version:\n")
self.write(self.request.version)
self.write("\n-------headers['Accept-Language']:\n")
self.write(self.request.headers["Accept-Language"])
self.write("\n-------body:\n")
self.write(self.request.body)
self.write("\n-------remote_ip:\n")
self.write(self.request.remote_ip)
self.write("\n-------protocol:\n")
self.write(self.request.protocol)
self.write("\n-------host:\n")
self.write(self.request.host)
self.write("\n-------arguments:\n")
self.write(self.request.arguments)
self.write("\n-------query_arguments:\n")
self.write(self.request.query_arguments)
self.write("\n-------body_arguments:\n")
self.write(self.request.body_arguments)
self.write("\n-------files:\n")
self.write(self.request.files)
self.write("\n-------cookies:\n")
self.write(self.request.cookies)
self.write("\n")
#自定义settings
settings = {
"template_path":"templates",
"static_path":"statics",
}
if __name__ == "__main__":
tornado.options.parse_config_file("./config")
print(tornado.options.options.content) # 控制台输出内容
app = tornado.web.Application([
(r"/", IndexHandler),
],debug=True,**settings)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(tornado.options.options.port)
tornado.ioloop.IOLoop.current().start()
示例:
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options # 新导入的options模块
tornado.options.define("port", default=8000, type=int, help="服务器监听端口号")
class IndexHandler(tornado.web.RequestHandler):
"""主路由处理类"""
def get(self):
test = self.get_query_argument("test")
self.write(test)
if __name__ == "__main__":
tornado.options.parse_config_file("./config")
app = tornado.web.Application([
(r"/", IndexHandler),
],debug=True)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(tornado.options.options.port)
tornado.ioloop.IOLoop.current().start()
tornado中对于路由映射也支持正则提取uri,提取出来的参数会作为RequestHandler中对应请求方式的成员方法参数。若在正则表达式中定义了名字,则参数按名传递;若未定义名字,则参数按顺序传递。提取出来的参数会作为对应请求方式的成员方法的参数。
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="服务器监听端口号")
class IndexHandler(RequestHandler):
def get(self):
self.write("hello world.")
class SubjectCityHandler(RequestHandler):
def get(self, subject, city):
self.write(("Subject: %s
City: %s" % (subject, city)))
class SubjectDateHandler(RequestHandler):
def get(self, date, subject):
self.write(("Date: %s
Subject: %s" % (date, subject)))
if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application([
(r"/", IndexHandler),
(r"/sub-city/(.+)/([a-z]+)", SubjectCityHandler), # 无名方式
(r"/sub-date/(?P.+)/(?P\d+)", SubjectDateHandler), # 命名方式
])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
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="服务器监听端口号")
class IndexHandler(RequestHandler):
def get(self):
self.write("hello world.")
class UploadHandler(RequestHandler):
def post(self):
files = self.request.files
img_files = files.get('img')
if img_files:
#tornado.httputil.HTTPFile是接收到的文件对象,它有三个属性:
#1、filename 文件的实际名字,与form_filename1不同,字典中的键名代表的是表单对应项的名字;
#2、body 文件的数据实体;
#3、content_type 文件的类型。 这三个对象属性可以像字典一样支持关键字索引,如
img_file = img_files[0]["body"]
file = open("./filename", '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()
tornado.httputil.HTTPFile是接收到的文件对象,它有三个属性: