Web应用程序开发
Web应用程序的本质是什么
简单描述Web应用程序的本质,就是我们通过浏览器访问互联 上指定的 页文件展示到浏览器上。
流程如下图:
WSGI的流程
上述内容是动态开发的根基,只有根据上述内容才可以标准化的动态处理请求。
WSGI定义变量
这些基本上就是WSGI协议中定义的主要变量,也基本上涵盖了我们开发时所需要的变量。
Server端按照协议的内容生成这些environ字典,然后将请求信息交给Application,Application根据这些信息确认请求要处理的内容,然后返回响应消息。从头顺下来就是这个流程。
示例展示
Server端涉及到实现http相关内容,我们直接使用python内置wsgiref来实现,具体代码如下:
import time
from wsgiref.simple_server import make_server
class ResponseTimingMiddleware(object):
“””记录请求耗时”””
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
start_time = time.time()
response = self.app(environ, start_response)
response_time = (time.time() – start_time) * 1000
timing_text = “记录请求耗时中间件输出nn本次请求耗时: {:.10f}msnnn”.format(response_time)
response.append(timing_text.encode(‘utf-8’))
return response
def simple_app(environ, start_response):
“””Simplest possible application object”””
status = ‘200 OK’
response_headers = [(‘Content-type’, ‘text/plain; charset=utf-8’)]
start_response(status, response_headers)
return_body = []
for key, value in environ.items():
return_body.append(“{} : {}”.format(key, value))
return_body.append(“nHello WSGI!”)
# 返回结果必须是bytes
return [“n”.join(return_body).encode(“utf-8”)]
# 创建应用程序
app = ResponseTimingMiddleware(simple_app)
# 启动服务,监听8080
httpd = make_server(‘localhost’, 8080, app)
httpd.serve_forever()
启动服务后,我们打开浏览器访问http://localhost:8080,执行结果如下。
文章知识点与官方知识档案匹配,可进一步学习相关知识Python入门技能树桌面应用开发Tkinter215278 人正在系统学习中 相关资源:tong:tong-桐是一个以学习为目的的GoWeb框架,遵循GPLV3协议-其它…
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!