python web框架
Posted Brown羊羊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python web框架相关的知识,希望对你有一定的参考价值。
Model (数据库操作) View (模板文件) Controller(业务处理)
简称MVC
也可以称MTV
Model Template (类似View) Controller
一个简单web框架
# -*- coding:utf-8 -*- # Author:Brownyangyang from wsgiref.simple_server import make_server def RunServer(environ,start_response): #environ 封装客户端发来的数据 #start_response 封装要返回给用户的数据 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) return [‘<h1>Hello,web!</h1>‘.encode(‘utf-8‘),] if __name__ == ‘__main__‘: # 把ip,端口,函数传给make_server httpd = make_server(‘‘,8000,RunServer) print("Serving HTTP on port 8000......") httpd.serve_forever()
访问不同后缀
# -*- coding:utf-8 -*- # Author:Brownyangyang from wsgiref.simple_server import make_server def handle_index(): return [‘<h1>This is index!</h1>‘.encode (‘utf-8‘), ] def handle_data(): return [‘<h1>This is data!</h1>‘.encode (‘utf-8‘), ] def RunServer(environ,start_response): #environ 封装客户端发来的数据 #start_response 封装要返回给用户的数据 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) current_url = environ[‘PATH_INFO‘] if current_url == ‘/index‘: return handle_index() elif current_url == ‘/data‘: return handle_data() else: return [‘<h1>404</h1>‘.encode (‘utf-8‘), ] if __name__ == ‘__main__‘: # 把ip,端口,函数传给make_server httpd = make_server(‘‘,8000,RunServer) print("Serving HTTP on port 8000......") httpd.serve_forever()
再优化下
# -*- coding:utf-8 -*- # Author:Brownyangyang from wsgiref.simple_server import make_server def handle_index(): return [‘<h1>This is index!</h1>‘.encode (‘utf-8‘), ] def handle_data(): return [‘<h1>This is data!</h1>‘.encode (‘utf-8‘), ] URL_DICT = { "/index":handle_index, "/data":handle_data, } def RunServer(environ,start_response): #environ 封装客户端发来的数据 #start_response 封装要返回给用户的数据 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) current_url = environ[‘PATH_INFO‘] func = None if current_url in URL_DICT: func = URL_DICT[current_url] if func: return func() else: return [‘<h1>404!</h1>‘.encode (‘utf-8‘), ] if __name__ == ‘__main__‘: # 把ip,端口,函数传给make_server httpd = make_server(‘‘,8000,RunServer) print("Serving HTTP on port 8000......") httpd.serve_forever() 如何把上面拆分成MVC呢: 执行脚本a.py如下: # -*- coding:utf-8 -*- # Author:Brownyangyang from wsgiref.simple_server import make_server from Controller import account URL_DICT = { "/index":account.handle_index, "/data":account.handle_data, } def RunServer(environ,start_response): #environ 封装客户端发来的数据 #start_response 封装要返回给用户的数据 start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)]) current_url = environ[‘PATH_INFO‘] func = None if current_url in URL_DICT: func = URL_DICT[current_url] if func: return func() else: return [‘<h1>404!</h1>‘.encode (‘utf-8‘), ] if __name__ == ‘__main__‘: # 把ip,端口,函数传给make_server httpd = make_server(‘‘,8000,RunServer) print("Serving HTTP on port 8000......") httpd.serve_forever()
在a.py当前路径新建View、Controller和Model
View下面建一个index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>index @uu</h1> </body> </html>
Controller下面建一个account.py,专门存放自定义方法
# -*- coding:utf-8 -*- # Author:Brownyangyang import time def handle_index(): current_time = str(time.time()) f = open(‘View/index.html‘,mode=‘rb‘) data = f.read() f.close() data = data.replace(b‘@uu‘,current_time.encode(‘utf-8‘)) return [data, ] def handle_data(): return [‘<h1>This is data!</h1>‘.encode (‘utf-8‘), ]
Model没写,还没有这么复杂
ps:了解下wsgiref模块吧
WSGI(Web Server Common Interface)是专门为Python语言制定的web服务器与应用程序之间的网关接口规范,通俗的来说,只要一个服务器拥有一个实现了WSGI标准规范的模块(例如apache的mod_wsgi模块),那么任意的实现了WSGI规范的应用程序都能与它进行交互。因此,WSGI也主要分为两个程序部分:服务器部分和应用程序部分。
wsgiref则是官方给出的一个实现了WSGI标准用于演示用的简单Python内置库,它实现了一个简单的WSGI Server和WSGI Application(在simple_server模块中),主要分为五个模块:simple_server, util, headers, handlers, validate。
以上是关于python web框架的主要内容,如果未能解决你的问题,请参考以下文章