tornado的请求与响应:
server.py:
# tornado的基础web框架模块
# tornado核心io循环模块,封装了linux的epoll和kqueue,是tornado高效的基础
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
# 导入参数
from project03 import config
from project03.application import Application
if __name__ == "__main__":
# 实例化一个app对象
app = Application()
# 先创建一个服务,再去绑定端口
httpServer = tornado.httpserver.HTTPServer(app)
# 使用变量的值
httpServer.bind(config.options["port"])
# 开启的进程数
httpServer.start(1)
# IOLoop.current():返回当前线程的IOLoop实例
# IOLoop.start():启动IOLoop实例的I/O循环,同时开启监听
tornado.ioloop.IOLoop.current().start()
程序的入口。
config.py:
import os
BASE_DIRS = os.path.dirname(__file__)
options = {
"port": 11112
}
settings = {
"static_path": os.path.join(BASE_DIRS, "static"),
"template_path": os.path.join(BASE_DIRS, "templates"),
"debug": True
}
程序的一些配置文件。
application.py:
import tornado.web
from project03 import config
from project03.views.index import *
from project03.views.index import BBB
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", AAA),
(r"/BBB", BBB, {"word1": "good"}),
(r"/json1", Json),
(r"/status", Status),
(r"/redirect", Redirect),
(r"/error", Error),
# 反向解析
tornado.web.url(r"/fanxiang",Fanxiang,name="fanxiang"),
(r"/canshu1/(\w+)/(\w+)/(\w+)",Canshu1),
(r"/canshu2/(?P<h1>\w+)/(?P<h3>\w+)/(?P<h2>\w+)", Canshu2),
(r"/get1",Jieshou_get1),
(r"/get2", Jieshou_get2),
(r"/post1",Jieshou_post1),
(r"/request",Request_object),
]
super(Application, self).__init__(handlers, **config.settings)
写一些url
views/index.py:
import json
import tornado.web
# 类似于django中的视图
class AAA(tornado.web.RequestHandler):
# 处理get请求
def get(self, *args, **kwargs):
url = self.reverse_url(name="fanxiang")
# 给浏览器响应信息
self.write("<a href='%s'>去反向解析</a>" % (url))
class BBB(tornado.web.RequestHandler):
def initialize(self, word1):
self.word1 = word1
def get(self, *args, **kwargs):
print(self.word1)
self.write("BBBBBBBBBBB")
# 还有一个设置请求头的(两种方法)
# json
class Json(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
per = {
"name": "shao",
"age": 21
}
# 将字典转为json数据
jsonstr = json.dumps(per)
self.write(jsonstr)
# 状态码
class Status(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.set_status(404)
self.write("status")
# 重定向
class Redirect(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.redirect("/")
# 设置状态码的页面
class Error(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
if status_code == 500:
code = 500
self.write("服务器内部错误!!!")
self.set_status(code)
def get(self, *args, **kwargs):
flag = self.get_query_argument("flag")
if flag == "0":
self.send_error(500)
else:
self.write("没错误!!!")
# 反向解析
class Fanxiang(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.write("已经反向解析了!!!")
# 提取url部分内容
class Canshu1(tornado.web.RequestHandler):
def get(self, h1, h2, h3, *args, **kwargs):
print(h1)
print(h2)
print(h3)
self.write("接受参数1")
class Canshu2(tornado.web.RequestHandler):
def get(self, h1, h2, h3, *args, **kwargs):
print(h1)
print(h2)
print(h3)
self.write("接受参数2")
# 接受get参数
class Jieshou_get1(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
a = self.get_query_argument(name="a")
print(a)
b = self.get_query_argument(name="b", default="BBB", strip=True)
print(b)
self.write("get参数")
class Jieshou_get2(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
# get_query_arguments返回列表
a = self.get_query_arguments(name="a")
print(a)
self.write("get参数")
# 接受post参数
class Jieshou_post1(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.render("jieshou_post1.html")
def post(self, *args, **kwargs):
# get_body_argument
name = self.get_body_argument("name")
print(name)
self.write("接受post1")
#关于request
class Request_object(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
# request对象
print(self.request)
# HTTP请求的方式
print(self.request.method)
# 被请求的主机名
print(self.request.host)
# 请求的完整的资源地址,包括路径和get查询参数部分
print(self.request.uri)
# 请求的路径部分
print(self.request.path)
# 请求的参数部分
print(self.request.query)
# 使用的HTTP版本
print(self.request.version)
# 请求头
print(self.request.headers)
# 请求体
print(self.request.body)
# 客户端的ip
print(self.request.remote_ip)
# 上传文件,字典类型
print(self.request.files)
self.write("request对象")
写了一些请求与响应的内容,代码里面有注释,所以在外面就不写了。
templates/jieshou_post1.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/post1" method="post">
name:<input type="text" name="name"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
放一些模板